# How about:
# The lighthouse job will set an output for the number of warnings, but also we have already failed on errors. So if we get to the release job, there are no errors.
# Then, in the release job, we can set:
environment:
name: production
# This environment can have required reviewers, so the job will wait for approval.
# But we only want to require approval if there are warnings. So we can conditionally set the environment? Actually, the environment is set at the job level and we cannot change it conditionally.
# Alternatively, we can have two release jobs: one that runs automatically if no warnings, and one that requires approval if there are warnings? That doesn't make sense.
# Another idea: we can have the release job always require an environment that has required reviewers. But then we require approval even when there are no warnings? That might not be desired.
# Alternatively, we can have a step in the release job that checks the warnings and then fails (or requires approval) based on that.
# But note: the requirement is to have release approvals for the release. So maybe we want to always require approval for the release job? But then the LHCI gate is just to provide information.
# Actually, the requirement says: "enforcing budgets, flake mitigation, and release approvals". So the release approval is part of the gate. We want to block the release if there are errors, and require approval if there are warnings? Or always require approval for release? The problem says "release approvals", so we want a human to approve the release. But we also want to enforce budgets and flake mitigation.
# Let me re-read: "Engineer a Lighthouse CI gate enforcing budgets, flake mitigation, and release approvals."
# So we want:
# - Budgets: fail if exceeded.
# - Flake mitigation: run multiple times and take median.
# - Release approvals: require human approval for release (maybe only when there are performance regressions? or always?)
# The common practice is to have a performance budget and then if the budget is exceeded, fail the CI. For release, we might want to have a separate gate that requires a human to approve the release, but that human should look at the Lighthouse report and decide.
# So maybe:
# On PRs: run LHCI and fail if budget exceeded (errors). Warnings are allowed but reported.
# On release: run LHCI and then always require a human to approve the release (so the release job is in an environment that requires approval). But then we also want to fail if there are errors? So we can have:
# - The LHCI job (on release) will fail if there are errors (so the release won't even be attempted).
# - If there are no errors, then we proceed to the release job which requires manual approval (so a human must approve the release, and they can look at the Lighthouse report that was uploaded).
# That seems reasonable.
# So we don't need to conditionally set the environment based on warnings. We always require approval for the release job, but we have already blocked the release if there are errors.
# But then what about warnings? The human approving the release should be aware of the warnings. We can have the LHCI job post a comment on the release PR (or the release note) with the warnings.
# So:
# lighthouse job:
# - runs on every PR and on release branch (or when creating a release)
# - collects and asserts, fails on errors.
# - uploads the report and also posts a comment with the results (or at least the warnings) so that the reviewer can see.
# release job:
# - runs only on release branch (or when creating a release) and after the lighthouse job (if it succeeded)
# - requires approval (by using an environment with required reviewers)
# But note: the release job might be the one that actually deploys. So we want to deploy only after approval.
# So the workflow:
# on:
# push:
# branches:
# - main
# tags:
# - 'v*'
# pull_request:
# jobs:
# lighthouse:
# runs-on: ubuntu-latest
# steps:
# ... run LHCI and assert, fail on errors.
# ... upload and post comment with results (including warnings).
# release:
# needs: lighthouse
# if: startsWith(github.ref, 'refs/tags/') # only on tags
# environment: production # this environment requires manual approval
# runs-on: ubuntu-latest
# steps:
# ... deploy steps
# But wait: the `needs: lighthouse` will wait for the lighthouse job to succeed. So if there are errors, the lighthouse job fails and the release job won't run.
# And if there are no errors, the release job will run but it will wait for manual approval because of the environment.
# This meets the requirement:
# - Budgets: enforced by assert (errors fail the lighthouse job, which blocks the release).
# - Flake mitigation: by setting `numberOfRuns` to 3 and taking median.
# - Release approvals: by requiring the release job to run in an environment that requires approval.
# But note: the release job will require approval even if there are no warnings. That might be acceptable because we want a human to approve every release.
# However, the requirement says "release approvals" in the context of a Lighthouse CI gate. So it might be that we only require approval if there are warnings? But the above design requires approval for every release (if the lighthouse job passed). That might be too heavy.
# Alternatively, we can have the release job conditionally require approval based on the presence of warnings? But we cannot conditionally set the environment.
# Another idea: we can have the release job always run, but then in the release job we check the warnings and if there are warnings, we fail the job and require a human to re-run? That doesn't make sense.
# Or we can have two release jobs: one that runs automatically if there are no warnings, and one that requires approval if there are warnings? But then how do we decide which one to run?
# We can set an output from the lighthouse job (number of warnings) and then in the release job we conditionally set the environment? Actually, we can conditionally set the `environment` in a job? I don't think so.
# Alternatively, we can have the release job always run, but then in the release job we check the warnings and if there are warnings, we post a comment and then wait for a manual approval by using the `github-script` to create a deployment and then wait for the deployment to be approved? That's complicated.
# Given the complexity, maybe we should always require approval for releases. That is a common practice anyway.
# But if we want to conditionally require approval only when there are warnings, we might need to break the release into two steps:
# Step 1: Run LHCI and then if there are warnings, we create a deployment (or set an environment) that requires approval, and then if there are no warnings, we skip that step and deploy automatically.
# But how to conditionally set the environment? We can't.
# Alternatively, we can have two separate jobs:
# job1: lighthouse (fails on errors, sets output for warnings)
# job2: release-without-approval (if no warnings, runs automatically)
# job3: release-with-approval (if warnings, runs and requires approval)
# But then we have two release jobs? We don't want to deploy twice.
# We can have:
# release:
# needs: lighthouse
# if: needs.lighthouse.outputs.has-warnings == '0'
# runs-on: ubuntu-latest
# steps: ... deploy without approval
# release-approval:
# needs: lighthouse
# if: needs.lighthouse.outputs.has-warnings != '0'
# environment: production # requires approval
# runs-on: ubuntulatest
# steps: ... deploy
# But then we have two jobs that deploy? We don't want that. We want one deployment.
# We can have the deployment step in a separate job that runs conditionally? But then we have two jobs that do the same deployment? That's not good.
# Alternatively, we can have a single release job that conditionally requires approval by using an environment that always requires approval, and then we use the `if` condition to skip the job if there are no warnings? But then we don't get the deployment if there are no warnings? That's not what we want.
# Actually, we want:
# - If there are errors: fail the lighthouse job and no release.
# - If there are no errors and no warnings: release automatically.
# - If there are no errors but there are warnings: release but with manual approval.
# So we can do:
# lighthouse job: runs on every PR and on release tags, fails on errors, sets output `has-warnings` (0 or 1).
# Then, two release jobs:
# release-auto:
# needs: lighthouse
# if: needs.lighthouse.outputs.has-warnings == '0'
# runs-on: ubuntu-latest
# steps: ... deploy
# release-approval:
# needs: lighthouse
# if: needs.lighthouse.outputs.has-warnings != '0'
# environment: production
# runs-on: ubuntu-latest
# steps: ... deploy
# But then we have two jobs that do the same deployment. We can extract the deployment steps into a reusable workflow or a composite action.
# Alternatively, we can have one release job that uses an environment that requires approval, but then we set the environment conditionally? We can't.
# Given the complexity, and since the requirement is to have release approvals (meaning every release requires approval) then we can just set the release job to always require approval. But then we are requiring approval even when there are no warnings, which might be acceptable.
# However, the requirement might be to only require approval when there are warnings (i.e., performance regressions but not exceeding budgets). But the problem statement doesn't specify.
# Let's assume that we want to require approval only when there are warnings (i.e., the performance is below the recommended but still above the error threshold). Then we need the two-job approach.
# But note: the release job might be complex and we don't want to duplicate. So we can do:
# jobs:
# lighthouse: ... (sets output has-warnings)
# release:
# needs: lighthouse
# # We want to run if there are no errors (so lighthouse job succeeded) and then we want to conditionally require approval.
# # We can have one job that uses an environment that requires approval, but then we don't want to require approval if there are no warnings.
# # Alternatively, we can have the release job always run, but then in the release job we have a step that checks the warnings and if there are no warnings, we skip the approval step? But the approval is at the job level, not the step level.
# # So we cannot conditionally require approval at the job level based on an output.
# # Therefore, we must have two separate jobs, each with its own environment? Or one job with an environment that always requires approval, and then we conditionally run the job? But then if there are no warnings, we don't run the job? That would skip the release.
# # Actually, we want:
# # If no warnings: run the release job without requiring approval? But we can't remove the requirement of the environment.
# # So we are stuck.
# Given the constraints of GitHub Actions environments, we cannot conditionally set the environment. So we have to choose:
# Option 1: Always require approval for the release job. Then we don't need to check for warnings in the release job, because the approval is always required.
# Option 2: Have two release jobs, each with the same deployment steps, but one with an environment that requires approval and one without. We conditionally run one of them.
# Since the requirement says "release approvals", it might be acceptable to always require approval for releases. But if we want to only require approval when there are warnings, we have to go with option 2.
# Let's go with option 2 to meet the requirement exactly: only require approval when there are warnings.
# We can avoid duplicating the deployment steps by using a reusable workflow or by having a composite action that does the deployment, and then two jobs that call that composite action.
# Example:
# jobs:
# lighthouse: ... (sets output has-warnings)
# release-auto:
# needs: lighthouse
# if: needs.lighthouse.outputs.has-warnings == '0'
# runs-on: ubuntu-latest
# steps:
# - uses: ./.github/actions/deploy # a composite action that does the deployment
# release-approval:
# needs: lighthouse
# if: needs.lighthouse.outputs.has-warnings != '0'
# environment: production
# runs-on: ubuntu-latest
# steps:
# - uses: ./.github/actions/deploy
# This way, we have one composite action `deploy` that contains the deployment steps.
# But note: the release job might need to run on a specific event (like a tag) and we are already triggering the workflow on tags. So we are good.
# However, what if we want to run the release on a branch? We can adjust the `if` condition of the release jobs to also check the event.
# Let's design the workflow trigger:
# We want the lighthouse job to run on every PR and on every release (tag). The release jobs should only run on release tags.
# So:
# on:
# pull_request:
# push:
# tags:
# - 'v*'
# Then, the lighthouse job runs on both, but the release jobs only run on tags? Actually, the release jobs are conditioned on the output of the lighthouse job and also we can condition on the event? But the release jobs are only defined for the workflow that runs on tags? Actually, the workflow runs on both, so the release jobs will be defined for both, but the `if` condition of the release jobs will only be true when there is a tag? Not necessarily.
# We can add:
# if: startsWith(github.ref, 'refs/tags/') && needs.lighthouse.outputs.has-warnings == '0'
# But then if we are on a PR, the release jobs won't run.
# Alternatively, we can have the release jobs only run on tags by having:
# if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') && ...
# But then if we are on a PR, the release jobs won't be created? Actually, the workflow is triggered on both, so the jobs will be created, but the condition will be false for PRs.
# So:
# jobs:
# lighthouse:
# runs-on: ubuntu-latest
# steps: ... (runs on both PR and tags)
# release-auto:
# needs: lighthouse
# if: |
# github.event_name == 'push' &&
# startsWith(github.ref, 'refs/tags/') &&
# needs.lighthouse.outputs.has-warnings == '0'
# runs-on: ubuntu-latest
# steps: ...
# release-approval:
# needs: lighthouse
# if: |
# github.event_name == 'push' &&
# startsWith(github.ref, 'refs/tags/') &&
# needs.lighthouse.outputs.has-warnings != '0'
# environment: production
# runs-on: ubuntu-latest
# steps: ...
# This way, the release jobs only run on push to a tag.
# But note: the lighthouse job runs on PRs and tags. We don't want to run the release jobs on PRs.
# So the above condition for release jobs includes `github.event_name == 'push'` and `startsWith(github.ref, 'refs/tags/')`, so they only run on tags.
# And the `needs.lighthouse.outputs.has-warnings` is set only if the lighthouse job ran and succeeded? Actually, the lighthouse job runs on both PR and tags, but for PRs the release jobs won't run because of the condition. So the output is set only when the lighthouse job runs? Yes.
# But what if the lighthouse job fails? Then the release jobs won't run because `needs.lighthouse` will be skipped? Actually, if the lighthouse job fails, the release jobs will not run because `needs` condition is not met? Actually, `needs` will be considered as failed? We have to set `needs.lighthouse.outputs.has-warnings` only if the lighthouse job succeeded. But if the lighthouse job fails, the workflow is failed and the release jobs won't run? Not exactly: if the lighthouse job fails, then the release jobs will not run because they depend on it? Actually, by default, if a job fails, the jobs that depend on it are not run. So we are safe.
# However, if we want the release jobs to run only when the lighthouse job succeeded, we don't need to do anything because `needs` will only be successful if the lighthouse job succeeded? Actually, `needs` will be considered successful if the lighthouse job succeeds. If the lighthouse job fails, the workflow fails and the release jobs are not run.
# So we are good.
# Summary:
# We have three jobs:
# - lighthouse: runs on every PR and every tag push. Runs LHCI collect and assert, fails on errors. Sets an output `has-warnings` (0 or 1) based on whether there are any warnings in the assert results.
# - release-auto: runs only on tag push and when there are no warnings (has-warnings == 0). Deploys without requiring approval.
# - release-approval: runs only on tag push and when there are warnings (has-warnings != 0). Requires approval (via environment) and then deploys.
# This meets the requirement:
# - Budgets: enforced by assert (errors fail the lighthouse job, so no release).
# - Flake mitigation: by setting `numberOfRuns` to 3 and taking median.
# - Release approvals: required only when there are warnings (i.e., performance is below recommended but above error threshold). But note: we are also requiring approval for every release that has warnings? Actually, we are requiring approval for every release that has warnings, but not for every release? Wait, we have two release jobs: one for no warnings (auto) and one for warnings (with approval). So we only require approval when there are warnings.
# But what if we want to always require approval for releases? Then we can just have one release job that always requires approval and remove the release-auto job. But the requirement says "release approvals", so maybe we want to always have approval? The problem doesn't specify.
# Given the ambiguity, I'll design for the more interesting case: only require approval when there are warnings.
# Implementation:
# We need to set the output `has-warnings` in the lighthouse job.
# Steps in lighthouse job:
# 1. Checkout code.
# 2. Install LHCI.
# 3. Run collect and assert, but capture the assert output to a file and not fail the job on errors? Actually, we want to fail on errors. So we run assert and if it fails (exit code 1) we fail the job. But we also want to capture the warnings.
# How to capture the warnings if the assert fails? We can run assert and then even if it fails, we can still parse the report? Actually, when assert fails, it still generates the report? We can set `--output` to a directory and then we can parse the `lighthouserc.json` or the `report.json`?
# Alternatively, we can run `lhci assert` without failing and then parse the report, and then if there are errors, we fail. But then we are not using the built-in failure of `lhci assert`.
# Let's do:
# lhci collect --url=... --numberOfRuns=3
# lhci assert ... --output=json --output-path=./lhci-report.json || true
# Then, we parse the `lhci-report.json` to count errors and warnings.
# If there are errors, we fail the job.
# Then, set the output `has-warnings` to 1 if there are warnings, else 0.
# But note: the `lhci assert` command will output the report even if it fails? Actually, the `assert` command will generate the report in the output path regardless of the exit code? I think so.
# So:
# - run: lhci collect ...
# - run: |
# lhci assert ... --output=json --output-path=./lhci-report.json || true
# - run: |
# errors=$(jq '[.assertionResults[] | select(.result == "error")] | length' lhci-report.json)
# warnings=$(jq '[.assertionResults[] | select(.result == "warn")] | length' lhci-report.json)
# if [ "$errors" -gt 0 ]; then
# echo "There are $errors errors. Failing."
# exit 1
# fi
# echo "has-warnings=$warnings" >> $GITHUB_OUTPUT
# But note: the `has-warnings` should be 0 if warnings=0, and 1 if warnings>0.
# Actually, we want to set `has-warnings` to 1 if there are any warnings, else 0.
# So:
# has_warnings=0
# if [ "$warnings" -gt 0 ]; then
# has_warnings=1
# fi
# echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
# But we can do:
# echo "has-warnings=$((warnings > 0 ? 1 : 0))" >> $GITHUB_OUTPUT
# However, we are in a bash script and we are using jq which outputs a number, so we can do:
# if [ "$warnings" -gt 0 ]; then
# echo "has-warnings=1" >> $GITHUB_OUTPUT
# else
# echo "has-warnings=0" >> $GITHUB_OUTPUT
# fi
# Alternatively, we can use jq to compute the has-warnings:
# has_warnings=$(jq '[.assertionResults[] | select(.result == "warn")] | length > 0' lhci-report.json)
# echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
# But note: jq outputs "true" or "false", and we want 0 or 1.
# has_warnings=$(jq '[.assertionResults[] | select(.result == "warn")] | length > 0' lhci-report.json | grep -o 'true' && echo 1 || echo 0)
# That's messy.
# Let's stick to:
# warnings=$(jq '[.assertionResults[] | select(.result == "warn")] | length' lhci-report.json)
# if [ "$warnings" -gt 0 ]; then
# echo "has-warnings=1" >> $GITHUB_OUTPUT
# else
# echo "has-warnings=0" >> $GITHUB_OUTPUT
# fi
# But note: the `assertionResults` might not have any `warn` if there are no warnings, so `warnings` might be empty? Actually, jq will output 0 if the array is empty.
# So we can do:
# warnings=$(jq '[.assertionResults[] | select(.result == "warn")] | length' lhci-report.json)
# if [ "$warnings" -gt 0 ]; then
# echo "has-warnings=1" >> $GITHUB_OUTPUT
# else
# echo "has-warnings=0" >> $GITHUB_OUTPUT
# fi
# And then, if there are errors, we exit 1.
# But note: we are running `lhci assert` and then capturing the report. The `lhci assert` command might exit with 1 if there are errors, but we are ignoring it with `|| true`. Then we check for errors ourselves.
# This is acceptable.
# However, we also want to upload the report? We can run `lhci upload` after the assert? But the assert might fail and we still want to upload? Yes, we want to upload the report even if there are errors (so we can see the failures). So we should upload after the assert and before we fail? Actually, we run collect and then assert (which uses the collected data) and then we upload. We can upload after the assert.
# But note: the `lhci upload` command uploads the last collected run? Or we need to specify? Actually, the `lhci upload` command uploads the current run's results (from the collect step). So we can run:
# lhci collect ...
# lhci assert ...
# lhci upload ...
# But if the assert fails, we still want to upload? Yes.
# So:
# lhci collect ...
# lhci assert ... --output=json --output-path=./lhci-report.json || true
# lhci upload ...
# Then we parse the `lhci-report.json` and fail if there are errors.
# But note: the `lhci upload` might fail? We can ignore that? Or we can let it fail? Probably we want to upload even if the assert fails, so we don't want the upload to fail the job? Actually, we want to know if the upload fails? Maybe we can ignore upload failure for now.
# Alternatively, we can run the upload and then if it fails, we still parse the report? But the report is already generated by the assert step.
# So:
# set -e # we want to fail on any error except the assert (which we allow to fail) and upload (which we might allow to fail?)
# lhci collect ...
# lhci assert ... --output=json --output-path=./lhci-report.json || true
# lhci upload ... || echo "Upload failed, but continuing."
# Then parse the report and fail if errors.
# But note: if the collect fails, we want to fail immediately. So we don't set -e at the beginning? We can let the job fail on any error except the ones we handle.
# Alternatively, we can do:
# lhci collect ...
# assert_exit_code=0
# lhci assert ... --output=json --output-path=./lhci-report.json || assert_exit_code=$?
# lhci upload ... || true
# # Now, if assert_exit_code is 1 (meaning there were errors) we want to fail? But we are going to check the report for errors anyway.
# Actually, we are ignoring the assert exit code and then checking the report. So we don't need to capture the assert exit code.
# So:
# lhci collect ...
# lhci assert ... --output=json --output-path=./lhci-report.json || true
# lhci upload ... || true
# Then parse the report.
# But note: the `lhci assert` might fail because of errors, but we are ignoring that. Then we parse and if there are errors, we fail.
# This is acceptable.
# Now, the `lhci-report.json` is generated by the assert step. We assume that the assert step will generate the JSON report even if it fails.
# Let's code the lighthouse job:
# jobs:
# lighthouse:
# runs-on: ubuntu-latest
# outputs:
# has-warnings: ${{ steps.lhci.outputs.has-warnings }}
# steps:
# - uses: actions/checkout@v2
# - name: Install Lighthouse CI
# run: npm install -g @lhci/cli
# - name: Run Lighthouse CI
# id: lhci
# run: |
# lhci collect --url=https://example.com --numberOfRuns=3
# lhci assert --preset=lighthouse:recommended --config=./lighthouserc.json --output=json --output-path=./lhci-report.json || true
# lhci upload --target=temporary-public-storage || true
# # Parse the report
# errors=$(jq '[.assertionResults[] | select(.result == "error")] | length' lhci-report.json)
# warnings=$(jq '[.assertionResults[] | select(.result == "warn")] | length' lhci-report.json)
# if [ "$errors" -gt 0 ]; then
# echo "There are $errors errors. Failing."
# exit 1
# fi
# has_warnings=0
# if [ "$warnings" -gt 0 ]; then
# has_warnings=1
# fi
# echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
# Then, the release jobs as above.
# But note: the `lhci assert` command uses the config from `lighthouserc.json`. We have to have that file in the repo.
# Also, we are running on `https://example.com`. We need to set the URL to the actual site we are testing. We can set it in the config or in the command. We can set the URL in the config? Or we can pass it as an argument? We are passing `--url` in collect.
# We can also set the URL in the config. But we might want to test different URLs for different environments? Then we can set the URL in the CI environment variable.
# We can do:
# lhci collect --url=${{ secrets.SITE_URL }} --numberOfRuns=3
# And then set the `SITE_URL` in the repository secrets.
# Alternatively, we can set the URL in the config and then override it with the `--url` flag? Actually, the `--url` flag in collect overrides the config.
# So we can have a generic config and then pass the URL.
# Now, what about the budgets? They are in the config file.
# This design should work.
# Let's write the complete workflow:
# .github/workflows/lighthouse.yml:
# name: Lighthouse CI
# on:
# pull_request:
# push:
# tags:
# - 'v*'
# jobs:
# lighthouse:
# runs-on: ubuntu-latest
# outputs:
# has-warnings: ${{ steps.lhci.outputs.has-warnings }}
# steps:
# - uses: actions/checkout@v2
# - name: Install Lighthouse CI
# run: npm install -g @lhci/cli
# - name: Run Lighthouse CI
# id: lhci
# run: |
# # Set the URL from an environment variable or use a default?
# URL=${SITE_URL:-https://example.com}
# lhci collect --url=$URL --numberOfRuns=3
# lhci assert --preset=lighthouse:recommended --config=./lighthouserc.json --output=json --output-path=./lhci-report.json || true
# lhci upload --target=temporary-public-storage || true
# # Parse the report
# errors=$(jq '[.assertionResults[] | select(.result == "error")] | length' lhci-report.json)
# warnings=$(jq '[.assertionResults[] | select(.result == "warn")] | length' lhci-report.json)
# if [ "$errors" -gt 0 ]; then
# echo "There are $errors errors. Failing."
# exit 1
# fi
# has_warnings=0
# if [ "$warnings" -gt 0 ]; then
# has_warnings=1
# fi
# echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
# - name: Install jq
# run: sudo apt-get install jq # if not already installed? Actually, the ubuntu-latest might not have jq. We can install it.
# But note: the step that runs the LHCI also installs jq? We are using jq in the same step. So we need to install jq before using it.
# We can split:
# - name: Install jq
# run: sudo apt-get update && sudo apt-get install -y jq
# - name: Run Lighthouse CI
# id: lhci
# run: |
# ... (as above)
# Alternatively, we can use a container that has jq? Or we can use `node` to parse JSON? But we are using jq.
# We can also parse the JSON with `node` if we don't want to install jq. But let's install jq.
# Now, the release jobs:
# release-auto:
# needs: lighthouse
# if: |
# github.event_name == 'push' &&
# startsWith(github.ref, 'refs/tags/') &&
# needs.lighthouse.outputs.has-warnings == '0'
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Deploy
# run: echo "Deploying automatically because no warnings."
# release-approval:
# needs: lighthouse
# if: |
# github.event_name == 'push' &&
# startsWith(github.ref, 'refs/tags/') &&
# needs.lighthouse.outputs.has-warnings != '0'
# environment: production
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Deploy
# run: echo "Deploying with manual approval because there are warnings."
# But note: the `release-approval` job will require manual approval because of the `environment: production` and if the environment has required reviewers.
# We must set up the environment `production` in the repository settings and require reviewers.
# This design works.
# However, note that the `release-auto` job will run automatically without approval, and `release-approval` will wait for approval.
# But what if we want to always require approval for releases? Then we can remove the `release-auto` job and just have one release job that always requires approval, and then we don't need the `has-warnings` output? But then we are not using the warnings to conditionally require approval.
# Since the requirement says "release approvals", I think we should always require approval for releases. But the problem also says "enforcing budgets, flake mitigation, and release approvals". It doesn't say that release approvals are conditional.
# Given the ambiguity, I'll provide both options and let the user choose.
# But the initial design (with conditional approval) is more nuanced and might be what is intended: only require approval when there are warnings (i.e., performance is below the recommended but not failing). If there are errors, we fail anyway.
# I'll go with the conditional approval design.
# One more thing: the `lighthouserc.json` should be in the repo. We can put it in the root.
# Also, we are using `temporary-public-storage` for upload. That's a LHCI server that is public. We might want to use our own LHCI server? We can change the upload target.
# Now, let's write the complete workflow file.
# Note: We are using `npm install -g @lhci/cli` to install LHCI. We might want to cache it? But for simplicity, we install every time.
# Also, we are using `sudo apt-get install jq` which might not be necessary if we use a container that has jq. Alternatively, we can use `node` to parse the JSON. Let's use `node` to avoid installing jq.
# We can parse the JSON with node:
# errors=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults.filter(r => r.result === 'error').length);")
# warnings=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults.filter(r => r.result === 'warn').length);")
# But note: the `lhci-report.json` might be an array? Actually, the `lhci assert` output in JSON format is an object with `assertionResults` array.
# So:
# errors=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'error').length : 0);")
# warnings=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'warn').length : 0);")
# We can do that.
# Let's adjust the lighthouse job step:
# - name: Run Lighthouse CI
# id: lhci
# run: |
# URL=${SITE_URL:-https://example.com}
# lhci collect --url=$URL --numberOfRuns=3
# lhci assert --preset=lighthouse:recommended --config=./lighthouserc.json --output=json --output-path=./lhci-report.json || true
# lhci upload --target=temporary-public-storage || true
# errors=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'error').length : 0);")
# warnings=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'warn').length : 0);")
# if [ "$errors" -gt 0 ]; then
# echo "There are $errors errors. Failing."
# exit 1
# fi
# has_warnings=0
# if [ "$warnings" -gt 0 ]; then
# has_warnings=1
# fi
# echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
# This way we don't need to install jq.
# But note: the `node` command might fail if the file doesn't exist? We are assuming the assert step generated the file. If the assert step failed to generate the file, then we have a problem. But the `lhci assert` should generate the file even if it fails? Let's hope so.
# Alternatively, we can check if the file exists.
# Let's do:
# if [ ! -f lhci-report.json ]; then
# echo "LHCI report not found. Failing."
# exit 1
# fi
# So:
# - name: Run Lighthouse CI
# id: lhci
# run: |
# URL=${SITE_URL:-https://example.com}
# lhci collect --url=$URL --numberOfRuns=3
# lhci assert --preset=lighthouse:recommended --config=./lighthouserc.json --output=json --output-path=./lhci-report.json || true
# lhci upload --target=temporary-public-storage || true
# if [ ! -f lhci-report.json ]; then
# echo "LHCI report not found. Failing."
# exit 1
# fi
# errors=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'error').length : 0);")
# warnings=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'warn').length : 0);")
# if [ "$errors" -gt 0 ]; then
# echo "There are $errors errors. Failing."
# exit 1
# fi
# has_warnings=0
# if [ "$warnings" -gt 0 ]; then
# has_warnings=1
# fi
# echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
# This is robust.
# Now, the complete workflow:
# We assume that the `lighthouserc.json` is in the root of the repository.
# We also assume that the `SITE_URL` secret is set in the repository settings for the production site.
# For PRs, we might want to test the PR's deployment? Then we need to set the URL to the PR deployment. We can use the `github.event.pull_request.head.repo.url` or something? But that's complicated.
# Alternatively, we can set the URL in the config for PRs to be the PR's deployment URL? But then we need to know the URL.
# Since this is complex, we can leave it as an exercise to the user to set the `SITE_URL` appropriately for the environment.
# For PRs, we might want to test the merge commit's deployment? We can use the `github.event.pull_request.head.sha` to build the URL? That depends on the hosting.
# Given the complexity, I'll assume that the user will set the `SITE_URL` appropriately for the environment (maybe using environment variables in the CI).
# Alternatively, we can set the URL in the `lighthouserc.json` for the `collect` step? We are overriding with `--url`, so we can set the URL in the command.
# We can set:
# URL=${{ secrets.SITE_URL }}
# But for PRs, we might not have the `SITE_URL` set? Or we can set a different secret for PRs? We can have:
# URL=${SITE_URL:-${PR_DEPLOYMENT_URL:-https://example.com}}
# And then set `PR_DEPLOYMENT_URL` for PRs? But we don't have that secret.
# Alternatively, we can compute the PR deployment URL from the PR number? For example, if we deploy PRs to `https://pr-${PR_NUMBER}.example.com`.
# We can get the PR number from the event:
# If it's a PR, we can set:
# PR_NUMBER=${{ github.event.pull_request.number }}
# Then:
# URL="https://pr-${PR_NUMBER}.example.com"
# But we are in a shell script, so we need to pass the PR number as an environment variable.
# We can do:
# env:
# PR_NUMBER: ${{ github.event.pull_request.number }}
# SITE_URL: ${{ secrets.SITE_URL }}
# Then in the script:
# if [ -n "$PR_NUMBER" ]; then
# URL="https://pr-${PR_NUMBER}.example.com"
# else
# URL=${SITE_URL}
# fi
# But note: the workflow triggers on both PR and push (tags). So for PRs, `github.event.pull_request.number` is set, and for tags, it's not.
# We can adjust:
# - name: Run Lighthouse CI
# id: lhci
# env:
# PR_NUMBER: ${{ github.event.pull_request.number }}
# SITE_URL: ${{ secrets.SITE_URL }}
# run: |
# if [ -n "$PR_NUMBER" ]; then
# URL="https://pr-${PR_NUMBER}.example.com"
# else
# URL=${SITE_URL}
# fi
# ...
# But note: the `github.event.pull_request.number` is only available for pull_request events. For push events, it's empty.
# This is acceptable.
# However, we might want to test the production site on tags and the PR site on PRs.
# So we are good.
# Let's write the final workflow.
# Note: We are using `github.event.pull_request.number` which is only available for pull_request events. For push events (tags) we use the `SITE_URL` secret.
# We assume that the `SITE_URL` secret is set to the production URL.
# Now, the complete workflow:
# .github/workflows/lighthouse.yml:
# name: Lighthouse CI
# on:
# pull_request:
# push:
# tags:
# - 'v*'
# jobs:
# lighthouse:
# runs-on: ubuntu-latest
# outputs:
# has-warnings: ${{ steps.lhci.outputs.has-warnings }}
# steps:
# - uses: actions/checkout@v2
# - name: Install Lighthouse CI
# run: npm install -g @lhci/cli
# - name: Run Lighthouse CI
# id: lhci
# env:
# PR_NUMBER: ${{ github.event.pull_request.number }}
# SITE_URL: ${{ secrets.SITE_URL }}
# run: |
# if [ -n "$PR_NUMBER" ]; then
# URL="https://pr-${PR_NUMBER}.example.com"
# else
# URL=${SITE_URL}
# fi
# echo "Testing URL: $URL"
# lhci collect --url=$URL --numberOfRuns=3
# lhci assert --preset=lighthouse:recommended --config=./lighthouserc.json --output=json --output-path=./lhci-report.json || true
# lhci upload --target=temporary-public-storage || true
# if [ ! -f lhci-report.json ]; then
# echo "LHCI report not found. Failing."
# exit 1
# fi
# errors=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'error').length : 0);")
# warnings=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'warn').length : 0);")
# if [ "$errors" -gt 0 ]; then
# echo "There are $errors errors. Failing."
# exit 1
# fi
# has_warnings=0
# if [ "$warnings" -gt 0 ]; then
# has_warnings=1
# fi
# echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
# release-auto:
# needs: lighthouse
# if: |
# github.event_name == 'push' &&
# startsWith(github.ref, 'refs/tags/') &&
# needs.lighthouse.outputs.has-warnings == '0'
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Deploy
# run: echo "Deploying automatically because no warnings."
# release-approval:
# needs: lighthouse
# if: |
# github.event_name == 'push' &&
# startsWith(github.ref, 'refs/tags/') &&
# needs.lighthouse.outputs.has-warnings != '0'
# environment: production
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Deploy
# run: echo "Deploying with manual approval because there are warnings."
# This should work.
# However, note: the `release-auto` and `release-approval` jobs are mutually exclusive because of the condition on `has-warnings`. But what if the `has-warnings` is not set? Actually, the lighthouse job will set it to 0 or 1.
# Also, note: the `release-auto` job will run only when there are no warnings, and `release-approval` only when there are warnings. And both only on tag pushes.
# We are done.
# But note: the `lighthouserc.json` must be present. We assume it is in the root.
# Also, we are using `--preset=lighthouse:recommended` in the assert. We might want to adjust the preset? We can set it in the config.
# Actually, the `--preset` in assert can be overridden by the config? We are using both `--preset` and `--config`. The `--config` file might have its own `assert` configuration. We are using the preset and then the config? The documentation says that the `--config` file can have `ci` and `assert` sections. We are using the `--preset` to set the preset for the assert? Actually, the `lhci assert` command can take a `--preset` to set the preset for the assertions? Or for the collection? I think the `--preset` in `assert` is for the assertions? Actually, the `lhci assert` command uses the `--preset` to set the assertions preset? But we are also providing a config file that has assertions. They are merged? I'm not sure.
# Let's check: the `lhci assert` command can take a `--preset` to set the assertions preset (like `lighthouse:recommended`). And then the `--config` file can override? Or the config file is used for collection? Actually, the `lhci` command has two subcommands: `collect` and `assert`. The `collect` uses the `collect` section of the config, and the `assert` uses the `assert` section. We are providing `--config` to both? Actually, we are:
# lhci collect ... --config=./lighthouserc.json
# lhci assert ... --config=./lighthouserc.json
# But we are also providing `--preset` in the assert. That might override the `assert` section's preset? Or it might be merged.
# To be safe, we can put everything in the config file and not use `--preset` in the command.
# Let's adjust:
# In the config file, we set:
# {
# "ci": {
# "collect": {
# "url": ["https://example.com"], // but we are overriding with --url, so we can leave it empty?
# "numberOfRuns": 3,
# "settings": { ... }
# },
# "assert": {
# "preset": "lighthouse:recommended",
# "assertions": { ... }
# },
# "upload": {
# "target": "temporary-public-storage"
# }
# }
# }
# Then we run:
# lhci collect --config=./lighthouserc.json --url=$URL
# lhci assert --config=./lighthouserc.json
# But note: the `collect` command will use the `url` from the config if we don't provide `--url`. We are providing `--url`, so it will override.
# So we can remove the `--url` from the config file and always pass it via command line? Or we can set the URL in the config for each environment? We are setting it dynamically.
# Let's keep the `--url` in the command and remove the `url` from the config file.
# Alternatively, we can set the URL in the config file for the `collect` section? But then we have to change the config for PRs and tags? We are already setting it dynamically.
# So we remove the `url` from the `collect` section in the config file.
# Then:
# lhci collect --config=./lighthouserc.json --url=$URL --numberOfRuns=3 # but note: the config file might have `numberOfRuns`? We are overriding with `--numberOfRuns`? Actually, the `--numberOfRuns` is a flag for the collect command, and it will override the config.
# We can set `numberOfRuns` in the config file and then not pass it in the command? But we want to set it to 3 for flake mitigation.
# So we can set it in the config file.
# Let's adjust the config file:
# lighthouserc.json:
# {
# "ci": {
# "collect": {
# "numberOfRuns": 3,
# "settings": {
# "preset": "desktop",
# "maxWaitForFcp": 3000,
# "maxWaitForLargestContentfulPaint": 5000,
# "maxWaitForLoad": 3000
# }
# },
# "assert": {
# "preset": "lighthouse:recommended",
# "assertions": {
# "categories:performance": ["error", {"minScore": 0.9}],
# ... // other assertions and budgets
# }
# },
# "upload": {
# "target": "temporary-public-storage"
# }
# }
# }
# Then we run:
# lhci collect --config=./lighthouserc.json --url=$URL
# lhci assert --config=./lighthouserc.json
# And we don't need to pass `--numberOfRuns` or `--preset` in the command.
# This is cleaner.
# So the final workflow:
# .github/workflows/lighthouse.yml:
name: Lighthouse CI
on:
pull_request:
push:
tags:
- 'v*'
jobs:
lighthouse:
runs-on: ubuntu-latest
outputs:
has-warnings: ${{ steps.lhci.outputs.has-warnings }}
steps:
- uses: actions/checkout@v2
- name: Install Lighthouse CI
run: npm install -g @lhci/cli
- name: Run Lighthouse CI
id: lhci
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
SITE_URL: ${{ secrets.SITE_URL }}
run: |
if [ -n "$PR_NUMBER" ]; then
URL="https://pr-${PR_NUMBER}.example.com"
else
URL=${SITE_URL}
fi
echo "Testing URL: $URL"
lhci collect --config=./lighthouserc.json --url=$URL
lhci assert --config=./lighthouserc.json --output=json --output-path=./lhci-report.json || true
lhci upload || true
if [ ! -f lhci-report.json ]; then
echo "LHCI report not found. Failing."
exit 1
fi
errors=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'error').length : 0);")
warnings=$(node -e "const data = require('./lhci-report.json'); console.log(data.assertionResults ? data.assertionResults.filter(r => r.result === 'warn').length : 0);")
if [ "$errors" -gt 0 ]; then
echo "There are $errors errors. Failing."
exit 1
fi
has_warnings=0
if [ "$warnings" -gt 0 ]; then
has_warnings=1
fi
echo "has-warnings=$has_warnings" >> $GITHUB_OUTPUT
release-auto:
needs: lighthouse
if: |
github.event_name == 'push' &&
startsWith(github.ref, 'refs/tags/') &&
needs.lighthouse.outputs.has-warnings == '0'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy
run: echo "Deploying automatically because no warnings."
release-approval:
needs: lighthouse
if: |
github.event_name == 'push' &&
startsWith(github.ref, 'refs/tags/') &&
needs.lighthouse.outputs.has-warnings != '0'
environment: production
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy
run: echo "Deploying with manual approval because there are warnings."
# This is the complete design.
# Note: The `release-auto` and `release-approval` jobs are just placeholders for the actual deployment steps. Replace the `echo` with your actual deployment steps.
# Also, the `lighthouserc.json` should be in the root of the repository.
# We are using `temporary-public-storage` for upload. That's a free service provided by LHCI. You might want to use your own LHCI server for private projects.
# To use your own LHCI server, change the `upload.target` in the config file.
# This design enforces:
# - Budgets: via the assertions in the config.
# - Flake mitigation: by running 3 times and taking the median (default behavior of LHCI when `numberOfRuns` > 1).
# - Release approvals: by requiring manual approval for the release when there are warnings (and blocking release on errors).
# We are done.
# However, note: the `release-approval` job will require approval because of the `environment: production`. We must set up the `production` environment in the repository settings and require reviewers.
# If you want to always require approval for releases (even without warnings), then you can remove the `release-auto` job and just have:
# release:
# needs: lighthouse
# if: |
# github.event_name == 'push' &&
# startsWith(github.ref, 'refs/tags/')
# environment: production
# runs-on: ubuntu-latest
# steps: ...
# And then we don't need the `has-warnings` output? But we still want to fail on errors, which is done by the lighthouse job. So we can simplify.
# But the requirement says "release approvals", so we are providing a conditional approval. If you want unconditional approval, use the simpler version.
# I leave it to the user to decide.
# Final note: the `lhci upload` command will upload the results to the LHCI server (temporary-public-storage or your own). Then you can view the reports at the provided URL. We are not posting the report URL anywhere. We might want to post it as a comment on the PR or on the release? We can do that in the lighthouse job.
# But that's beyond the scope.
# We have engineered the gate as requested.
# Let me know if you have any questions.
# However, note: the `lhci upload` command in the lighthouse job will upload the results for every run (PR and tags). That might be acceptable.
# We are done.
# But wait: the `lhci collect` and `lhci assert` and `lhci upload` are using the same config. We are setting `numberOfRuns` in the config, so we don't need to pass it.
# Also, the `--url` in collect overrides the config's `url` (if any). We removed the `url` from the config, so we are safe.
# One more thing: the `lhci assert` command will use the assertions from the config. We are setting the error thresholds in the config. So if any assertion fails (with error) then the `lhci assert` will exit with 1, but we are ignoring it and then checking the JSON. So we are in control.
# This is acceptable.
# We are good.
# Let's write the final workflow file.
# However, note: the `lhci collect` command will use the `url` from the command line and the `numberOfRuns` from the config. So we are running 3 times for flake mitigation.
# Perfect.
# End of design.</think>