Skip to content

Commit

Permalink
Merge pull request #3183 from ampproject/add/1840-reliable-builds
Browse files Browse the repository at this point in the history
Add a more reliable build step
  • Loading branch information
westonruter authored Sep 9, 2019
2 parents 0a6f6dd + 2a86f5d commit 24fb61a
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 119 deletions.
59 changes: 53 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@
module.exports = function( grunt ) {
'use strict';
require( 'dotenv' ).config();

// Root paths to include in the plugin build ZIP when running `npm run build`.
const productionIncludedRootFiles = [
'LICENSE',
'amp.php',
'assets',
'back-compat',
'includes',
'readme.txt',
'templates',
'vendor',
];

// These patterns paths will be excluded from among the above directory.
const productionExcludedPathPatterns = [
/.*\/src\/.*/,
/.*images\/stories-editor\/.*\.svg/,
];

// These will be removed from the vendor directory after installing but prior to creating a ZIP.
// ⚠️ Warning: These paths are passed straight to rm command in the shell, without any escaping.
const productionVendorExcludedFilePatterns = [
'composer.*',
'vendor/*/*/.editorconfig',
'vendor/*/*/.gitignore',
'vendor/*/*/composer.*',
'vendor/*/*/Doxyfile',
'vendor/*/*/LICENSE',
'vendor/*/*/phpunit.*',
'vendor/*/*/*.md',
'vendor/*/*/*.txt',
'vendor/*/*/*.yml',
'vendor/*/*/.*.yml',
'vendor/*/*/tests',
];

grunt.initConfig( {

pkg: grunt.file.readJSON( 'package.json' ),
Expand Down Expand Up @@ -32,6 +68,9 @@ module.exports = function( grunt ) {
verify_matching_versions: {
command: 'php bin/verify-version-consistency.php',
},
composer_install: {
command: 'if [ ! -e build ]; then echo "Run grunt build first."; exit 1; fi; cd build; composer install --no-dev -o && composer remove cweagans/composer-patches --update-no-dev -o && rm -r ' + productionVendorExcludedFilePatterns.join( ' ' ),
},
create_build_zip: {
command: 'if [ ! -e build ]; then echo "Run grunt build first."; exit 1; fi; if [ -e amp.zip ]; then rm amp.zip; fi; cd build; zip -r ../amp.zip .; cd ..; echo; echo "ZIP of build: $(pwd)/amp.zip"',
},
Expand Down Expand Up @@ -95,17 +134,24 @@ module.exports = function( grunt ) {
const versionAppend = new Date().toISOString().replace( /\.\d+/, '' ).replace( /-|:/g, '' ) + '-' + commitHash;

const paths = lsOutput.trim().split( /\n/ ).filter( function( file ) {
return ! /^(blocks|\.|bin|([^/]+)+\.(md|json|xml)|Gruntfile\.js|tests|wp-assets|readme\.md|composer\..*|patches|webpack.*|assets\/images\/stories-editor\/.*\.svg|assets\/src|assets\/css\/src|docker-compose\.yml|.*\.config\.js|codecov\.yml|example\.env)/.test( file );
const topSegment = file.replace( /\/.*/, '' );
if ( ! productionIncludedRootFiles.includes( topSegment ) ) {
return false;
}

for ( const productionExcludedPathPattern of productionExcludedPathPatterns ) {
if ( productionExcludedPathPattern.test( file ) ) {
return false;
}
}

return true;
} );

paths.push( 'vendor/autoload.php' );
paths.push( 'composer.*' ); // Copy in order to be able to do run composer_install.
paths.push( 'assets/js/*.js' );
paths.push( 'assets/js/*.deps.json' );
paths.push( 'assets/css/*.css' );
paths.push( 'vendor/composer/**' );
paths.push( 'vendor/sabberworm/php-css-parser/lib/**' );
paths.push( 'vendor/fasterimage/fasterimage/src/**' );
paths.push( 'vendor/willwashburn/stream/src/**' );

grunt.config.set( 'copy', {
build: {
Expand Down Expand Up @@ -138,6 +184,7 @@ module.exports = function( grunt ) {
} );
grunt.task.run( 'readme' );
grunt.task.run( 'copy' );
grunt.task.run( 'shell:composer_install' );

done();
}
Expand Down
56 changes: 56 additions & 0 deletions bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

# Exit if any command fails.
set -e

# Change to the expected directory.
cd "$(dirname "$0")"
cd ..
PLUGIN_DIR=$(pwd)

# Enable nicer messaging for build status.
BLUE_BOLD='\033[1;34m';
GREEN_BOLD='\033[1;32m';
RED_BOLD='\033[1;31m';
YELLOW_BOLD='\033[1;33m';
COLOR_RESET='\033[0m';
error () {
echo -e "\n${RED_BOLD}$1${COLOR_RESET}\n"
}
status () {
echo -e "\n${BLUE_BOLD}$1${COLOR_RESET}\n"
}
success () {
echo -e "\n${GREEN_BOLD}$1${COLOR_RESET}\n"
}
warning () {
echo -e "\n${YELLOW_BOLD}$1${COLOR_RESET}\n"
}

status "Time to release AMP ⚡️"

status "Setting up a fresh build environment in a temporary folder. ✨"

# Create a fresh temporary folder in a way that works across platforms.
BUILD_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'amp-production-build')

# Do a local clone to move the current files across.
git clone -l . "$BUILD_DIR"
cd "$BUILD_DIR"

# Run the build.
status "Installing dependencies... 📦"
composer install
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm install

status "Generating build... ⚙️"
npm run build

status "Copying the ZIP file back over... ↩️"
rm -f "$PLUGIN_DIR/amp.zip"
cp "$BUILD_DIR/amp.zip" "$PLUGIN_DIR/amp.zip"

status "Removing the temporary folder again... 🗑️"
rm -rf "$BUILD_DIR"

success "You've built AMP! 🎉 \nThe ZIP file can be found in the following location:\n$PLUGIN_DIR/amp.zip"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"extra": {
"patches": {
"sabberworm/php-css-parser": {
"PHP-CSS-Parser: Fix parsing CSS selectors which contain commas <https://github.com/sabberworm/PHP-CSS-Parser/pull/138>": "patches/php-css-parser-mods.diff"
"PHP-CSS-Parser: Fix parsing CSS selectors which contain commas <https://github.com/sabberworm/PHP-CSS-Parser/pull/138>": "https://github.com/sabberworm/PHP-CSS-Parser/commit/fa139f65c5b098ae652c970b25e6eb03fc495eb4.diff"
}
}
}
Expand Down
22 changes: 19 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 0 additions & 109 deletions patches/php-css-parser-mods.diff

This file was deleted.

0 comments on commit 24fb61a

Please sign in to comment.