Skip to content
Latchkey

How to Roll Back a Deployment on Failure From CI in GitHub Actions

Gate a kubectl rollout undo step on if: failure() so a deploy that fails its readiness wait reverts to the last working ReplicaSet.

Run kubectl rollout status to detect a failed deploy, then add a step with if: failure() that runs kubectl rollout undo to restore the previous revision.

Steps

  • Apply manifests and wait with kubectl rollout status --timeout.
  • Add a step with if: failure() running kubectl rollout undo.
  • Re-check status after the undo so the job reflects the recovered state.

Workflow

.github/workflows/deploy.yml
steps:
  - run: kubectl apply -f k8s/
  - id: wait
    run: kubectl rollout status deployment/web -n prod --timeout=120s
  - name: Roll back
    if: failure() && steps.wait.outcome == 'failure'
    run: |
      kubectl rollout undo deployment/web -n prod
      kubectl rollout status deployment/web -n prod --timeout=120s

Gotchas

  • rollout undo needs at least two revisions; the very first deploy has nothing to revert to.
  • Helm users get the same effect with helm upgrade --atomic.

Related guides

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