How to Roll Back a Deployment With GitLab CI/CD
A manual rollback job that takes a target tag lets you redeploy a known-good version on demand.
Add a when: manual rollback job that deploys the image tag in a ROLLBACK_TAG variable, so you can revert without reverting code.
Steps
- Tag and keep every deployable image (e.g. by short SHA).
- Add a manual
rollbackjob parameterized byROLLBACK_TAG. - Redeploy that tag and wait for the rollout.
- Run it from the pipeline or via "Run pipeline" with the variable set.
.gitlab-ci.yml
.gitlab-ci.yml
rollback:
stage: deploy
when: manual
variables:
ROLLBACK_TAG: ''
script:
- test -n "$ROLLBACK_TAG" || (echo "Set ROLLBACK_TAG" && exit 1)
- ./deploy.sh --tag "$ROLLBACK_TAG"
- kubectl rollout status deployment/myapp -n production --timeout=120s
environment:
name: productionGotchas
- Rollback only works if old image tags still exist; do not prune them aggressively.
- Rolling back code-coupled database migrations needs a separate, tested down path.
Related guides
How to Run Database Migrations on Deploy With GitLab CI/CDRun database migrations as a dedicated stage before the app deploy in GitLab CI/CD using a resource_group so…
How to Run a Blue-Green Deploy With GitLab CI/CDRun a blue-green deploy in GitLab CI/CD by releasing to the idle color, then switching the live pointer in a…