Add lighthouse to build workflow #550
Workflow file for this run
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
name: build preview | |
on: | |
pull_request: | |
branches: | |
- "*" | |
env: | |
REPO_OWNER: "nginx" | |
REPO_NAME: "documentation" | |
FRONT_DOOR_USERNAME: ${{ secrets.FRONT_DOOR_USERNAME }} | |
FRONT_DOOR_PASSWORD: ${{ secrets.FRONT_DOOR_PASSWORD }} | |
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }} | |
MAIN_REPORT_ARTIFACT_NAME: "lighthouse-reports-main" | |
jobs: | |
deploy-example-site: | |
uses: nginxinc/docs-actions/.github/workflows/docs-build-push.yml@04ed2db338ee08cc560a327f412684d0c8260de2 # v1.0.11 | |
with: | |
production_url_path: "/nginx-hugo-theme" | |
preview_url_path: "/previews/nginx-hugo-theme" | |
docs_source_path: "public" | |
docs_build_path: "./exampleSite" | |
doc_type: "hugo" | |
environment: "preview" | |
force_hugo_theme_version: "" | |
auto_deploy_branch: "main" | |
auto_deploy_env: "prod" | |
secrets: | |
AZURE_CREDENTIALS: ${{secrets.AZURE_CREDENTIALS_DOCS}} | |
AZURE_KEY_VAULT: ${{secrets.AZURE_KEY_VAULT_DOCS}} | |
lighthouseci: | |
if: github.event.pull_request | |
needs: deploy-example-site | |
runs-on: ubuntu-22.04 | |
steps: | |
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 | |
with: | |
ref: ${{ github.event.workflow_run.head_branch }} | |
- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 | |
with: | |
node-version: 22 | |
- name: Installing packages | |
run: cd performance && npm ci | |
- name: Generating lighthouse reports for PR... | |
env: | |
REPORT_NAME: "pr" | |
run: | | |
node performance/lighthouse-script.js | |
- name: Retrieve the latest artifact ID for lighthouse report main | |
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
id: fetch-artifact-id | |
with: | |
script: | | |
const { owner, repo } = context.issue; | |
const response = github.rest.actions.listArtifactsForRepo({ | |
owner, | |
repo, | |
}); | |
const artifactName = process.env.MAIN_REPORT_ARTIFACT_NAME; | |
const filteredArtifacts = artifacts.data.artifacts.filter(a => a.name === artifactName); | |
if(filteredArtifacts.length === 0) { | |
core.setFailed(`Error: Not able to find artifact with name ${artifactName}`); | |
} | |
else { | |
const latestArtifact = filteredArtifacts.sort((a, b) => new Date(b.created_at) - new Date(a.created_at))[0]; | |
console.log(`Success! Found the latest artifact with name #{artifactName}`); | |
core.setOutput("artifact_id", latestArtifact.id); | |
} | |
result-encoding: string | |
- name: Download the artifact for main | |
uses: actions/download-artifact@f44cd7b40bfd40b6aa1cc1b9b5b7bf03d3c67110 # v4.1.0 | |
with: | |
artifact-ids: ${{ steps.fetch_artifact.outputs.artifact_id }} | |
path: ./main-report | |
- name: Move the main report artifact to same directory as pr report | |
run: | | |
mv ./main-report/main-report.json ./lighthouse-reports | |
- name: Compare the artifacts for negative differences in performance | |
continue-on-error: true | |
run: | | |
FIELDS=("performance" "accessibility") | |
TOLERANCE=0.05 | |
FLOOR_VALUE=90 | |
for FIELD in "${FIELDS[@]}"; do | |
PR_VALUE=$(cat lighthouse-reports/pr-report.json | jq -r ".categories.$FIELD.score") | |
MAIN_VALUE=$(cat lighthouse-reports/main-report.json | jq -r ".categories.$FIELD.score") | |
echo "$FIELD: PR - $PR_VALUE | Main - $MAIN_VALUE" | |
if (( $(echo "$PR_VALUE < $FLOOR_VALUE" | bc -l) )); then | |
echo "Error: $FIELD score in PR ($PR_VALUE) is less than accepted value ($FLOOR_VALUE)" | |
exit 1 | |
fi | |
if [ $FIELD = "performance" ]; then | |
LOWER_BOUND=$(echo "$MAIN_VALUE - $TOLERANCE" | bc) | |
UPPER_BOUND=$(echo "$MAIN_VALUE + $TOLERANCE" | bc) | |
if (( $(echo "$PR_VALUE < $LOWER_BOUND" | bc -l) || $(echo "$PR_VALUE > $UPPER_BOUND" | bc -l) )); then | |
echo "Error: $FIELD score in PR ($PR_VALUE) is less than in MAIN ($MAIN_VALUE)" | |
exit 1 | |
fi | |
else | |
if (( $(echo "$PR_VALUE < $MAIN_VALUE" | bc -l) )); then | |
echo "Error: $FIELD score in PR ($PR_VALUE) is less than in MAIN ($MAIN_VALUE)" | |
exit 1 | |
fi | |
fi | |
done | |
- uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # v4.5.0 | |
if: ${{ !cancelled() }} | |
with: | |
name: lighthouse-reports-pr | |
path: lighthouse-reports/pr-report.json | |
retention-days: 3 |