Deploy to Kubernetes and roll back on failure
In this task you'll deploy to Kubernetes from a pipeline, check it worked, and undo it automatically if not.
Continuous deployment isn't finished when kubectl apply returns: that only means the API server accepted the change. Wait for the rollout with kubectl rollout status --timeout, which fails if new pods don't become ready in time, then run a smoke test against the app.
If either fails, a step with if: failure() runs kubectl rollout undo, which returns the deployment to the previous version, and the pipeline still ends red so someone looks. You can practise this for free: the kind action creates a throwaway Kubernetes cluster inside the GitHub runner.
Example. SS Kaiser's Friday release shipped an image tag with a typo. The pipeline's rollout status check timed out after two minutes, the undo step restored the old version, and customers saw no errors; the team fixed the tag on Monday.
Before you start
You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 40 minutes.
Where to look
- Repository
- .github/workflows/deploy.yml
- Actions tab
Steps
- Add
.github/workflows/deploy.ymlfrom Commands. It creates a kind cluster, deploys version 1 and then the version you choose. - Run it from Actions with Run workflow and image
nginx:1.27. The rollout succeeds and the smoke test passes. - Run it again with a broken image such as
nginx:does-not-exist. Rollout status times out, the undo step runs, and the last step shows the deployment back on version 1.The run is still red, which is what you want. - Read the log for the undo step and the final
kubectl get deployoutput. - Add a readiness probe to the deployment so bad versions that start but don't serve are also caught.
Commands
name: deploy
on:
workflow_dispatch:
inputs:
image: { description: 'Image to deploy', default: 'nginx:1.27' }
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: helm/kind-action@v1
- run: |
kubectl create deployment web --image=nginx:1.26 --replicas=2
kubectl rollout status deploy/web --timeout=120s
- name: Deploy the new version
run: |
kubectl set image deploy/web nginx="${{ inputs.image }}"
kubectl rollout status deploy/web --timeout=90s
- name: Smoke test
run: kubectl run smoke --rm -i --restart=Never --image=busybox:1.36 -- wget -qO- -T 5 http://$(kubectl get pod -l app=web -o jsonpath='{.items[0].status.podIP}') | grep -q nginx
- name: Roll back
if: failure()
run: |
kubectl rollout undo deploy/web
kubectl rollout status deploy/web --timeout=90s
kubectl get deploy web -o wide
Watch out for
- Cost
- Free: kind runs inside the GitHub runner. On a real cluster, pair this with OIDC sign-in from the earlier task.
- Security
- On a real cluster, give the pipeline's identity rights to one namespace only.
- Common mistakes
- No timeout on rollout status (the job waits until GitHub's limit), and smoke tests that pass while the app is broken (check a real page, not just a port).
Clean up
Nothing to clean up: the kind cluster disappears with the runner.
Words to know
- Rollout status
- Waits until a deployment's new pods are ready, or fails after a timeout.
- Smoke test
- A quick check that the deployed app answers at all.
- Rollout undo
- Returns a deployment to its previous version.
- if: failure()
- Runs a step only when an earlier step failed.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands OnKeep going
- CI/CD roadmap: what to learn, in what orderExplained
- Test stages: what to test, and where in the pipelineExplained
- Release strategies: rolling, blue-green, canary and feature flagsExplained
- Reuse Azure Pipelines YAML with templatesPrevious in CI/CD in depth
- Final project: from commit to production with gates and rollbackNext in CI/CD in depth
- CI/CD and DevSecOpsAll pages in this topic