Skip to content

Commit

Permalink
🏗 Consolidate all Travis-related state in build-system/travis.js (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha authored Feb 12, 2019
1 parent 8ba524e commit b843500
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 83 deletions.
3 changes: 2 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

'use strict';

const {isTravisBuild} = require('./build-system/travis');
const minimist = require('minimist');
const argv = minimist(process.argv.slice(2));

Expand All @@ -40,7 +41,7 @@ module.exports = function(api) {
'modules': 'commonjs',
'loose': true,
'targets': {
'browsers': process.env.TRAVIS ?
'browsers': isTravisBuild() ?
['Last 2 versions', 'safari >= 9'] : ['Last 2 versions'],
},
}],
Expand Down
3 changes: 2 additions & 1 deletion build-system/ctrlcHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const colors = require('ansi-colors');
const log = require('fancy-log');
const {execScriptAsync, exec} = require('./exec');
const {isTravisBuild} = require('./travis');

const {green, cyan} = colors;

Expand All @@ -31,7 +32,7 @@ const killSuffix = (process.platform == 'win32') ? '>NUL' : '';
* @param {string} command
*/
exports.createCtrlcHandler = function(command) {
if (!process.env.TRAVIS) {
if (!isTravisBuild()) {
log(green('Running'), cyan(command) + green('. Press'), cyan('Ctrl + C'),
green('to cancel...'));
}
Expand Down
25 changes: 15 additions & 10 deletions build-system/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
*/

const colors = require('ansi-colors');
const {
isTravisBuild,
isTravisPullRequestBuild,
travisPullRequestBranch,
travisPullRequestSha,
} = require('./travis');
const {getStdout} = require('./exec');

const commitLogMaxCount = 100;
Expand All @@ -30,9 +36,9 @@ const commitLogMaxCount = 100;
* @return {string}
*/
exports.gitMergeBaseMaster = function() {
if (process.env.TRAVIS) {
return getStdout(
`git merge-base master ${process.env.TRAVIS_PULL_REQUEST_SHA}`).trim();
if (isTravisBuild()) {
const traviPrSha = travisPullRequestSha();
return getStdout(`git merge-base master ${traviPrSha}`).trim();
}
return gitMergeBaseLocalMaster();
};
Expand Down Expand Up @@ -120,13 +126,12 @@ exports.gitDiffColor = function() {
};

/**
* Returns the name of the branch from which the PR originated. On Travis, this
* is exposed via TRAVIS_PULL_REQUEST_BRANCH.
* Returns the name of the branch from which the PR originated.
* @return {string}
*/
exports.gitBranchName = function() {
return process.env.TRAVIS ?
process.env.TRAVIS_PULL_REQUEST_BRANCH :
return isTravisPullRequestBuild() ?
travisPullRequestBranch() :
getStdout('git rev-parse --abbrev-ref HEAD').trim();
};

Expand All @@ -135,8 +140,8 @@ exports.gitBranchName = function() {
* @return {string}
*/
exports.gitCommitHash = function() {
if (process.env.TRAVIS_PULL_REQUEST_SHA) {
return process.env.TRAVIS_PULL_REQUEST_SHA;
if (isTravisPullRequestBuild()) {
return travisPullRequestSha();
}
return getStdout('git rev-parse --verify HEAD').trim();
};
Expand Down Expand Up @@ -182,7 +187,7 @@ function gitMergeBaseLocalMaster() {
* @return {string}
*/
function gitMasterBaseline() {
if (process.env.TRAVIS) {
if (isTravisBuild()) {
return exports.gitTravisMasterBaseline();
}
return gitMergeBaseLocalMaster();
Expand Down
27 changes: 18 additions & 9 deletions build-system/pr-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const {
gitTravisMasterBaseline,
shortSha,
} = require('./git');
const {
isTravisBuild,
isTravisPullRequestBuild,
isTravisPushBuild,
travisPullRequestSha,
} = require('./travis');
const {execOrDie, exec, getStderr, getStdout} = require('./exec');

const fileLogPrefix = colors.bold(colors.yellow('pr-check.js:'));
Expand Down Expand Up @@ -99,13 +105,13 @@ function timedExecOrDie(cmd) {
* Prints a summary of files changed by, and commits included in the PR.
*/
function printChangeSummary() {
if (process.env.TRAVIS) {
if (isTravisBuild()) {
console.log(fileLogPrefix, colors.cyan('origin/master'),
'is currently at commit',
colors.cyan(shortSha(gitTravisMasterBaseline())));
console.log(fileLogPrefix,
'Testing the following changes at commit',
colors.cyan(shortSha(process.env.TRAVIS_PULL_REQUEST_SHA)));
colors.cyan(shortSha(travisPullRequestSha())));
}

const filesChanged = gitDiffStatMaster();
Expand Down Expand Up @@ -385,7 +391,7 @@ const command = {
}
// Unit tests with Travis' default chromium in coverage mode.
timedExecOrDie(cmd + ' --headless --coverage');
if (process.env.TRAVIS) {
if (isTravisBuild()) {
// A subset of unit tests on other browsers via sauce labs
cmd = cmd + ' --saucelabs_lite';
startSauceConnect();
Expand All @@ -408,7 +414,7 @@ const command = {
if (compiled) {
cmd += ' --compiled';
}
if (process.env.TRAVIS) {
if (isTravisBuild()) {
if (coverage) {
// TODO(choumx, #19658): --headless disabled for integration tests on
// Travis until Chrome 72.
Expand All @@ -432,7 +438,7 @@ const command = {
timedExecOrDie('rm -R dist');
},
runVisualDiffTests: function(opt_mode) {
if (process.env.TRAVIS) {
if (isTravisBuild()) {
process.env['PERCY_TOKEN'] = atob(process.env.PERCY_TOKEN_ENCODED);
} else if (!process.env.PERCY_PROJECT || !process.env.PERCY_TOKEN) {
console.log(
Expand Down Expand Up @@ -474,7 +480,9 @@ function runAllCommands() {
command.testBuildSystem();
command.cleanBuild();
command.buildRuntime();
command.runVisualDiffTests(/* opt_mode */ 'master');
if (isTravisPushBuild()) {
command.runVisualDiffTests(/* opt_mode */ 'master');
}
command.runLintCheck();
command.runJsonCheck();
command.runDepAndTypeChecks();
Expand Down Expand Up @@ -576,7 +584,7 @@ function main() {
runYarnLockfileCheck();

// Run the local version of all tests.
if (!process.env.TRAVIS) {
if (!isTravisBuild()) {
process.env['LOCAL_PR_CHECK'] = true;
printChangeSummary();
console.log(fileLogPrefix, 'Running all pr-check commands locally.');
Expand All @@ -590,8 +598,9 @@ function main() {
colors.cyan(process.env.BUILD_SHARD),
'\n');

if (process.env.TRAVIS_EVENT_TYPE === 'push') {
console.log(fileLogPrefix, 'Running all commands on push build.');
if (!isTravisPullRequestBuild()) {
console.log(fileLogPrefix,
'Running all commands, since this is not a PR build.');
runAllCommands();
stopTimer('pr-check.js', startTime);
return 0;
Expand Down
3 changes: 2 additions & 1 deletion build-system/single-pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const relativePath = require('path').relative;
const tempy = require('tempy');
const through = require('through2');
const {extensionBundles, altMainBundles, TYPES} = require('../bundles.config');
const {isTravisBuild} = require('./travis');
const {TopologicalSort} = require('topological-sort');
const TYPES_VALUES = Object.keys(TYPES).map(x => TYPES[x]);
const wrappers = require('./compile-wrappers');
Expand Down Expand Up @@ -419,7 +420,7 @@ function isCommonJsModule(file) {
* @param {!Object} config
*/
function transformPathsToTempDir(graph, config) {
if (!process.env.TRAVIS) {
if (!isTravisBuild()) {
log('Writing transforms to', colors.cyan(graph.tmp));
}
// `sorted` will always have the files that we need.
Expand Down
3 changes: 2 additions & 1 deletion build-system/tasks/ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

const ava = require('gulp-ava');
const gulp = require('gulp-help')(require('gulp'));
const {isTravisBuild} = require('../travis');

/**
* Runs ava tests.
Expand All @@ -27,7 +28,7 @@ function runAvaTests() {
'get-zindex/test.js',
'prepend-global/test.js',
])
.pipe(ava({silent: !!process.env.TRAVIS}));
.pipe(ava({silent: isTravisBuild()}));
}

gulp.task('ava', 'Runs ava tests for gulp tasks', runAvaTests);
22 changes: 9 additions & 13 deletions build-system/tasks/bundle-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const octokit = require('@octokit/rest')();
const path = require('path');
const requestPost = BBPromise.promisify(require('request').post);
const url = require('url');
const {
isTravisPullRequestBuild,
isTravisPushBuild,
travisRepoSlug,
} = require('../travis');
const {getStdout} = require('../exec');
const {gitCommitHash, gitTravisMasterBaseline} = require('../git');

Expand Down Expand Up @@ -57,29 +62,20 @@ function getGzippedBundleSize() {
throw Error('could not infer bundle size from output.');
}

/**
* Return true if this task is running on Travis as part of a pull request.
*
* @return {boolean} true if running on Travis as part of a pull request.
*/
function isPullRequest() {
return process.env.TRAVIS && process.env.TRAVIS_EVENT_TYPE === 'pull_request';
}

/**
* Store the bundle size of a commit hash in the build artifacts storage
* repository to the passed value.
*
* @return {!Promise}
*/
function storeBundleSize() {
if (!process.env.TRAVIS || process.env.TRAVIS_EVENT_TYPE !== 'push') {
if (!isTravisPushBuild()) {
log(yellow('Skipping'), cyan('--on_push_build') + ':',
'this action can only be performed on `push` builds on Travis');
return;
}

if (process.env.TRAVIS_REPO_SLUG !== expectedGitHubRepoSlug) {
if (travisRepoSlug() !== expectedGitHubRepoSlug) {
log(yellow('Skipping'), cyan('--on_push_build') + ':',
'this action can only be performed on Travis builds on the',
cyan(expectedGitHubRepoSlug), 'repository');
Expand Down Expand Up @@ -127,7 +123,7 @@ function storeBundleSize() {
* Mark a pull request on Travis as skipped, via the AMP bundle-size GitHub App.
*/
async function skipBundleSize() {
if (isPullRequest()) {
if (isTravisPullRequestBuild()) {
const commitHash = gitCommitHash();
try {
const response = await requestPost(url.resolve(bundleSizeAppBaseUrl,
Expand All @@ -153,7 +149,7 @@ async function skipBundleSize() {
* Report the size to the bundle-size GitHub App, to determine size changes.
*/
async function reportBundleSize() {
if (isPullRequest()) {
if (isTravisPullRequestBuild()) {
const baseSha = gitTravisMasterBaseline();
const bundleSize = parseFloat(getGzippedBundleSize());
const commitHash = gitCommitHash();
Expand Down
5 changes: 3 additions & 2 deletions build-system/tasks/check-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const log = require('fancy-log');
const markdownLinkCheck = BBPromise.promisify(require('markdown-link-check'));
const path = require('path');
const {gitDiffAddedNameOnlyMaster, gitDiffNameOnlyMaster} = require('../git');
const {isTravisBuild} = require('../travis');

const maybeUpdatePackages = process.env.TRAVIS ? [] : ['update-packages'];
const maybeUpdatePackages = isTravisBuild() ? [] : ['update-packages'];

/**
* Parses the list of files in argv, or extracts it from the commit log.
Expand Down Expand Up @@ -70,7 +71,7 @@ function checkLinks() {
deadLinksFound = true;
deadLinksFoundInFile = true;
log('[%s] %s', colors.red('✖'), result.link);
} else if (!process.env.TRAVIS) {
} else if (!isTravisBuild()) {
log('[%s] %s', colors.green('✔'), result.link);
}
});
Expand Down
3 changes: 2 additions & 1 deletion build-system/tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const closureCompiler = require('gulp-closure-compiler');
const colors = require('ansi-colors');
const fs = require('fs-extra');
const gulp = require('gulp');
const {isTravisBuild} = require('../travis');
const {VERSION: internalRuntimeVersion} = require('../internal-version') ;

const rename = require('gulp-rename');
Expand All @@ -46,7 +47,7 @@ exports.closureCompile = function(entryModuleFilename, outputDir,
inProgress++;
compile(entryModuleFilename, outputDir, outputFilename, options)
.then(function() {
if (process.env.TRAVIS) {
if (isTravisBuild()) {
// Print a progress dot after each task to avoid Travis timeouts.
process.stdout.write('.');
}
Expand Down
3 changes: 2 additions & 1 deletion build-system/tasks/dep-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ const path = require('path');
const source = require('vinyl-source-stream');
const through = require('through2');
const {createCtrlcHandler, exitCtrlcHandler} = require('../ctrlcHandler');
const {isTravisBuild} = require('../travis');


const root = process.cwd();
const absPathRegExp = new RegExp(`^${root}/`);
const red = msg => log(colors.red(msg));

const maybeUpdatePackages = process.env.TRAVIS ? [] : ['update-packages'];
const maybeUpdatePackages = isTravisBuild() ? [] : ['update-packages'];

/**
* @typedef {{
Expand Down
14 changes: 9 additions & 5 deletions build-system/tasks/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
'use strict';

const {gitCommitterEmail} = require('../git');
const {isTravisBuild, travisJobNumber} = require('../travis');

const COMMON_CHROME_FLAGS = [
// Dramatically speeds up iframe creation time.
'--disable-extensions',
Expand Down Expand Up @@ -66,7 +69,7 @@ module.exports = {
['babelify', {'sourceMapsAbsolute': true}],
],
// Prevent "cannot find module" errors on Travis. See #14166.
bundleDelay: process.env.TRAVIS ? 5000 : 1200,
bundleDelay: isTravisBuild() ? 5000 : 1200,
},

reporters: ['super-dots', 'karmaSimpleReporter'],
Expand Down Expand Up @@ -128,7 +131,7 @@ module.exports = {
autoWatch: true,

browsers: [
process.env.TRAVIS ? 'Chrome_travis_ci' : 'Chrome_no_extensions',
isTravisBuild() ? 'Chrome_travis_ci' : 'Chrome_no_extensions',
],

customLaunchers: {
Expand Down Expand Up @@ -226,7 +229,8 @@ module.exports = {

sauceLabs: {
testName: 'AMP HTML on Sauce',
tunnelIdentifier: process.env.TRAVIS_JOB_NUMBER,
// Identifier used in build-system/sauce_connect/start_sauce_connect.sh.
tunnelIdentifier: isTravisBuild() ? travisJobNumber() : gitCommitterEmail(),
startConnect: false,
connectOptions: {
noSslBumpDomains: 'all',
Expand All @@ -237,7 +241,7 @@ module.exports = {
mocha: {
reporter: 'html',
// Longer timeout on Travis; fail quickly at local.
timeout: process.env.TRAVIS ? 10000 : 2000,
timeout: isTravisBuild() ? 10000 : 2000,
},
captureConsole: false,
verboseLogging: false,
Expand All @@ -250,7 +254,7 @@ module.exports = {
failOnEmptyTestSuite: false,

// IF YOU CHANGE THIS, DEBUGGING WILL RANDOMLY KILL THE BROWSER
browserDisconnectTolerance: process.env.TRAVIS ? 2 : 0,
browserDisconnectTolerance: isTravisBuild() ? 2 : 0,

// Import our gulp webserver as a Karma server middleware
// So we instantly have all the custom server endpoints available
Expand Down
Loading

0 comments on commit b843500

Please sign in to comment.