Last active
September 17, 2018 18:53
-
-
Save mettamatt/18b9febcf4c33aa7cea8b1d757d9e934 to your computer and use it in GitHub Desktop.
Google Lighthouse integration with Tugboat. In order to use this, you'll need store your GitHub personal access token (GITHUB_TOKEN) as an environment variable on Tugboat. Make sure the token has all access to the repos and gists. You'll also need to upload your repos's SSH key from Tugboat to GitHub. https://developers.google.com/web/tools/ligh…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
php: | |
image: tugboatqa/php:7-apache | |
default: true | |
commands: | |
init: | |
# Install Node, Google Lighthouse, and jq | |
- apt-get update | |
- curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - | |
- apt-get install -y nodejs chromium jq | |
- npm install -g lighthouse | |
build: | | |
# Warm the cache | |
"wget -e robots=off --quiet --page-requisites --delete-after --header \"Host: tugboat.qa\" http://localhost || /bin/true" | |
# Test for a pull request. | |
if [[ -v TUGBOAT_GITHUB_HEAD ]]; then | |
# Grab the status url from GitHub | |
curl -sSL -X GET -H 'Content-Type: application/json' \ | |
\-u ${TUGBOAT_GITHUB_OWNER}:${GITHUB_TOKEN} \ | |
"https://api.github.com/repos/${TUGBOAT_GITHUB_OWNER}/${TUGBOAT_GITHUB_REPO}/statuses/${TUGBOAT_GITHUB_HEAD}" > status.json | |
STATUS_URL=$(cat status.json | jq -r '.[0] | .url'); | |
# Set the Pull Request status to 'Pending' | |
curl -sSL -X POST -H 'Content-Type: application/json' \ | |
\-u ${TUGBOAT_GITHUB_OWNER}:${GITHUB_TOKEN} \ | |
\-d "{ \"state\": \"pending\", \"target_url\": \"${TUGBOAT_PREVIEW_URL}\", \"description\": \"Building lighthouse report.\", \"context\": \"Tugboat.qa\" }" ${STATUS_URL} > /dev/null | |
fi | |
# Run lighthouse as JSON, pipe it to jq to wrangle and send it to GitHub Gist via curl | |
# so Lighthouse Viewer can grab it. | |
lighthouse http://localhost --quiet --chrome-flags="--no-sandbox --headless" \ | |
\--output json > audit.json | |
# Get the Lighthouse category scores. | |
cat audit.json | jq '.categories | .[] | {category: .title, score: (.score * 100)}' > score.json | |
# Send up a Gist | |
cat audit.json | jq -r "{ description: \"Tugboat:${TUGBOAT_REPO} - ${TUGBOAT_PREVIEW_URL}\", public: \"false\", files: {\"${TUGBOAT_PREVIEW_ID}-$(date "+%Y%m%d").lighthouse.report.json\": {content: (. | tostring) }}}" \ | |
| curl -sSL -X POST -H 'Content-Type: application/json' \ | |
\-u ${TUGBOAT_GITHUB_OWNER}:${GITHUB_TOKEN} \ | |
\-d @- https://api.github.com/gists > results.gist | |
# Let's be nice and add the Lighthouse Viewer link in the Gist description. | |
GID=$(cat results.gist | jq -r '.id') | |
curl -sSL -X POST -H 'Content-Type: application/json' \ | |
\-u ${TUGBOAT_GITHUB_OWNER}:${GITHUB_TOKEN} \ | |
\-d "{ \"description\": \"Tugboat: ${TUGBOAT_REPO} - Preview: ${TUGBOAT_PREVIEW_URL} - Lighthouse: https://googlechrome.github.io/lighthouse/viewer/?gist=${GID}\" }" "https://api.github.com/gists/${GID}" > updated.gist | |
# Check for a pull request again. | |
if [[ -v TUGBOAT_GITHUB_HEAD ]]; then | |
# Leave a comment, posting Lighthouse scores for quick reference | |
PR=$(echo ${TUGBOAT_PREVIEW} | sed 's/pr//') | |
curl -sSL -X POST -H 'Content-Type: application/json' \ | |
\-u ${TUGBOAT_GITHUB_OWNER}:${GITHUB_TOKEN} \ | |
\-d "{ \"body\": \"Lighthouse Report:\n $(cat score.json | jq -r '. | "- \(.category): \(.score)"' | sed -z 's/\n/\\n/g')\nhttps://googlechrome.github.io/lighthouse/viewer/?gist=${GID}\" }" "https://api.github.com/repos/${TUGBOAT_GITHUB_OWNER}/${TUGBOAT_GITHUB_REPO}/issues/${PR}/comments" > comment.json | |
# Set the Pull Request status to 'Success' | |
curl -sSL -X POST -H 'Content-Type: application/json' \ | |
\-u ${TUGBOAT_GITHUB_OWNER}:${GITHUB_TOKEN} \ | |
\-d "{ \"state\": \"success\", \"target_url\": \"https://googlechrome.github.io/lighthouse/viewer/?gist=${GID}\", \"description\": \"Your Lighthouse report is complete.\", \"context\": \"Tugboat.qa\" }" ${STATUS_URL} > /dev/null | |
fi | |
# Print the link to the report in stdout. | |
echo -e "\n*************************************************************\n \ | |
Your Lighthouse report is complete and can be accessed here:\n \ | |
https://googlechrome.github.io/lighthouse/viewer/?gist=${GID}\n\ | |
*************************************************************\n" || /bin/true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment