Exercises: CI/CD Pipelines
yaml module (pip install pyyaml
if you don't already have it). Download starter.py
below, run python3 starter.py from the folder you
saved it in, and it builds a real scenario/ folder on
disk: a tiny sample project with its own
.github/workflows/ folder, containing one valid
workflow and one intentionally broken one.
This is a description of the real folder structure starter.py creates on disk -- not a GitHub screenshot.
scenario/
└── sample_project/
├── requirements.txt
├── app.py
├── test_app.py
└── .github/
└── workflows/
├── ci.yml <- a real, VALID lint + test workflow
└── ci-broken.yml <- Task 4's broken workflow
1 Parse a workflow file for real
Use the already-implemented load_workflow() in starter.py to parse scenario/sample_project/.github/workflows/ci.yml with yaml.safe_load(). Confirm it comes back as a normal Python dict, and that workflow["jobs"] contains one job: lint-and-test.
2 Validate a workflow's structure
Fill in validate_workflow_structure(workflow) (TODO 1). Given an already-parsed workflow dict, return a list of problem strings for anything structurally wrong: a missing on key, a missing or empty jobs dict, or any job missing runs-on or a non-empty steps list. An empty list means the workflow is structurally valid.
3 Combine parsing and validation
Fill in validate_workflow_file(path) (TODO 2): load the file, catch a real YAML syntax error with yaml.YAMLError instead of crashing, then run validate_workflow_structure() and return (is_valid, problems). Run it against ci.yml — it should report (True, []).
🐞 Task 4 — Debug the Code: a workflow with a missing key
ci-broken.yml looks almost identical to the valid
ci.yml — same trigger, same job name, same steps —
but one required key is missing from its one job:
jobs:
lint-and-test:
steps:
- name: Check out the repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install ruff pytest
- name: Lint with ruff
run: ruff check .
- name: Run tests with pytest
run: pytest
Diagnose it, then fix it for real:
- Read it against Sub-topic 2 of the lesson: every job needs both
runs-on(which runner it executes on) andsteps. Compare this job toci.yml's and spot exactly what's missing. - Run your
validate_workflow_file()from Task 3 against this file — it should report the exact missing key, by name, not just "something's wrong." - Fix the file by hand (add
runs-on: ubuntu-latestback in, indented as a sibling ofsteps) and re-validate — it should now report(True, []).
This file's bug — a job with no runs-on — is exactly the kind of mistake GitHub Actions itself rejects before a workflow ever runs, not a hypothetical: run python3 solution.py to see the working validator catch it end to end, with the real problem message printed.