Coder Social home page Coder Social logo

lighthouse-ci's Introduction

Lighthouse CI

This repo contains the frontend and backend for the Lighthouse CI server.

Auditing Github Pull Requests

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.

Run Lighthouse on Github PRs

To audit pull requests, do the following:

1. Initial setup

Add the lighthousebot to your repo

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.

Get an API Key

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:

Travis LIGHTHOUSE_API_KEY env variable

The runlighthouse.js script will include your key in requests made to the CI server.

2. Deploy the PR

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.

3. Call runlighthouse.js

Install the script:

yarn add --dev https://github.com/ebidel/lighthouse-ci

Next, in .travis.yml call runlighthouse.js 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.
  - node node_modules/lighthouse-ci/runlighthouse.js 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:

Lighthouse Github comment

You can also opt-out of the comment by using the --no-comment flag.

Failing a PR when it drops your Lighthouse score

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.
  - node node_modules/lighthouse-ci/runlighthouse.js --score=96 https://staging.example.com

Options

$ node runlighthouse.js -h

Usage:
runlighthouse.js [--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.
    runlighthouse.js https://example.com

  Fails the PR if the score drops below 93. Posts the summary comment.
    runlighthouse.js --score=93 https://example.com

  Runs Lighthouse on WebPageTest. Fails the PR if the score drops below 93.
    runlighthouse.js --score=93 --runner=wpt --no-comment https://example.com

Running on WebPageTest instead of Chrome

By default, runlighthouse.js runs your PRs through Lighthouse hosted in the cloud. As an alternative, you can test on real devices using the WebPageTest integration:

node node_modules/lighthouse-ci/runlighthouse.js --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!

Source & Components

This repo contains several different pieces for the Lighthouse CI: a backend, frontend, and frontend UI.

UI Frontend

Quick way to try Lighthouse: https://lighthouse-ci.appspot.com/try

Relevant source:

CI server (frontend)

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

Example

Note: runlighthouse.js 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.

CI backend (builder)

Server that runs Lighthouse against a URL, using Chrome.

REST endpoints:

  • https://lighthouse-ci.appspot.com/ci

Example

Note: runlighthouse.js does this for you.

curl -X POST \
  -H "Content-Type: application/json" \
  -H "X-API-KEY: <YOUR_LIGHTHOUSE_API_KEY>" \
  --data '{"format": "json", "url": "https://staging.example.com"}' \
  https://builder-dot-lighthouse-ci.appspot.com/ci

Relevant source:

Contains example Dockerfiles for running Lighthouse using Headless Chrome and full Chrome. Both setups us Google App Engine Flexible containers (Node).

Running your own CI server

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.

Development

Initial setup:

  1. 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
yarn 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:

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.
Generating a new OAuth2 token

If you need to generate a new OAuth token:

  1. Sign in to the `lighthousebot Github account. (Admins: the credentials are in the usual password tool).
  2. Visit personal access tokens: https://github.com/settings/tokens.
  3. Regenerate the token. Important: this invalidates the existing token so other developers will need to be informed.
  4. Update token in frontend/.oauth_token.

Deploy

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

FAQ

Why not deployment events?

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.
Why not a Github Webhook?

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 Ligthhouse 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.

lighthouse-ci's People

Contributors

brendankenny avatar ebidel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.