Write a pipeline step that runs a smoke test after deploy and rolls back on failure.
AdvancedHands-onWrite-a-pipeline tasks
Answer
- name: Deploy
run: |
kubectl -n prod set image deploy/api api=myrepo/api:${{ github.sha }}
kubectl -n prod rollout status deploy/api --timeout=300s
- name: Smoke test
id: smoke
run: |
for path in /health /api/v1/status; do
code=$(curl -s -o /dev/null -w '%{http_code}' "https://api.example.com${path}")
echo "$path -> $code"; [ "$code" = "200" ] || exit 1
done
- name: Roll back
if: failure()
run: kubectl -n prod rollout undo deploy/api && kubectl -n prod rollout status deploy/api
Key trick → rollout status exits non-zero if the rollout doesn't finish within the timeout, and if: failure() runs the rollback when any earlier step fails, whether that is the deploy or the smoke test.
More on Write-a-pipeline tasks
Write a GitHub Actions workflow that runs tests on every PR and push to main.BeginnerWrite a workflow that builds a Docker image and pushes it to ECR using OIDC (no access keys).IntermediateWrite a matrix job that tests on Python 3.10, 3.11 and 3.12 on Linux and Windows.BeginnerWrite a declarative Jenkinsfile with build, test, Docker push and a manual approval before prod.AdvancedWrite a GitHub Actions deploy job that requires approval for production using environments.BeginnerWrite a reusable workflow and call it from another repo.Intermediate