CI/CD Pipelines
Chapters 27–29 gave you three separate pieces: automated tests (Chapter 27), local git (Chapter 28), and pull requests hosted on GitHub (Chapter 29). This chapter wires them together so they run themselves. There's no new Python syntax here — the new language is YAML, and the new tool is GitHub Actions, which runs your pytest suite automatically, on GitHub's own servers, every time someone pushes or opens a PR, without anyone having to remember to run it by hand.
How to read this chapter
- Sub-topic 1 covers why CI/CD exists at all — the real failure modes it closes off.
- Sub-topic 2 covers the anatomy of a GitHub Actions workflow file —
on,jobs,steps, and the two most common building-block actions. - Sub-topic 3 builds one complete, working workflow that installs dependencies, lints with
ruff, and runspytest— the workflow this chapter's own exercises are built around. - Sub-topic 4 extends it with a build matrix (testing across several Python versions at once) and pip caching, so runs stay fast as a project grows.
- Sub-topic 5 covers how CI results actually show up on a pull request, and how branch protection turns a passing check from "nice to see" into a real merge gate.
- Sub-topic 6 zooms out to the wider pipeline — linting, type checking, security scanning, build artifacts, and deployment stages — that "CI/CD" covers beyond just running tests.
- A note on every YAML example in this chapter: every workflow file shown was parsed with Python's
yaml.safe_load()to confirm it's valid YAML before being written into this lesson — the same way Chapters 28–29 verified every git command by actually running it. Action names and versions (actions/checkout@v4,actions/setup-python@v5) are GitHub's real, current official actions.
pytest before pushing, because remembering to is easy to skip under a deadline. The PR gets reviewed on the diff alone, looks fine, and merges. Two days later, main is broken for everyone. This chapter is about removing "did someone remember to run the tests" from that story entirely — by having a machine run them, automatically, on every single push, whether anyone remembers or not.
1. Why CI/CD matters
- "Works on my machine" is a real, common failure — different installed package versions, different OS, different environment variables, all invisible until code runs somewhere else.
- Without automation, tests only get run when a human remembers to — and under time pressure, that's exactly when they get skipped.
- Manual deploys repeat the same class of mistake at a higher cost: a forgotten step, a stale build, a file deployed from the wrong branch.
"Works on my machine" is the most common version of this problem. It happens for reasons that have nothing to do with whether the code is actually correct:
- A different version of a dependency is installed locally than what's pinned in
requirements.txt. - A test quietly depends on a file, environment variable, or timezone that happens to be set up on one machine and not another.
- The contributor is on a different operating system than the one the project actually deploys to.
None of these are hypothetical — they're the ordinary, daily reality of more than one person touching the same codebase. The fix isn't "be more careful"; it's running every check in one identical, disposable environment, every time, regardless of whose machine the change came from.
Without CI/CD: With CI/CD:
write code write code
(maybe) run tests locally push
push |
open PR v
reviewed on the diff alone GitHub Actions runs the
merged full test suite automatically,
...bug discovered later, on a clean machine, in minutes
after it's already shared |
v
PR shows pass/fail BEFORE
a human ever has to guess
The same logic applies to deployment. A manual deploy is a human running a checklist by hand — build the project, copy the right files, restart the right service — and every one of those steps is a place a tired or rushed human can skip something or run it against the wrong branch. An automated deploy pipeline runs the exact same steps, in the exact same order, every time, with no step silently forgotten.
This is precisely the scenario from the hook: a contributor's local pytest run (Chapter 27) and their pushed branch (Chapter 28) are only useful to the team once something confirms they actually agree — and that confirmation needs to happen automatically, on GitHub, not rely on the contributor's memory or the reviewer's trust.
- CI/CD replaces "a human remembered to check" with "a machine checks automatically, every time."
- "Works on my machine" bugs come from environment differences invisible until code runs somewhere else, identically, every time.
- Manual deploys fail the same way manual testing does — a skipped or misordered step, not a code defect.
Go Deeper: is GitHub Actions the only CI/CD tool?
No — Jenkins, GitLab CI/CD, CircleCI, and Travis CI are all widely used alternatives, and the underlying ideas (trigger on a push, run in a clean environment, report pass/fail) are shared across all of them. GitHub Actions is covered here specifically because it's built directly into GitHub (Chapter 29's platform), needs no separate account or service, and is free for public repositories — making it the lowest-friction way to add real CI to a project you're already hosting there.
- "Works on my machine" bugs come from environment differences that only a clean, identical, automated environment reliably catches.
- Tests and deploy steps that only run when a human remembers eventually get skipped under pressure — automation removes that dependency.
- CI automatically builds/tests every change; CD extends that automation to packaging and shipping it.
2. GitHub Actions basics
- A GitHub Actions workflow is a YAML file in
.github/workflows/describing when to run, and what to do. ondefines the trigger;jobsdefines what runs; each job'sstepsdefine, in order, the individual commands or actions to execute.- A runner is the actual machine (a fresh virtual machine GitHub provisions per run) that executes a job's steps.
A workflow file lives at a specific, required path: .github/workflows/<anything>.yml. GitHub scans that folder in every repository and treats every .yml file inside it as a workflow definition. The simplest possible one is barely more than its trigger and one command:
name: Basic CI
on: push
jobs:
say-hello:
runs-on: ubuntu-latest
steps:
- name: Print a greeting
run: echo "Hello from GitHub Actions"
on), and what work to do when it fires (jobs). A repository can have many workflow files at once — one for tests, a separate one for deployment, another for a scheduled nightly task — each triggered independently.
Reading that file top to bottom: name is just a human-readable label, shown in GitHub's UI. on: push is the trigger — this workflow runs on every push, to any branch. Real projects are almost always more specific than that, scoping the trigger to particular branches and events:
on:
push:
branches: [main]
pull_request:
branches: [main]
This runs the workflow in two situations: a direct push to main, and any pull request whose target branch is main — which is exactly the moment a PR from Chapter 29 needs a pass/fail result attached to it, before anyone reviews the diff by eye.
Thought process: the decision that matters isn't YAML syntax, it's scoping the trigger correctly — running a workflow on every push to every branch wastes runner minutes on throwaway branches, while running it only on main misses the whole point of catching problems before they merge. Triggering on both a push to main and any PR targeting main is the standard shape almost every real project uses.
Write the prompt you'd give an AI assistant to build this:
Show a strong version of this prompt
Write just the `on:` trigger section of a GitHub Actions workflow YAML file. It should run the workflow in two cases: (1) a direct push to the `main` branch, and (2) any pull request that targets `main` as its base branch. Use the `branches:` filter under both `push` and `pull_request` so it doesn't fire on every branch. Show the YAML and briefly explain why scoping the trigger this way avoids wasting CI minutes on unrelated branches.
Inside jobs, each named entry (say-hello above) is one independent unit of work. runs-on picks the machine it executes on:
runs-on: ubuntu-latest is by far the most common choice — a fresh Ubuntu Linux VM, spun up from scratch for that one run and destroyed afterward, so nothing from a previous run (leftover files, cached state, environment drift) ever leaks in. GitHub also offers Windows (windows-latest) and macOS (macos-latest) runners for projects that need to test on those platforms specifically.
Each job gets its own fresh runner. That distinction matters once a workflow has more than one job:
needs, covered in Sub-topic 6) — which is why splitting unrelated work (say, linting and a security scan) into separate jobs can finish faster than cramming everything into one.
Finally, steps is an ordered list, and each step is one of exactly two things: a shell command (run), or a reusable, pre-built action (uses). The two actions almost every Python workflow starts with are actions/checkout and actions/setup-python:
name: Checkout and set up Python
on: push
jobs:
build:
runs-on: ubuntu-latest
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: Confirm Python is available
run: python --version
run: pytest) or invokes a reusable, versioned action published by GitHub or the community (uses: actions/checkout@v4), optionally passing it inputs via with. The @v4/@v5 suffix pins a specific major version of that action — the same reason Chapter 27's requirements.txt pins dependency versions, so a workflow doesn't silently break when an action publishes a breaking change.
A fresh runner starts completely empty — it does not automatically have your repository's code on it. actions/checkout@v4 is the step that clones your repository onto the runner (the same clone operation Chapter 29 covered, just run by GitHub instead of you). actions/setup-python@v5 then installs the requested Python version and puts it on the runner's PATH, so every step after it can run python and pip normally. Almost every job in this chapter starts with these same two steps, in this same order — checkout has to come first, since there's nothing to set up Python for otherwise.
- A workflow is a YAML file in
.github/workflows/, made of a trigger (on), one or morejobs, and each job's orderedsteps. - A runner is a fresh, disposable VM (usually
ubuntu-latest) that executes a job's steps; jobs run in parallel unless one depends on another. actions/checkout@v4clones your repo onto the runner;actions/setup-python@v5installs and configures Python — almost every Python workflow's first two steps.
Go Deeper: what other events can trigger a workflow besides push and pull_request?
Plenty — schedule (a cron expression, for a nightly or weekly run), workflow_dispatch (a manual "Run workflow" button in GitHub's UI, useful for deploys you want to trigger deliberately rather than automatically), release (when a GitHub release is published), and issues (when an issue from Chapter 29 is opened or labeled) are all common ones. A single workflow's on block can list several of these together, each with its own filters.
- A workflow file lives at
.github/workflows/*.ymland is built from a trigger, jobs, and each job's steps. - A runner is a fresh, disposable VM; a job is an ordered list of steps that run on one runner.
actions/checkout@v4thenactions/setup-python@v5is the standard opening pair of steps for a Python workflow.
3. Building a lint + test workflow
- A real CI workflow, put together from Sub-topic 2's pieces: check out the code, set up Python, install dependencies, lint, then test.
ruffis a fast, widely-used Python linter;pytest(Chapter 27) is the test runner.- The job fails — and blocks, once branch protection is set up in Sub-topic 5 — the moment any single step exits with a non-zero status.
Here's the complete, working workflow this chapter's own exercises are built around — nothing in it goes beyond what Sub-topic 2 already introduced:
name: Lint and Test
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
lint-and-test:
runs-on: ubuntu-latest
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
Two things worth noticing about the run steps. First, the | after run: in "Install dependencies" is plain YAML syntax for a multi-line string — each line becomes its own command, run in sequence, in the same shell. Second, the order of the last two steps is deliberate: lint before test. Linting is far faster than running a full test suite, so catching a trivial style or syntax issue there means the workflow fails fast, before spending runner time on tests that were never going to matter if the lint step was going to fail anyway.
ruff is a modern, extremely fast linter for Python (a single ruff check . scans an entire project in milliseconds); it's used here as the concrete example, but the same workflow shape applies with flake8 or any other linter — only the install line and the one run command change.
Running ruff check . against a file with an actual issue makes the pass/fail behavior concrete:
import os
import json
def load_config(path):
with open(path) as f:
return json.load(f)
app.py:1:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 fixable with the `--fix` option.
That non-zero exit status is exactly what makes the whole workflow useful: any step that fails stops the job right there and marks it red, and every subsequent step in that job is skipped. GitHub Actions doesn't need any special "did it pass" logic — it's using the same success/failure convention every command-line tool already follows, the same one that makes pytest's own exit code meaningful when you run it locally.
Thought process: the actual engineering decision in this workflow is step ordering and scope, not YAML mechanics — lint before test because it's cheaper and fails faster; one job, not several, because lint and test share the same installed dependencies and there's no benefit to paying for a second runner setup just to split them apart here (Sub-topic 6 shows when splitting into separate jobs does pay off).
Write the prompt you'd give an AI assistant to build this:
Show a strong version of this prompt
Write a GitHub Actions workflow file, .github/workflows/ci.yml, for a Python project. Requirements: (1) trigger on push to main and on any pull_request targeting main, (2) one job named lint-and-test running on ubuntu-latest, (3) steps in this exact order: actions/checkout@v4, actions/setup-python@v5 configured for Python 3.12, a step that upgrades pip and installs both requirements.txt and the packages ruff and pytest, a step that runs `ruff check .`, and a final step that runs `pytest`. Explain why lint should run before pytest in the step order, and confirm the YAML is valid by describing how you'd check it with yaml.safe_load() in Python.
- The standard shape: checkout, setup-python, install dependencies, lint, test — in that order, lint before test to fail fast and cheap.
ruff check .statically analyzes the project without running it; a non-zero exit status fails the step (and the job) exactly like any other command-line tool.- Any failed step stops the job immediately and skips everything after it — the same success/failure convention Chapter 27's
pytestexit code already relies on.
Go Deeper: why pip install ruff pytest instead of putting them in requirements.txt?
Many real projects do exactly that — but a common convention is keeping requirements.txt limited to what the application itself needs to run, and listing dev-only tools (linters, test frameworks, formatters) either directly in the workflow, or in a separate file like requirements-dev.txt installed only in CI. Either approach is valid; what matters is that whichever one a project picks, the workflow installs everything the lint and test steps actually need before running them.
- A complete lint + test workflow is just Sub-topic 2's building blocks in a deliberate order: checkout, setup-python, install, lint, test.
- Lint runs before test because it's faster and fails cheaper.
- A non-zero exit from any step fails that step, stops the job, and skips whatever comes after — the same convention as any other CLI tool's exit code.
4. Workflow matrix and caching
- A matrix strategy runs the same job multiple times, once per combination of values (e.g. one run per Python version) — in parallel, not one after another.
- This catches version-specific bugs Sub-topic 3's single-version workflow can't — "works on 3.12" isn't the same claim as "works on every Python version this project supports."
- Caching pip's downloaded packages between runs cuts install time, since the same dependencies rarely change from one run to the next.
Adding a matrix to Sub-topic 3's workflow needs exactly two additions: a strategy.matrix block, and a reference to that matrix's value wherever the single, hardcoded version used to be:
name: Lint and Test (matrix)
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
lint-and-test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- name: Check out the repository
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
- 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
${{ matrix.python-version }} is GitHub Actions' expression syntax — it substitutes in whichever value from the matrix list this particular parallel run is handling. With three versions listed, this one job definition produces three independent runs: lint-and-test (3.10), lint-and-test (3.11), lint-and-test (3.12), each on its own fresh runner, all starting at the same time:
jobs.lint-and-test (matrix: python-version)
|
+-- lint-and-test (3.10) -- own runner, runs independently
+-- lint-and-test (3.11) -- own runner, runs independently
+-- lint-and-test (3.12) -- own runner, runs independently
All three run in parallel -- total time is roughly the SLOWEST
one, not the sum of all three.
Thought process: the version list itself is the real decision, not the matrix syntax — it should match whatever a project actually claims to support (often documented in pyproject.toml's python_requires or a README), not an arbitrary "add a few versions." Listing versions the project doesn't actually support wastes CI time testing combinations no user will ever run.
Write the prompt you'd give an AI assistant to build this:
Show a strong version of this prompt
Here is my existing GitHub Actions workflow (paste the ci.yml from Sub-topic 3). Modify it to: (1) add a strategy.matrix block that runs the job against Python 3.10, 3.11, and 3.12, (2) reference the matrix value with ${{ matrix.python-version }} in both the job's setup-python step name and its python-version input, and (3) enable pip caching via actions/setup-python's built-in cache: "pip" option rather than a separate caching action. Explain what "each matrix combination runs on its own runner, in parallel" actually means for total workflow time versus running the three versions one after another.requirements.txt hasn't changed since yesterday. cache: "pip" on actions/setup-python@v5 is the simplest way to fix this: it automatically saves pip's download cache after a run and restores it at the start of the next one (keyed on a hash of your dependency file), so unchanged dependencies are pulled from the cache instead of re-downloaded from the internet.
The effect compounds with the matrix above: three parallel jobs each installing the same dependencies would otherwise mean three full, repeated downloads every single run; with caching enabled, only the very first run (or any run after requirements.txt changes) pays that full cost.
- A matrix runs one job multiple times with different substituted values, in parallel —
${{ matrix.python-version }}is the substitution syntax. - Total time for a matrixed job is roughly its slowest single combination, not the sum of all of them, since they run at the same time.
cache: "pip"onactions/setup-python@v5saves and restores pip's download cache between runs, keyed on your dependency file's contents.
Go Deeper: what invalidates the pip cache?
By default, actions/setup-python's built-in caching keys the cache to a hash of your dependency file (requirements.txt, or pyproject.toml/poetry.lock for other setups). Change one character in requirements.txt — bump a version pin, add a new package — and the hash changes, so the next run gets a fresh cache miss and rebuilds it once. This is exactly the right invalidation rule: dependencies genuinely did change, so a stale cache would be actively wrong to reuse.
- A matrix strategy tests every version combination as its own parallel run, from one job definition.
- Matrixed runs happen in parallel — total time tracks the slowest one, not the sum.
- Pip caching (
cache: "pip") skips re-downloading unchanged dependencies, keyed on your dependency file's hash.
5. Status checks and branch protection
- Every workflow run against a PR shows up directly on that PR as a status check — pass, fail, or still running.
- By default, a status check is purely informational — a red X doesn't stop anyone from merging.
- Branch protection with required status checks is what actually turns a passing workflow into a hard requirement before a merge button becomes clickable.
Once a workflow like Sub-topic 3's is committed to .github/workflows/, GitHub runs it automatically on every matching push and PR — no extra setup needed to make it run. What it looks like on a real pull request:
A stand-in for GitHub's real PR "Checks" section, described in prose/structure since it can't be captured as a real screenshot here.
Pull Request #57 — Add shopping list feature
Some checks haven't completed yet
1 pending, 0 failing, 2 successful checks
✓ lint-and-test (3.10) 38s
✓ lint-and-test (3.11) 41s
⏳ lint-and-test (3.12) running...
[ Merge pull request ] (still clickable -- not required yet)
lint-and-test (3.10), lint-and-test (3.11), etc.), so a reviewer can see exactly which Python version, if any, actually failed — not just one combined red/green result.
Notice the mockup's merge button: still clickable, even with a check pending. That's the default, and it's a real gap — a red X or a still-running check is only useful information if someone is required to wait for it. Branch protection is the GitHub repository setting that closes that gap:
main) that GitHub enforces regardless of who's pushing or merging. Required status checks is one such rule: it names specific checks (like lint-and-test (3.12)) that must report success before the merge button becomes clickable at all — turning Sub-topic 3's workflow from a report into an actual gate.
A stand-in for GitHub's real branch protection settings page and its effect on a PR, described in prose/structure.
Settings > Branches > Branch protection rule for "main"
[x] Require status checks to pass before merging
[x] Require branches to be up to date before merging
Status checks that are required:
[x] lint-and-test (3.10)
[x] lint-and-test (3.11)
[x] lint-and-test (3.12)
---------------------------------------------------------
Pull Request #58 — Fix off-by-one in total()
✗ lint-and-test (3.11) FAILED — 1 test failed
Merge blocked
Required status check "lint-and-test (3.11)" is failing.
[ Merge pull request ] (button disabled, greyed out)
Thought process: the judgment call in branch protection isn't "should I turn it on" (almost always yes, for any shared branch) — it's exactly which checks to mark required. Marking a flaky or still-experimental check as required blocks every future merge on something unreliable; the right default is requiring only checks that are both stable and genuinely meaningful, like every matrix combination of the lint-and-test job.
Write the prompt you'd give an AI assistant to help with this:
Show a strong version of this prompt
My repository has a GitHub Actions workflow, lint-and-test, that runs as a matrix across Python 3.10, 3.11, and 3.12 -- so it reports three separate status checks per PR (lint-and-test (3.10), (3.11), (3.12)). Walk me through the exact steps in GitHub's Settings > Branches to add a branch protection rule for main that: (1) requires all three of those specific checks to pass before the merge button is enabled, and (2) requires the branch to be up to date with main before merging. Explain what "require branches to be up to date" actually prevents that requiring the checks alone doesn't.
"Require branches to be up to date before merging" is worth calling out specifically: it forces a PR branch to have the latest main merged or rebased in (Chapter 28's territory again) before its checks count, closing one more real gap — a PR that passed CI a week ago, before main changed underneath it, isn't proof it still passes today.
- Every workflow run against a PR shows up as a status check — per matrix combination, attached to the exact commit it ran against.
- A status check alone is informational only — nothing stops a merge unless branch protection says otherwise.
- Required status checks under branch protection is what actually blocks the merge button until the named checks pass.
Go Deeper: can an admin bypass a required status check?
By default, repository admins can still merge past a failing required check — branch protection has a separate "Do not allow bypassing the above settings" option that closes even that door, applying the rule to everyone including admins. Most real teams leave admins able to bypass in a genuine emergency, but treat doing so as rare and notable, not routine — if bypassing a required check becomes normal, the check has effectively stopped meaning anything.
- A status check is a workflow run's pass/fail result, shown directly on the PR that triggered it.
- Status checks alone don't block anything — branch protection's "required status checks" is what does.
- "Require branches to be up to date" closes the gap where a PR passed CI before
mainchanged underneath it.
6. Beyond tests: the wider pipeline
- "CI/CD" covers far more than running pytest — type checking, security scanning, building distributable artifacts, and deploying are all common pipeline stages.
- Independent checks (lint/test, type checking, security scanning) are usually split into separate parallel jobs; a build or deploy stage that depends on all of them uses
needsto wait until they succeed. - This sub-topic is conceptual — each stage is a real, runnable pattern, but going deep on any one of them (a full deployment pipeline, a specific security scanner) is beyond this chapter's scope.
Beyond lint and test, four more stages show up constantly in real pipelines:
- Type checking — tools like
mypystatically check type hints for consistency, catching a class of bug neither a linter nor a test necessarily will (a function called with the wrong argument type, but never actually exercised by a test with that specific bad input). - Security scanning — tools like
pip-auditcheck installed dependencies against known vulnerability databases, flagging a package version with a published CVE before it ships. - Build artifacts — packaging the project into its distributable form (a wheel, a Docker image, a zip) and uploading it so later stages, or a human, can retrieve exactly what was built from this specific commit.
- Deployment stages — taking a build artifact that passed every prior stage and actually shipping it, often to a staging environment first, then production, frequently gated behind a manual approval step.
Put together, with lint-and-test's matrix from Sub-topic 4 alongside three new jobs:
name: Full Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
lint-and-test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
- run: pip install -r requirements.txt
- run: ruff check .
- run: pytest
type-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install mypy -r requirements.txt
- run: mypy .
security-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install pip-audit
- run: pip-audit -r requirements.txt
build:
needs: [lint-and-test, type-check, security-scan]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build distribution
run: python -m build
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
needs?
needs makes a job wait for one or more other jobs to succeed before it starts, instead of running immediately in parallel with everything else. Here, build lists all three earlier jobs in its needs — it only runs once lint-and-test (all three matrix combinations), type-check, and security-scan have all reported success. If any of them fails, build is skipped entirely, so a broken or vulnerable commit never gets packaged in the first place.
lint-and-test (3.10) \
lint-and-test (3.11) |-- all run in PARALLEL, independent of
lint-and-test (3.12) / each other and of the two jobs below
type-check |
security-scan /
|
| needs: [lint-and-test, type-check, security-scan]
v
build -- only runs if EVERY job above succeeded
actions/upload-artifact@v4 is the last new piece: it saves whatever's in the given path (here, dist/, where Python's standard build tool puts a built wheel and source distribution) as a downloadable artifact attached to that specific workflow run — retrievable later from GitHub's UI, or by a following deployment job. A real deployment stage would typically follow this same needs pattern one level further: a deploy job that needs: [build], downloads the artifact, and ships it — often restricted to only run on pushes to main (not every PR) via an if condition, and frequently requiring a manual approval through GitHub's environments feature before it's allowed to touch production.
Thought process: the real design decision across a multi-job pipeline is what runs in parallel versus what's gated — lint/type-check/security-scan share no dependency on each other's results, so there's no reason to force them to run in sequence and waste time; build genuinely does depend on all three passing, so it's the one place `needs` belongs. Getting this wrong either wastes CI time (unnecessary sequencing) or wastes compute on doomed work (building an artifact from code that fails its own tests).
Write the prompt you'd give an AI assistant to build this:
Show a strong version of this prompt
Extend my existing matrix lint-and-test workflow (paste the Sub-topic 4 YAML) into a full pipeline with these additional jobs, all in the same workflow file: (1) a type-check job on ubuntu-latest with Python 3.12 that installs mypy plus requirements.txt and runs `mypy .`, (2) a security-scan job that installs pip-audit and runs `pip-audit -r requirements.txt`, (3) a build job that uses `needs: [lint-and-test, type-check, security-scan]` so it only runs after all three succeed, checks out the repo, runs `python -m build`, and uploads the resulting dist/ folder using actions/upload-artifact@v4. Explain why lint-and-test, type-check, and security-scan should run in parallel rather than one after another, and why build specifically needs the `needs` key when the others don't.
- Type checking (
mypy) and security scanning (pip-audit) are common pipeline stages beyond lint + test, each catching a different class of problem. - Independent checks run as separate parallel jobs;
needsgates a later job (like a build) on all of them succeeding first. actions/upload-artifact@v4saves a built package from one job, retrievable later or by a downstream deploy job — the connecting piece between "tests passed" and "this exact, verified build gets shipped."
Go Deeper: what's the difference between Continuous Delivery and Continuous Deployment?
Both build on the same automated pipeline; they differ only in the very last step. Continuous Delivery means every change that passes the pipeline is automatically packaged and ready to ship, but a human still clicks the button that actually releases it (matching the manual-approval environment mentioned above). Continuous Deployment goes one step further and removes even that click — a change that passes every stage ships to production automatically, with no human gate at all. Most teams start with delivery and only move to full deployment once they trust their pipeline's coverage enough to remove the last manual checkpoint.
- Real pipelines extend lint + test with type checking, security scanning, build artifacts, and deployment stages.
- Independent stages run as parallel jobs;
needsgates a job (like a build) on one or more others succeeding first. - Continuous Delivery automates everything up to a human-approved release; Continuous Deployment removes that last manual step too.
- Virtually every professional software team with a shared repository runs some form of this chapter's pipeline — a red status check on a PR blocking merge is one of the most common daily experiences of working on a real codebase.
- The matrix strategy from Sub-topic 4 is exactly how open-source libraries credibly claim to "support Python 3.10–3.12" — not as an unverified README claim, but as a badge backed by a workflow that actually runs the full suite on each version, every single commit.
- The needs-gated pipeline from Sub-topic 6 (test, type-check, security-scan, then build, then deploy) is the same fundamental shape used at companies of every size, from a single-maintainer open-source project to teams shipping to millions of users — only the specific tools and the number of stages change.
Points to Remember
- CI/CD replaces "a human remembered to run this" with a machine running it automatically, on every push, in a clean and identical environment.
- A GitHub Actions workflow is a YAML file in
.github/workflows/: a trigger (on), one or morejobs, and each job's orderedsteps, executed on a fresh, disposable runner. - A real lint + test workflow chains
actions/checkout@v4,actions/setup-python@v5, dependency install, a linter (ruff), andpytest— lint before test, to fail fast and cheap. - A
strategy.matrixruns a job once per listed value (e.g. every supported Python version) in parallel;cache: "pip"onactions/setup-python@v5skips re-downloading unchanged dependencies. - Every workflow run shows up on its PR as a status check, but only blocks merging once branch protection marks it as a required status check.
- Real pipelines extend beyond tests to type checking, security scanning, build artifacts, and deployment, with
needsgating later stages on earlier ones succeeding — Continuous Delivery automates up to a human release click; Continuous Deployment removes even that.