🐍 Python for Everyone

← Back to the quiz

Chapter 30 Exercises 40 min, hands-on

Exercises: CI/CD Pipelines

How to run these These exercises validate real GitHub Actions workflow YAML using Python's 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.
What starter.py builds

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:

.github/workflows/ci-broken.yml
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:

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.

← Back to Quiz Practice Bank →