Skip to content

Commit

Permalink
Merge pull request #42 from ImmoweltGroup/feature/multi-configuration
Browse files Browse the repository at this point in the history
Feature/multi configuration
  • Loading branch information
c0de1ovr authored Jan 23, 2019
2 parents 650baa5 + 5d8d649 commit c26a02c
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 38 deletions.
26 changes: 17 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ before_install:
- export PATH="$HOME/.yarn/bin:$PATH"
install:
- yarn
script:
- yarn lint
- yarn test
- yarn integTest
after_success:
- yarn release
- yarn global add @immowelt/docker-publish
- docker login -u=$IMMO_DOCKER_USER -p=$IMMO_DOCKER_PASS
- DEBUG=*immowelt* docker-publish --tags=https://api.github.com/repos/ImmoweltGroup/lighthouse-ci/tags --image=immowelt/lighthouse-ci --arg=LIGHTHOUSE_CI_VERSION

jobs:
include:
- stage: tests
name: "linting and testing"
script:
- yarn lint
- yarn test
- yarn integTest
- stage: release
name: "releasing"
if: branch = master
script:
- yarn release
- yarn global add @immowelt/docker-publish
- docker login -u=$IMMO_DOCKER_USER -p=$IMMO_DOCKER_PASS
- DEBUG=*immowelt* docker-publish --tags=https://api.github.com/repos/ImmoweltGroup/lighthouse-ci/tags --image=immowelt/lighthouse-ci --arg=LIGHTHOUSE_CI_VERSION
branches:
except:
- /^v\d+\.\d+\.\d+$/
21 changes: 4 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
FROM node:8-alpine

