Rollback and release strategy in a CI/CD pipeline
A pipeline rollback redeploys a previous, known-good immutable image tag, triggered by a health check failure or by hand, and works only if database changes are backward compatible.
- Updated Nov 27, 2026
- Markdown
Add a health gate, an automatic rollback step and a manual rollback workflow that redeploys a previous image tag.
- A deploy workflow that runs a script on your server
- Images tagged with the commit SHA or version
A health gate decides whether a release stays
- The pipeline deploys the new tag, then the health gate polls the health endpoint several times.
- When every check passes, the job succeeds and v2 stays live.
- When the checks fail, the pipeline redeploys the previous tag and marks the run as failed.
Static diagram
- Deploy v2releaseHealth gate
- Health gatepassesv2 stays live
- Health gatefailsBack to v1
A deploy that you cannot undo is a gamble. A pipeline makes rollback cheap by keeping every released image, deploying by an immutable tag, and checking health right after each deploy. This lesson adds a health gate, an automatic rollback and a manual rollback workflow to the SSH deploy from Deploy over SSH from GitHub Actions. The ideas behind it are in Rollbacks and Zero-downtime deploys.
How it works#
Every release is an image with a tag that never changes, for example sha-1a2b3c4 or 1.2.0. The server runs one tag at a time. A deploy swaps the tag. A health gate then asks the app whether it is serving. If yes, the release is done. If no, the pipeline redeploys the previous tag. Because the old image still exists in the registry, going back is a pull and a restart, with no rebuild. A rollback is only safe if the database still works with the old code, which is why migrations need care.
Step 1: Keep tags immutable#
Never reuse a tag. latest and a moving stable tag cannot answer “what exactly ran yesterday?”. Tag by commit SHA or by version, and keep old ones in the registry long enough to roll back to. Do not clean up the tag a server currently runs or the one before it. Also record which tag is live. A tiny file on the server works:
echo "$TAG" > /srv/app/.current-tagThe deploy script reads the old value into a variable before overwriting it, which is what the automatic rollback uses.
Step 2: Add a health endpoint and gate#
Your app needs an endpoint that returns success only when it can serve, for example /healthz. See Docker health checks. After the deploy step, loop on it with a deadline:
for i in $(seq 1 30); do
if curl -fsS --max-time 5 https://example.com/healthz > /dev/null; then
echo "healthy"
exit 0
fi
echo "waiting ($i)"
sleep 5
done
echo "unhealthy after retries" >&2
exit 1curl -f makes HTTP errors return a non-zero exit code. --max-time stops one slow request from hanging the loop. The loop is bounded, so the job fails in a known time instead of hanging. A health check that always returns 200 proves nothing, so make it touch what the app needs, such as the database connection.
Step 3: Roll back automatically on failure#
Put the gate in the deploy job, then add a step that runs only on failure:
name: deploy
on:
workflow_dispatch:
inputs:
tag:
description: "Image tag to deploy, for example sha-1a2b3c4"
required: true
previous_tag:
description: "Known-good tag to fall back to"
required: true
permissions:
contents: read
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- name: Prepare SSH
run: |
install -m 700 -d ~/.ssh
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ci
chmod 600 ~/.ssh/id_ci
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts
env:
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
- name: Deploy new tag
env:
TAG: ${{ inputs.tag }}
run: ssh -i ~/.ssh/id_ci deploy@203.0.113.10 "$TAG"
- name: Health gate
run: |
for i in $(seq 1 30); do
curl -fsS --max-time 5 https://example.com/healthz > /dev/null && exit 0
sleep 5
done
exit 1
- name: Roll back on failure
if: failure()
env:
PREVIOUS_TAG: ${{ inputs.previous_tag }}
run: ssh -i ~/.ssh/id_ci deploy@203.0.113.10 "$PREVIOUS_TAG"if: failure() runs the step only when an earlier step failed. It reuses the same restricted SSH key and the same script, so rollback is just another deploy of the old tag. The inputs go through env instead of ${{ }} inside the shell line, because an expression is pasted into the script before it runs on the runner, so a crafted value could run commands there even though the server validates the tag. Pass the previous tag automatically if your release step records it.
Step 4: Provide a manual rollback#
The same workflow doubles as a manual rollback: run it from the Actions tab with the old tag as tag. For a cleaner interface, create a second workflow that takes only tag and runs the deploy and the health gate. Manual rollback matters because a release can pass the health check and still be wrong, for example a bug found an hour later. You decide, you pick the tag, and you press the button.
Step 5: Make migrations backward compatible#
Code rollback takes seconds. A schema change may not roll back at all. Use two releases for any breaking schema change, often called expand and contract:
- Expand. Add the new column or table, nullable or with a default. Old code ignores it, new code uses it.
- Migrate. Backfill data while both versions work.
- Contract. In a later release, after the old code is gone, remove the old column.
Never drop or rename a column in the same release that stops using it. Take a database backup before a risky migration, and test the restore (see Test a backup by restoring it). For the full container walk-through, read the blog guide Roll back a failed Docker deploy and Zero-downtime Docker deploys with rollback.
Check it worked#
Test rollback before you need it. Deploy a deliberately broken version to a staging environment, for example one whose /healthz returns 500, and watch the gate fail and the previous tag come back. Confirm the server runs the old image (docker compose ps shows the old tag) and that the site answers. Then run the manual rollback with a known tag and confirm the same. A rollback you have never run is an assumption.
Edge cases and gotchas#
- The old image was pruned. Registry cleanup or
docker image pruneon the server can remove the tag you want. Keep at least the last few releases. - Health check too shallow. A static
okendpoint passes while the database is unreachable. Check dependencies. - Health check too strict. A slow start fails a 30-second window. Tune the retries to your app’s real start time, not a guess.
- Rollback hides the cause. After rolling back, read the failed release’s logs before trying again. See Read container logs.
- Cancelling a deploy halfway. Keep
cancel-in-progress: falsefor deploy groups so a new run waits. - Config changes. Rolling back the image does not roll back changed environment variables or compose files. Version those with the code.
- Passing health but serving errors. Some failures show up only under real traffic. See Fix a deploy that passes health checks but serves errors.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
manifest unknown on rollback pull | Old tag deleted from the registry | Keep recent tags, or pick another known-good tag |
| Health gate fails but app works by hand | Wrong URL, proxy not ready or DNS | Test the exact URL from the runner’s point of view |
| Rollback step never runs | Missing if: failure() | Add it to the step |
| Job hangs on the gate | No --max-time on curl | Add a timeout and a bounded loop |
| App crashes after rollback | Migration not backward compatible | Restore from backup, use expand and contract next time |
| Two deploys collide | No concurrency group | Add one with cancel-in-progress: false |
refusing tag: from the script | Input does not match the allowed pattern | Pass a tag like sha-1a2b3c4 |
Quick reference#
# health gate
curl -fsS --max-time 5 https://example.com/healthz
# manual rollback: run the deploy workflow with the old tag
# server check
docker compose ps
cat /srv/app/.current-tag- name: Roll back on failure
if: failure()
env:
PREVIOUS_TAG: ${{ inputs.previous_tag }}
run: ssh -i ~/.ssh/id_ci deploy@203.0.113.10 "$PREVIOUS_TAG"Related lessons#
- Rollbacks: the concept and manual steps without a pipeline.
- Zero-downtime deploys: swapping versions without dropping requests.
- Docker health checks: the health signal inside the container.
- Version and tag releases: where the immutable tags come from.
- Blue-green vs rolling deploys: release strategies beyond a simple swap.
Next#
Continue with What to monitor first.
Check what you learned
Frequently asked questions
How do I roll back a deployment in a CI/CD pipeline?
Redeploy the previous known-good image tag. A manual workflow_dispatch workflow that takes the tag as an input is the simplest way, and it uses the same deploy script as a normal release.
Why should image tags be immutable?
If a tag can be overwritten, the same name can mean different code on different days, so you cannot reliably return to a previous state. Tag with the commit SHA or a version and never reuse the tag.
What is a health gate?
A step after the deploy that requests a health endpoint until it succeeds or a deadline passes. If it never succeeds, the job fails and can trigger a rollback.
Why do database migrations make rollbacks hard?
Code can go back to the previous version in seconds, but a migration that dropped or renamed data cannot. Make migrations backward compatible so the old code still works against the new schema.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Zero Downtime Deploy for Docker Apps, With Rollback How a Docker deploy waits for readiness before cutover, holds the previous release for rollback, rolls back on its own, and how to rehearse it.