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()runningkubectl 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=120sGotchas
- 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
How to Wait for a Rollout to Finish From CI in GitHub ActionsMake a GitHub Actions deploy fail loudly when pods do not become ready by running kubectl rollout status with…
How to Deploy With helm upgrade --install From CI in GitHub ActionsDeploy a Helm chart idempotently from GitHub Actions with helm upgrade --install --wait --atomic, so the rele…