Exercises — Module 6: CI/CD
Environment: your GitHub repo with the Module 5 app, one EC2/VM as deploy target, Docker Hub (or GHCR). Jenkins runs in Docker locally. These exercises build ONE evolving pipeline — commit everything; the repo becomes your portfolio centerpiece.
Part A — Warm-Up Drills
Drill 1 — Hello workflow. Create .github/workflows/hello.yml triggered on push that echoes the branch name, commit SHA, and actor using ${{ github.* }} contexts. Push, watch it run in the Actions tab, read every log line.
✓ You can find: trigger event, runner OS, each step's duration.
Drill 2 — Fail on purpose. Add a step exit 1 mid-workflow. Observe: do later steps run? Does the job show red? Now wrap it with continue-on-error: true — what changes? Remove it after.
✓ You can explain job status vs step status.
Drill 3 — Secrets hygiene. Add a repo secret TEST_SECRET=hunter2. Print it in a step — observe the masking (***). Then try echo ${{ secrets.TEST_SECRET }} | base64 — masked or leaked? Write the one-line moral about "masking is not protection."
Drill 4 — Matrix taste. Run your test step across a matrix of two Python versions in parallel. Find where the parallel jobs show in the UI. ✓ Both legs green; total time ≈ single-leg time.
Drill 5 — Trigger zoo. Make one workflow each triggered by: pull_request, push to main only, push tags v*, workflow_dispatch (manual button), schedule (cron — pick a time 10 min ahead and watch it fire).
✓ You can pick the right trigger for: CI checks / deploy / nightly job / manual hotfix.
Part B — Guided Labs
Lab 1 — The CI Quality Gate (2 h)
On your Module 5 app repo:
- Workflow
ci.ymlonpull_request: checkout → setup-python → cached pip install →ruff check→pytest --cov --cov-fail-under=70. - Write 3 real tests for your app first (health endpoint, counter logic, one failure case).
- Add jobs (parallel):
hadolinton the Dockerfile,shellcheckon any scripts,docker build(no push) to prove buildability. - Enable branch protection: PRs required, CI checks required.
- Prove the gate: open a PR with (a) a lint error, (b) a failing test, (c) coverage below 70 — watch each get blocked; fix each. ✓ Three screenshots/notes of blocked PRs. This IS continuous integration.
Lab 2 — Security Scanning (60 min)
- Add CodeQL workflow (default template). Then introduce a deliberately vulnerable endpoint: SQL string concatenation from a query param. PR it. ✓ CodeQL annotates the exact line. Fix with parameterization; watch it clear.
- Add a Trivy job scanning your built image, failing on HIGH/CRITICAL. If it fails on the base image, fix by bumping the base tag — experience "the fix is one FROM line."
- Add
gitleaks(or GitHub secret scanning awareness): commit a fake AWS-shaped keyAKIA...in a test branch and watch it get flagged.
Lab 3 — CD to a Real Server (2 h)
- Prepare the target VM (Module 2 skills):
deployuser, docker installed, an.envfile at/opt/app/.env. - Generate a dedicated SSH keypair for CI; public key → server, private key → repo secret. (Note why you didn't reuse your personal key.)
- Workflow
deploy.ymlon tagv*: build+push image (tagged with the git tag) → SSH step: pull, stop old, run new, curl/health— fail the job if unhealthy. - Add a GitHub environment
productionrequiring your manual approval; attach it to the deploy job. Tagv0.1.0, approve, watch it ship. Curl the live server. ✓ Merge→tag→approve→live with zero manual server commands. - Rollback drill: ship
v0.2.0with a broken/health; watch deploy fail red; redeploy by re-running the pipeline onv0.1.0. Time your MTTR. Then do thegit revert+v0.2.1roll-forward variant and compare.
Lab 4 — Load Balancer + Zero-Downtime Taste (90 min)
- On the VM: run TWO instances of your app (ports 8081/8082) + nginx upstream round-robin (Module 6 §5 config). Curl repeatedly — see alternating hostnames.
- Benchmark baseline:
ab -n 2000 -c 50— record req/s and p95 (the-e/percentile output). - Manual rolling deploy: pull new image → restart instance 1 → health check → restart instance 2, while
while true; do curl -s ...; sleep 0.2; doneruns in another terminal. ✓ Zero failed requests in your curl loop during the deploy. If not — where's the gap? (nginx health checks / drain time) - Script the rolling deploy in bash (Module 3 boss fight, evolved) and wire it into the CD workflow replacing the single-container deploy.
Lab 5 — Jenkins Parallel Universe (2 h)
- Run Jenkins in Docker (mount a volume for JENKINS_HOME; mount docker.sock or install docker CLI in an agent — note the security caveat of the sock).
- Initial setup, install Git/Pipeline/SSH plugins. Add credentials: Docker Hub (username/password) and the deploy SSH key — into the credential store.
- Create a Pipeline job pointed at your repo's
Jenkinsfile: stages Test → Build → Push → Deploy(when main), post{failure} echo-notification. - Trigger by polling or a webhook (ngrok/localtunnel if local). Break a test; watch the stage view go red at the right stage.
- Write a 10-line comparison note: same pipeline, Actions vs Jenkins — setup effort, secrets handling, UI, where each wins. ✓ The note — you'll reuse it verbatim in interviews.
Lab 6 — Ansible Provisioning (2 h)
- Fresh VM (or wipe one). Inventory +
ansible all -m pingworking via SSH key. - Playbook
site.yml: create deploy user + authorized key, harden sshd (template + handler restart), install docker, deploy nginx reverse-proxy config from a Jinja2 template using variables (app_port). - Run with
--checkfirst — read the predicted changes. Apply. Run again:changed=0. ✓ Idempotency witnessed, not just believed. - Change
app_portvariable, re-run — ONLY the template task changes and ONLY the handler fires. Explain why. - Refactor into a role (
roles/webserver/). Bonus: encrypt a variable withansible-vaultand use it. - Final integration: a GitHub Actions job that runs this playbook against the VM (ansible in the runner, SSH key from secrets) — infrastructure config now goes through PRs too.
Part C — Challenges
C1 — PR annotations. Make test failures and lint errors appear as inline PR annotations (most setup-actions do this; verify by breaking a specific line).
C2 — Monorepo paths. Split your repo into api/ and frontend/ (fake frontend fine); make each CI job run ONLY when its path changed (paths: filters). Prove with two targeted PRs.
C3 — Reusable workflow. Extract build-and-push into a reusable workflow (workflow_call) with inputs (image name) and call it from two workflows.
C4 — Notify like production. Slack (or Discord) webhook message on deploy success/failure including version, actor, and a link to the run — via a tiny script step, not a marketplace action, so you understand the payload.
C5 — Cost/time golf. Get total CI time under half its current duration using: caching, parallel jobs, path filters, and cancel-in-progress concurrency. Record before/after times.
C6 — Blue/green on one box. Two compose projects app_blue/app_green + nginx switching between them via one symlinked config + reload. Script switch.sh green. Deploy new version to idle color, smoke test, switch, keep old as instant rollback. Wire into CD.
Part D — Break & Fix Scenarios
D1 — The flaky test. Add a test that fails ~30% of runs (random). Experience the team pain across 5 CI runs; then handle it properly: quarantine/fix, and write two sentences on why retries-as-policy rot pipelines.
D2 — Secret rotation fire drill. "Your Docker Hub token leaked." Rotate: revoke, create new, update secret, verify pipeline green — time it. Then answer: what if it had been the SSH deploy key?
D3 — Broken main. Someone (you) pushed directly to main bypassing protection (admin override) and broke the build. Fix forward under time pressure, then tighten settings so admins can't bypass.
D4 — Deploy hangs. Make the SSH deploy step hang (e.g., a prompt waiting for input). Watch the job sit. Add timeout-minutes at job and step level; re-run; document the default you'll now always set.
D5 — Ansible drift. Hand-edit the nginx config on the server ("hotfix"). Run the playbook — watch it stomp your edit. Write the moral connecting this to GitOps (Module 12 foreshadowing).
Part E — Interview Rehearsal (out loud)
- Design a CI/CD pipeline for a containerized web app, from PR to production. Whiteboard it.
- What quality gates do you run and in what order? Why that order?
- Deploy went out, error rates spiked. Your rollback options, trade-offs, and which you'd pick?
- Blue/green vs rolling vs canary — pick one per scenario I give you.
- How do you keep secrets out of pipelines and images? What's OIDC buying you?
- Jenkins vs GitHub Actions — when would you choose each? (Use your Lab 5 note.)
- What makes Ansible idempotent, and what are handlers for?
Done when
the blocked-PR trilogy from Lab 1 exists, Lab 3's rollback MTTR is recorded, Lab 4 achieved zero dropped requests, Lab 6 hit changed=0, and C6's blue/green switch works from the pipeline.
