This repo contains the frontend and backend for the Lighthouse CI server.
Please note: This drop in service is considered Beta. There are no SLAs or uptime guarantees. If you're interested in running your own CI server in a Docker container, check out Running your own CI server.
Lighthouse can be setup as part of your CI on Travis. As new pull requests come in, the Lighthouse CI tests the changes and reports back the new score.
To audit pull requests, do the following:
First, add lighthousebot as a collaborator on your repo. Lighthouse CI uses an OAuth token scoped to the repo
permission in order to update the status of your PRs and post comments on the issue as the little Lighthouse icon.
Request an API Key. API keys will eventually be enforced and are necessary so we can contact you when there are changes to the CI system.
Once you have a key, update Travis settings by adding an LIGHTHOUSE_API_KEY
environment variables with your key:
The lighthouse-ci
script will include your key in requests made to the CI server.
We recommend deploying your PR to a real staging server instead of running a local server on Travis. A staging environment will produce realistic performance numbers that are more representative of your production setup. The Lighthouse report will be more accurate.
In .travis.yml
, add an after_success
that deploys the PR's changes to a staging server.
after_success:
- ./deploy.sh # TODO(you): deploy the PR changes to your staging server.
Since every hosting environment has different deployment setups, the implementation of deploy.sh
is left to the reader.
Tip: Using Google App Engine? Check out
deploy_pr_gae.sh
which shows how to install the GAE SDK and deploy PR changes programmatically.
Install the script:
npm i --save-dev https://github.com/ebidel/lighthouse-ci
Add an NPM script to your package.json
:
"scripts": {
"lh": "lighthouse-ci"
}
Next, in .travis.yml
call npm run lh
as the last step in after_success
:
install:
- npm install # make sure to install the deps when Travis runs.
after_success:
- ./deploy.sh # TODO(you): deploy the PR changes to your staging server.
- npm run lh https://staging.example.com
When Lighthouse is done auditing the URL, the CI will post a comment to the pull request containing the updated scores:
You can also opt-out of the comment by using the --no-comment
flag.
Lighthouse CI can prevent PRs from being merged when the overall score falls below
a specified value. Just include the --score
flag:
after_success:
- ./deploy.sh # TODO(you): deploy the PR changes to your staging server.
- npm run lh --score=96 https://staging.example.com
$ lighthouse-ci -h
Usage:
lighthouse-ci [--score=<score>] [--no-comment] [--runner=chrome,wpt] <url>
Options:
--score Minimum score for the pull request to be considered "passing".
If omitted, merging the PR will be allowed no matter what the score. [Number]
--no-comment Doesn't post a comment to the PR issue summarizing the Lighthouse results. [Boolean]
--runner Selects Lighthouse running on Chrome or WebPageTest. [--runner=chrome,wpt]
--help Prints help.
Examples:
Runs Lighthouse and posts a summary of the results.
lighthouse-ci https://example.com
Fails the PR if the score drops below 93. Posts the summary comment.
lighthouse-ci --score=93 https://example.com
Runs Lighthouse on WebPageTest. Fails the PR if the score drops below 93.
lighthouse-ci --score=93 --runner=wpt --no-comment https://example.com
By default, lighthouse-ci
runs your PRs through Lighthouse hosted in the cloud. As an alternative, you can test on real devices using the WebPageTest integration:
lighthouse-ci --score=96 --runner=wpt https://staging.example.com
At the end of testing, your PR will be updated with a link to the WebPageTest results containing the Lighthouse report!
This repo contains several different pieces for the Lighthouse CI: a backend, frontend, and frontend UI.
Quick way to try Lighthouse: https://lighthouse-ci.appspot.com/try
Relevant source:
frontend/public/
- UI for https://lighthouse-ci.appspot.com/try.
Server that responds to requests from Travis.
REST endpoints:
https://lighthouse-ci.appspot.com/run_on_chrome
https://lighthouse-ci.appspot.com/run_on_wpt
Note: lighthouse-ci
does this for you.
POST https://lighthouse-ci.appspot.com/run_on_chrome
Content-Type: application/json
X-API-KEY: <YOUR_LIGHTHOUSE_API_KEY>
{
testUrl: "https://staging.example.com",
minPassScore: 96,
addComment: true,
repo: {
owner: "<REPO_OWNER>",
name: "<REPO_NAME>"
},
pr: {
number: <PR_NUMBER>,
sha: "<PR_SHA>"
}
}
Relevant source:
frontend/server.js
- server which accepts Github pull requests and updates the status of your PR.
Server that runs Lighthouse against a URL, using Chrome.
REST endpoints:
https://lighthouse-ci.appspot.com/ci
Note: lighthouse-ci
does this for you.
curl -X POST \
-H "Content-Type: application/json" \
-H "X-API-KEY: <YOUR_LIGHTHOUSE_API_KEY>" \
--data '{"output": "json", "url": "https://staging.example.com"}' \
https://builder-dot-lighthouse-ci.appspot.com/ci
Want to setup your own Lighthouse instance in a Docker container?
The good news is Docker does most of the work for us! The bulk of getting started is in Development. That will take you through initial setup and show how to run the CI frontend.
For the backend, see builder/README.md for building and running the Docker container.
Other changes, to the "Development" section:
- Create a personal OAuth token in https://github.com/settings/tokens. Drop it in
frontend/.oauth_token
. - Add a
CI_HOST
env variable to Travis settings that points to your own URL. The one where you deploy the Docker container.
Initial setup:
- Ask an existing dev for the oauth2 token. If you need to regenerate one, see below.
- Create
frontend/.oauth_token
and copy in the token value.
Run the dev server:
cd frontend
npm run start
This will start a web server and use the token in .oauth_token
. The token is used to update PR status in Github.
In your test repo:
- Run
npm i --save-dev https://github.com/ebidel/lighthouse-ci
- Follow the steps in Auditing Github Pull Requests for setting up your repo.
Notes:
- If you want to make changes to the builder, you'll need Docker and the GAE Node SDK.
- To make changes to the CI server, you'll probably want to run ngrok so you can test against a local server instead of deploying for each change. In Travis settings,
add a
CI_HOST
env variable that points to your ngrok instance.
If you need to generate a new OAuth token:
- Sign in to the lighthousebot Github account. (Admins: the credentials are in the usual password tool).
- Visit personal access tokens: https://github.com/settings/tokens.
- Regenerate the token. Important: this invalidates the existing token so other developers will need to be informed.
- Update token in
frontend/.oauth_token
.
By default, these scripts deploy to Google App Engine Flexible containers (Node). If you're running your own CI server, use your own setup :)
Deploy the frontend:
./deploy.sh YYYY-MM-DD frontend
Deploy the CI builder backend:
./deploy.sh YYYY-MM-DD builder
Github's Deployment API would be ideal, but it has some downsides:
- Github Deployments happen after a pull is merged. We want to support blocking PR merges based on a LH score.
- We want to be able to audit changes as they're add to the PR.
pull_request
/push
events are more appropriate for that.
The main downside of a Github webhook is that there's no way to include custom data in the payload Github sends to the webhook handler. For example, how would Lighthouse know what url to test? With a webhook, the user also has to setup it up and configure it properly.
Future work: Lighthouse CI could define a file that developer includes in their
repo. The CI endpoint could pull a .lighthouse_ci
file that includes meta
data {minLighthouseScore: 96, testUrl: 'https://staging.example.com'}
. However,
this requires work from the developer.
- cd builder
docker login
docker build .
(build from this (.) directory). This will output a hash at the end of the output.docker tag <the hash> esridc/lighthouse:<version>
docker tag <the hash> esridc/lighthouse:latest
This pushes it to our repo in org in dockerhub, our repo (lighthouse), tagging it with the version number and 'latest'docker push esridc/lighthouse:<version>
docker push esridc/lighthouse:latest