GoogleChromeLabs / lighthousebot

Run Lighthouse in CI, as a web service, using Docker. Pass/Fail GH pull requests.
Apache License 2.0
2.24k stars 127 forks source link

Changes for docker-compose and gitlab-ci #5

Open kmturley opened 7 years ago

kmturley commented 7 years ago

These changes allowed me to get your lighthouse-ci image working locally and on gitlab-ci using these files in my own project:

Docker Compose

docker-compose.yml

version: "2"
services:

  backend:
    build: ./backend
    command: python server.py
    environment:
      NODE_ENV: development
    networks:
      - backend
    ports:
      - '3000:3000'
    volumes:
      - ./backend:/usr/src/app:ro

  lighthouse:
    image: kmturley/lighthouse-ci
    networks:
      - backend
    ports:
      - "8085:8085"

networks:
  backend:
    driver: bridge

qa/page_runner.py

import os, requests, json, re
BASE_URL = os.getenv('BASE_URL', 'https://localhost:3000')
LIGHTHOUSE_IMAGE = os.getenv('LIGHTHOUSE_IMAGE', 'http://localhost:8085')
headers = {
  'Accept-Charset': 'UTF-8',
  'Content-Type': 'application/json',
  'X-API-KEY': '<YOUR_API_KEY>'
}

# r = requests.post(LIGHTHOUSE_IMAGE + '/ci', data='{"format": "json", "url": "' + BASE_URL + '"}', headers=headers)
r = requests.get(LIGHTHOUSE_IMAGE + '/stream?format=json&url=' + BASE_URL, headers=headers)

urls = re.findall(
    'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', r.text)
print (LIGHTHOUSE_IMAGE + urls[0].split('8085')[1])

req = requests.get(LIGHTHOUSE_IMAGE + urls[0].split('8085')[1], headers=headers)
data = req.json()
directory = 'qa/output';
if not os.path.exists(directory):
    os.makedirs(directory)
with open(directory + 'index.report.json', 'w') as f:
    json.dump(data, f)
print ('copied to: ' + directory + 'index.report.json')

Gitlab CI

.gitlab-ci.yml

test_accessibility:
  image: python
  stage: test
  services:
  - name: kmturley/lighthouse-ci
  script:
    - pip install -r requirements.txt
    - BASE_URL="https://$CI_BUILD_REF_SLUG-dot-$GAE_PROJECT.appspot.com" LIGHTHOUSE_IMAGE="http://kmturley__lighthouse-ci:8085" python qa/page_runner.py

BitBucket CI

bitbucket-pipelines.yml

image: python:2.7

pipelines:
  default:
    - step:
        services:
          - lighthouse
        script:
          - echo 'verify_accessibility' &&
            pip install -r requirements.txt &&
            BASE_URL="http://$BITBUCKET_BRANCH.$BITBUCKET_PROJECT.elasticbeanstalk.com" LIGHTHOUSE_IMAGE="http://127.0.0.1:8085" python qa/page_runner.py

definitions:
  services:
    lighthouse:
      image: kmturley/lighthouse-ci