From a9175de37135290174cb4b3dbfd03f11743004f0 Mon Sep 17 00:00:00 2001 From: Jake Jarvis Date: Tue, 17 Sep 2019 11:25:23 -0400 Subject: [PATCH] Clean up entrypoint (#whatwouldshellcheckdo) --- entrypoint.sh | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index e12816a..a0e3b01 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,24 +2,31 @@ set -e -mkdir report +# Check if we're being triggered by a pull request +PULL_REQUEST_NUMBER=$(jq .number "$GITHUB_EVENT_PATH") -# In case of push event this might be empty -PULL_REQUEST_NUMBER=$(jq .number ${GITHUB_EVENT_PATH}) - -if [ -n "${INPUT_NETLIFY_SITE}" ] && [ -n "${PULL_REQUEST_NUMBER}" ] && [ "${PULL_REQUEST_NUMBER}" != "null" ] -then - # PR + Netlify enabled: generate Netlify deploy preview URL +# If this is a PR and Netlify is configured, plan to check the deploy preview and generate its unique URL. +# Otherwise, simply check the plain URL. +if [ -n "$INPUT_NETLIFY_SITE" ] && [ -n "$PULL_REQUEST_NUMBER" ] && [ "$PULL_REQUEST_NUMBER" != "null" ]; then REPORT_URL="https://deploy-preview-${PULL_REQUEST_NUMBER}--${INPUT_NETLIFY_SITE}" - echo "Running against Netlify Deploy Preview ${REPORT_URL}" else - # Fallback to default UNPUT_URL REPORT_URL=${INPUT_URL} fi -lighthouse --port=9222 --chrome-flags="--headless --disable-gpu --no-sandbox --no-zygote" --output "html" --output "json" --output-path "report/lighthouse" ${REPORT_URL} +# Prepare directory for audit results. +# TODO: Name files after the tested URL to allow multiple tests. +# Right now, a second test would overwrite the first results. +OUTPUT_FOLDER="report" +OUTPUT_PATH="$OUTPUT_FOLDER/lighthouse" +mkdir $OUTPUT_FOLDER + +# Clarify in logs which URL we're auditing. +printf "* Beginning audit of %s ...\n\n" "$REPORT_URL" + +# Run Lighthouse! +lighthouse --port=9222 --chrome-flags="--headless --disable-gpu --no-sandbox --no-zygote" --output "html" --output "json" --output-path "${OUTPUT_PATH}" "${REPORT_URL}" -# Parse individual scores from JSON output +# Parse individual scores from JSON output. # Unorthodox jq syntax because of dashes -- https://github.com/stedolan/jq/issues/38 SCORE_PERFORMANCE=$(jq '.categories["performance"].score' ./report/lighthouse.report.json) SCORE_ACCESSIBILITY=$(jq '.categories["accessibility"].score' ./report/lighthouse.report.json) @@ -27,14 +34,16 @@ SCORE_PRACTICES=$(jq '.categories["best-practices"].score' ./report/lighthouse.r SCORE_SEO=$(jq '.categories["seo"].score' ./report/lighthouse.report.json) SCORE_PWA=$(jq '.categories["pwa"].score' ./report/lighthouse.report.json) -# Print scores to standard output (out of 100 instead of 0 to 1) -# TO DO: Don't use hacky bc b/c bash hates floating point arithmetic -printf "%s\n" "----------------------------------" -printf "| Performance: %.0f\t|\n" $(echo "$SCORE_PERFORMANCE*100" | bc -l) -printf "| Accessibility: %.0f\t|\n" $(echo "$SCORE_ACCESSIBILITY*100" | bc -l) -printf "| Best Practices: %.0f\t|\n" $(echo "$SCORE_PRACTICES*100" | bc -l) -printf "| SEO: %.0f\t|\n" $(echo "$SCORE_SEO*100" | bc -l) -printf "| Progressive Web App: %.0f\t|\n" $(echo "$SCORE_PWA*100" | bc -l) -printf "%s\n" "----------------------------------" +# Print scores to standard output (0 to 100 instead of 0 to 1). +# Using hacky bc b/c bash hates floating point arithmetic... +printf "\n* Completed audit of %s !" "$REPORT_URL" +printf "\n* Scores are printed below and detailed results are saved in ./report\n\n" +printf "%s\n" "+-------------------------------+" +printf "| Performance: %.0f\t|\n" "$(echo "$SCORE_PERFORMANCE*100" | bc -l)" +printf "| Accessibility: %.0f\t|\n" "$(echo "$SCORE_ACCESSIBILITY*100" | bc -l)" +printf "| Best Practices: %.0f\t|\n" "$(echo "$SCORE_PRACTICES*100" | bc -l)" +printf "| SEO: %.0f\t|\n" "$(echo "$SCORE_SEO*100" | bc -l)" +printf "| Progressive Web App: %.0f\t|\n" "$(echo "$SCORE_PWA*100" | bc -l)" +printf "%s\n" "+-------------------------------+" exit 0