Skip to content

Commit

Permalink
Travis Lighthouse setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel committed Apr 1, 2017
1 parent 2f3c1d3 commit 73c70a7
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@
.sass-cache
*oauth2.data
node_modules
travis/node_modules
*bower_components
*.vulcanize.html
*.vulcanize.js
Expand Down
27 changes: 27 additions & 0 deletions .travis.yml
@@ -0,0 +1,27 @@
language: node_js
node_js: '7'
dist: trusty
sudo: false
git:
depth: 1
env:
global:
- CLOUDSDK_CORE_DISABLE_PROMPTS=1
- CLOUDSDK_PYTHON_SITEPACKAGES=1
- GAE_APP_ID=cr-status
cache:
directories:
- node_modules
- travis/node_modules
install:
- npm install -g bower gulp
- npm install
- npm run install-travis
script:
- npm run lint
- npm run build
after_success:
- "./travis/deploy_pr_gae.sh"
- export LH_MIN_PASS_SCORE=96
- export LH_TEST_URL=https://lighthouse-ci-staging-dot-cr-status.appspot.com/features
- node travis/runLighthouse.js $LH_TEST_URL $LH_MIN_PASS_SCORE
Binary file added gcloud-client-secret.json.enc
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -7,7 +7,8 @@
"node": ">=6.0.0"
},
"scripts": {
"postinstall": "npm run deps; npm run build",
"postinstall": "npm run deps && npm run build",
"install-travis": "npm --prefix ./travis install ./travis",
"deps": "rm -rf static/bower_components; bower install",
"lint": "gulp lint",
"build": "gulp",
Expand Down
32 changes: 32 additions & 0 deletions travis/deploy_pr_gae.sh
@@ -0,0 +1,32 @@
#!/bin/bash
set -e

# Auto-Deploy Pull Request

# If this isn't a pull request, abort.
if [ "${TRAVIS_EVENT_TYPE}" != "pull_request" ]; then
echo "This only runs on pull_request events. Event was $TRAVIS_EVENT_TYPE."
exit
fi

# If there were build failures, abort
if [ "${TRAVIS_TEST_RESULT}" = "1" ]; then
echo "Deploy aborted, there were build/test failures."
exit
fi

./travis/install_google_cloud_sdk.sh

# Set the AppEngine version for staging
# VERSION=pr-$TRAVIS_PULL_REQUEST
VERSION=lighthouse-ci-staging

# Determine staging URL based on PR.
export LH_TEST_URL=https://$VERSION-dot-$GAE_APP_ID.appspot.com/features
echo "Pull Request: $TRAVIS_PULL_REQUEST will be staged at $LH_TEST_URL"

# Deploy to AppEngine
$HOME/google-cloud-sdk/bin/gcloud app deploy app.yaml -q --no-promote --version $VERSION

# Make sure an AppEngine instance has started.
curl $LH_TEST_URL
21 changes: 21 additions & 0 deletions travis/install_google_cloud_sdk.sh
@@ -0,0 +1,21 @@
#!/bin/bash
set -e

# Installs the Google Cloud SDK

# Decrypt the Service Account Key
openssl aes-256-cbc -K $encrypted_aee7e38c959c_key -iv $encrypted_aee7e38c959c_iv \
-in gcloud-client-secret.json.enc -out gcloud-client-secret.json -d

# Download & install the Google Cloud SDK
curl https://sdk.cloud.google.com | bash

# Update any necessary components
$HOME/google-cloud-sdk/bin/gcloud components update -q

# Set the AppEngine App ID to $GAE_APP_ID
$HOME/google-cloud-sdk/bin/gcloud config set project $GAE_APP_ID

# Authenticate to AppEngine using the service account
$HOME/google-cloud-sdk/bin/gcloud auth activate-service-account \
--key-file gcloud-client-secret.json
11 changes: 11 additions & 0 deletions travis/package.json
@@ -0,0 +1,11 @@
{
"name": "chrome-status-ci",
"version": "0.0.1",
"main": "index.js",
"license": "Apache-2.0",
"dependencies": {
"chalk": "^1.1.3",
"github": "^9.2.0",
"node-fetch": "^1.6.3"
}
}
95 changes: 95 additions & 0 deletions travis/runLighthouse.js
@@ -0,0 +1,95 @@
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

const chalk = require('chalk');
const fetch = require('node-fetch'); // polyfill

const args = process.argv.slice(2);
const LH_TEST_URL = args[0];
const LH_MIN_PASS_SCORE = args[1];
const PR_NUM = process.env.TRAVIS_PULL_REQUEST;
const PR_SHA = process.env.TRAVIS_PULL_REQUEST_SHA;
const REPO_SLUG = process.env.TRAVIS_PULL_REQUEST_SLUG;

const CI_HOST = 'https://lighthouse-ci.appspot.com';
const API_KEY = process.env.API_KEY;
const RUNNERS = {chrome: 'chrome', wpt: 'wpt'};

/**
* @param {!string} runner Where to run Lighthouse.
*/
function run(runner) {
const data = {
testUrl: LH_TEST_URL,
minPassScore: Number(LH_MIN_PASS_SCORE),
repo: {
owner: REPO_SLUG.split('/')[0],
name: REPO_SLUG.split('/')[1]
},
pr: {
number: parseInt(PR_NUM, 10),
sha: PR_SHA
}
};

let endpoint;
let body = JSON.stringify(data);

switch (runner) {
case RUNNERS.wpt:
endpoint = `${CI_HOST}/run_on_wpt`;
break;
case RUNNERS.chrome: // same as default
default:
endpoint = `${CI_HOST}/run_on_chrome`;
body = JSON.stringify(Object.assign({format: 'json'}, data));
}

fetch(endpoint, {
method: 'POST',
body,
headers: {
'Content-Type': 'application/json',
'X-API-KEY': API_KEY // Keep usage tight for now.
}
})
.then(resp => resp.json())
.then(json => {
if (runner === RUNNERS.wpt) {
console.log(chalk.green(
`Started Lighthouse run on WebPageTest: ${json.data.target_url}`));
return;
}

let colorize = chalk.green;
if (json.score < LH_MIN_PASS_SCORE) {
colorize = chalk.red;
}
console.log(colorize('Lighthouse CI score:'), json.score);
})
.catch(err => {
console.log(chalk.red('Lighthouse CI failed'), err);
process.exit(1);
});
}

// Run LH if this is a PR.
if (process.env.TRAVIS_EVENT_TYPE === 'pull_request') {
run(RUNNERS.wpt);
} else {
console.log('Lighthouse not run for non-PR commits');
}

0 comments on commit 73c70a7

Please sign in to comment.