Skip to content
Latchkey

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 rollback job parameterized by ROLLBACK_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: production

Gotchas

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →