Skip to content
devops.academy

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:

  1. Workflow ci.yml on pull_request: checkout → setup-python → cached pip install → ruff check → pytest --cov --cov-fail-under=70.
  2. Write 3 real tests for your app first (health endpoint, counter logic, one failure case).
  3. Add jobs (parallel): hadolint on the Dockerfile, shellcheck on any scripts, docker build (no push) to prove buildability.
  4. Enable branch protection: PRs required, CI checks required.
  5. 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)

  1. 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.
  2. 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."
  3. Add gitleaks (or GitHub secret scanning awareness): commit a fake AWS-shaped key AKIA... in a test branch and watch it get flagged.

Lab 3 — CD to a Real Server (2 h)

  1. Prepare the target VM (Module 2 skills): deploy user, docker installed, an .env file at /opt/app/.env.
  2. Generate a dedicated SSH keypair for CI; public key → server, private key → repo secret. (Note why you didn't reuse your personal key.)
  3. Workflow deploy.yml on tag v*: build+push image (tagged with the git tag) → SSH step: pull, stop old, run new, curl /health — fail the job if unhealthy.
  4. Add a GitHub environment production requiring your manual approval; attach it to the deploy job. Tag v0.1.0, approve, watch it ship. Curl the live server. ✓ Merge→tag→approve→live with zero manual server commands.
  5. Rollback drill: ship v0.2.0 with a broken /health; watch deploy fail red; redeploy by re-running the pipeline on v0.1.0. Time your MTTR. Then do the git revert + v0.2.1 roll-forward variant and compare.

Lab 4 — Load Balancer + Zero-Downtime Taste (90 min)

  1. 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.
  2. Benchmark baseline: ab -n 2000 -c 50 — record req/s and p95 (the -e/percentile output).
  3. Manual rolling deploy: pull new image → restart instance 1 → health check → restart instance 2, while while true; do curl -s ...; sleep 0.2; done runs in another terminal. ✓ Zero failed requests in your curl loop during the deploy. If not — where's the gap? (nginx health checks / drain time)
  4. 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)

  1. 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).
  2. Initial setup, install Git/Pipeline/SSH plugins. Add credentials: Docker Hub (username/password) and the deploy SSH key — into the credential store.
  3. Create a Pipeline job pointed at your repo's Jenkinsfile: stages Test → Build → Push → Deploy(when main), post{failure} echo-notification.
  4. Trigger by polling or a webhook (ngrok/localtunnel if local). Break a test; watch the stage view go red at the right stage.
  5. 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)

  1. Fresh VM (or wipe one). Inventory + ansible all -m ping working via SSH key.
  2. 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).
  3. Run with --check first — read the predicted changes. Apply. Run again: changed=0. ✓ Idempotency witnessed, not just believed.
  4. Change app_port variable, re-run — ONLY the template task changes and ONLY the handler fires. Explain why.
  5. Refactor into a role (roles/webserver/). Bonus: encrypt a variable with ansible-vault and use it.
  6. 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)

  1. Design a CI/CD pipeline for a containerized web app, from PR to production. Whiteboard it.
  2. What quality gates do you run and in what order? Why that order?
  3. Deploy went out, error rates spiked. Your rollback options, trade-offs, and which you'd pick?
  4. Blue/green vs rolling vs canary — pick one per scenario I give you.
  5. How do you keep secrets out of pipelines and images? What's OIDC buying you?
  6. Jenkins vs GitHub Actions — when would you choose each? (Use your Lab 5 note.)
  7. 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.