Interview Prep/Real-World Interview/Write-a-pipeline tasks

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.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-pipeline tasks