RUN echo "http://dl-cdn.alpinelinux.org/alpine/v3.7/community" >> /etc/apk/repositories \
&& echo "http://dl-cdn.alpinelinux.org/alpine/v3.7/main" >> /etc/apk/repositories \
&& apk --no-cache update \
&& apk add --no-cache chromium xvfb bash grep \
&& rm -rf /var/cache/apk/* /tmp/*

ENV CHROME_BIN /usr/bin/chromium-browser
ENV CHROME_PATH /usr/bin/chromium-browser

COPY bin/chromium-xvfb.sh /usr/bin/chromium-xvfb
RUN chmod +x /usr/bin/chromium-xvfb \
&& mv /usr/bin/chromium-browser /usr/bin/chromium \
&& ln -s /usr/bin/chromium-xvfb /usr/bin/chromium-browser
FROM romischart/node-8-chrome

USER root

#
# Install the Marathon CLI globally.
# Install the lighthouse ci globally.
#
ARG LIGHTHOUSE_CI_VERSION="latest"
RUN yarn global add @immowelt/lighthouse-ci@"$LIGHTHOUSE_CI_VERSION" \
&& mkdir -p /usr/src/app/lighthouse
&& mkdir -p /usr/src/app/lighthouse-ci

WORKDIR /usr/src/app/lighthouse-ci

Expand Down
27 changes: 27 additions & 0 deletions Dockerfile.headless
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM node:8-alpine

RUN echo "http://dl-cdn.alpinelinux.org/alpine/v3.7/community" >> /etc/apk/repositories \
&& echo "http://dl-cdn.alpinelinux.org/alpine/v3.7/main" >> /etc/apk/repositories \
&& apk --no-cache update \
&& apk add --no-cache chromium xvfb bash grep \
&& rm -rf /var/cache/apk/* /tmp/*

ENV CHROME_BIN /usr/bin/chromium-browser
ENV CHROME_PATH /usr/bin/chromium-browser

COPY bin/chromium-xvfb.sh /usr/bin/chromium-xvfb
RUN chmod +x /usr/bin/chromium-xvfb \
&& mv /usr/bin/chromium-browser /usr/bin/chromium \
&& ln -s /usr/bin/chromium-xvfb /usr/bin/chromium-browser


#
# Install the lighthouse ci globally.
#
ARG LIGHTHOUSE_CI_VERSION="latest"
RUN yarn global add @immowelt/lighthouse-ci@"$LIGHTHOUSE_CI_VERSION" \
&& mkdir -p /usr/src/app/lighthouse-ci

WORKDIR /usr/src/app/lighthouse-ci

ENTRYPOINT ["lighthouse-ci"]
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Also you are able to pass options down to lighthouse cli and chrome runner which
Example:
```
{
"chromeFlags": ["--show-paint-rects"],
"chromeFlags": ["--headless"],
"options": {
"disableDeviceEmulation": true,
"throttling": {
Expand All @@ -52,6 +52,14 @@ Example:
}
```

## Custom configuration file

You can also pass your own custom configuration file to the cli with the `-c` or `--config` option:

```sh
lighthouse-ci https://immowelt.de/ --config custom-config.json
```

# Docker

We dockerized this package for a better usability in CI pipelines, you can use it locally like this:
Expand Down
28 changes: 23 additions & 5 deletions bin/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ const flatten = require('lodash.flattendeep')
const pick = require('lodash.pick')
const lighthouse = require('lighthouse')
const chromeLauncher = require('chrome-launcher')
const { getConfig } = require('../src/config')

// Prepare CLI
// eslint-disable--next-line
const yargs = require('yargs')
// Always English
.detectLocale(false)
.usage('$0 [<urls>...]', 'Run lighthouse to the given urls')
.option('config', {
description: 'configuration file',
type: 'string',
alias: 'c'
})
.option('report', {
description: 'Generate a (html) report',
type: 'boolean',
Expand Down Expand Up @@ -60,20 +66,32 @@ if (yargs.quiet) {
const { info, success, warn, error } = signale
const { persist } = require('../src/report')
const { accumulate, evaluate } = require('../src/result')
const { getConfig } = require('../src/config')

const chromeDefaultFlags = [
'--disable-background-networking',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-extensions',
'--disable-gpu',
'--disable-setuid-sandbox',
'--disable-translate',
'--no-first-run',
'--no-sandbox',
'--safebrowsing-disable-auto-update'
]

const {
options: optionsFromConfig,
options: optionsFromConfig = {},
thresholds: thresholdsFromConfig,
chromeFlags: chromeFlagsFromConfig
} = getConfig()
chromeFlags: chromeFlagsFromConfig = []
} = getConfig(yargs.config)

if (chromeFlagsFromConfig && chromeFlagsFromConfig.length && chromeFlagsFromConfig.length > 0) {
info('Chrome flags used from configuration: %s', chromeFlagsFromConfig)
}

const launchChromeAndRunLighthouse = url => Promise
.resolve(chromeLauncher.launch({ chromeFlags: ['--headless', '--disable-gpu', '--no-sandbox', ...chromeFlagsFromConfig] }))
.resolve(chromeLauncher.launch({ chromeFlags: [...chromeDefaultFlags, ...chromeFlagsFromConfig] }))
.then(chrome => {
info('Chrome running on port %i {%s}', chrome.port, url)
const opts = {
Expand Down
16 changes: 16 additions & 0 deletions integTest/ci-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"chromeFlags": ["--headless"],
"options": {
"disableDeviceEmulation": false,
"throttling": {
"cpuSlowdownMultiplier": 1
}
},
"thresholds": {
"performance": 0,
"pwa": 0,
"best-practices": 0,
"accessibility": 0,
"seo": 0
}
}
5 changes: 4 additions & 1 deletion integTest/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ set -e

cd integTest

# Integration Test for report generation
# Integration Test for report generation without default configuration loading
node ./../bin/ci.js https://immowelt.de -r > output.txt
node ./validate.js

# Integration Test for report generation without custom configuration loading
node ./../bin/ci.js https://immowelt.de -r > output.txt --config ci-config.json
node ./validate.js

cd -
10 changes: 5 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const { resolve } = require('path')
const { statSync, readFileSync } = require('fs')
const { info, warn } = require('signale')

const filePathAndName = resolve(process.cwd(), CONFIG_DESCRIPTOR)

const configExists = () => {
const configExists = (filePathAndName) => {
try {
statSync(filePathAndName)
} catch (e) {
Expand All @@ -16,8 +14,10 @@ const configExists = () => {
return true
}

const getConfig = () => {
if (!configExists()) { return {} }
const getConfig = (configPath = CONFIG_DESCRIPTOR) => {
const filePathAndName = resolve(process.cwd(), configPath)

if (!configExists(filePathAndName)) { return {} }
try {
const config = JSON.parse(readFileSync(filePathAndName, 'utf8'))
evaluateConfiguration(config)
Expand Down

0 comments on commit c26a02c

Please sign in to comment.