Sub topic 13
Verifying and Rolling Back
This chapter assumes you can read the deploy job from the previous chapter: a deploy job that needs build, uses the production environment, and syncs files to S3 with OIDC credentials. You also need basic familiarity with curl and the GitHub CLI.
A deploy step that exits with code 0 only proves the upload command worked. It does not prove the site works. This chapter adds a check after the deploy and a way to undo a bad release.
Verify after you deploy
A smoke test is a small request that confirms the application is alive and serving the new version. Run it as the last step of the deploy job. If it fails, the job fails, and the run turns red.
A useful smoke test checks two things: the URL answers with a success status, and the response contains the commit you just shipped. The second check catches the case where the old version is still being served.
- name: Smoke test
env:
SITE_URL: https://example.com
run: |
for attempt in 1 2 3 4 5; do
status=$(curl -s -o /dev/null -w "%{http_code}" "$SITE_URL/health")
if [ "$status" = "200" ]; then
echo "Healthy on attempt $attempt"
exit 0
fi
echo "Attempt $attempt returned $status, retrying in 10s"
sleep 10
done
echo "Site did not become healthy"
exit 1The loop retries because a CDN or load balancer can take a few seconds to switch over. Without retries, you get false failures. Without a limit, a broken deploy would hang until the job timeout.
Roll back
A rollback restores the last known good version. With this pipeline, the simplest rollback is a redeploy of an earlier commit. You have three common options.
| Option | How | Trade-off |
|---|---|---|
| Revert the commit | `git revert <sha>` and push to `main` | Leaves a clear history, but runs the whole pipeline again |
| Re-run an old workflow run | Re-run the earlier successful run from the Actions tab | Fast, but depends on the artifact still being kept and on the old workflow file |
| Manual redeploy of a ref | Start a `workflow_dispatch` run on a chosen tag or commit | Works without changing `main`, needs the trigger to be set up |
Re-running an old run is limited by retention. Artifacts expire after the retention period, 90 days by default, and a re-run of an expired run cannot download the build.
Roll back automatically
You can make a failed smoke test trigger the rollback. A later step with if: failure() runs only when an earlier step failed. This example assumes you enabled versioning on the bucket and keep the previous build in a previous/ prefix.
- name: Roll back on failure
if: failure()
run: |
aws s3 sync s3://example-bucket/previous s3://example-bucket --delete
echo "Rolled back to the previous build"The flow
Note that the run still ends as failed after a rollback. That is intended, because the release did not go out, and the team needs to be told.
Common errors
- Checking only the exit code of the deploy command, not the live site.
- Testing a URL that a cache serves from the old release.
- Using
if: always()for rollback, which also rolls back successful runs. - Forgetting that the
productionenvironment may require approval again when you re-run a job.
Exercise
Why does the rollback step use if: failure() instead of if: always()?
Show answer
failure() runs the step only when a previous step failed, so a healthy deploy is left alone. always() would run the rollback on every run, including successful ones, and would undo good releases.
Exercise
Your smoke test passes, but users still see the old version. Name one likely cause and one check to add.
Show answer
A CDN or browser cache is still serving the old files. Make the smoke test compare a version marker, such as the commit SHA in a file or header, against the SHA that was deployed.