Task 2: The Pipeline, Empty
In this task you create three files in .github/workflows/, one workflow per event:
| File | Runs on | Its part |
|---|---|---|
branch.yml | a push to a topic branch | the developer's own loop: fast feedback while they work; it blocks nothing |
pr.yml | a pull request to main, and every new commit on it | the gate: its checks and the approval decide whether a change reaches main |
merge.yml | a merge into main | delivery: what passed the gate becomes an image and is deployed |
- The pull request workflow is the one gate. The branch workflow runs whether or not a pull request ever follows.
- Nobody pushes to
mainonce Task 3 protects it: the only writes tomainare merges of approved pull requests. GitHub records a merge as a push, so the merge workflow sayson: pushtomain. - The merge workflow reruns no tests: the pull request already tested the merged code. It checks only that the deployment works.
None of them does real work yet:
- Every job just prints what it will do.
- You learn which event runs which jobs, and in what order, while the jobs still take seconds.
- Later tasks replace one placeholder at a time with real work.
Refresh your workflow knowledge: GitHub Actions Workflows: the Structure of the File.
Create the topic branch
Open an issue, "Add the empty pipeline". GitHub numbers it: here it is #3.
Create its branch, named as The Plan names branches, and the directory GitHub reads workflows from:
1$ git switch main 2$ git pull 3$ git switch -c ci/3-empty-pipeline 4$ mkdir -p .github/workflows
Write the branch workflow
.github/workflows/branch.yml:
1name: Branch
2
3on:
4 push:
5 branches-ignore: [main]
6
7jobs:
8 lint:
9 runs-on: ubuntu-latest
10 steps:
11 - run: echo "lint - Task 7 runs ruff here"
12
13 unit-tests:
14 runs-on: ubuntu-latest
15 steps:
16 - run: echo "unit-tests - Task 7 runs pytest here"
branches-ignore: [main]: a push to any branch butmain. A push tomainbelongs to the merge workflow.- No
needs:: the two jobs start together, each on a runner of its own.
Write the pull request workflow
.github/workflows/pr.yml:
1name: Pull request
2
3on:
4 pull_request:
5 branches: [main]
6
7jobs:
8 lint:
9 runs-on: ubuntu-latest
10 steps:
11 - run: echo "lint - Task 7 runs ruff here"
12
13 unit-tests:
14 runs-on: ubuntu-latest
15 steps:
16 - run: echo "unit-tests - Task 7 runs pytest here"
17
18 image-build:
19 needs: [lint, unit-tests]
20 runs-on: ubuntu-latest
21 steps:
22 - run: echo "image-build - Task 4 builds the image here"
23
24 integration-tests:
25 needs: image-build
26 runs-on: ubuntu-latest
27 steps:
28 - run: echo "integration-tests - Task 8 runs the image against a database here"
29
30 security:
31 needs: image-build
32 runs-on: ubuntu-latest
33 steps:
34 - run: echo "security - Task 9 scans the code and the image here"
on: pull_requesttomain: the pull request's opening, and every push to its branch after it.needs:draws a graph of three columns:lintandunit-testsstart together;image-buildwaits for both: no image is built from code that fails them;integration-testsandsecuritywait for the image, since both use it, and then run together.
- A job whose
needs:failed is skipped.
Write the merge workflow
.github/workflows/merge.yml:
1name: Merge
2
3on:
4 push:
5 branches: [main]
6
7jobs:
8 image-push:
9 runs-on: ubuntu-latest
10 steps:
11 - run: echo "image-push - Task 4 builds the image and pushes it here"
12
13 deploy:
14 needs: image-push
15 runs-on: ubuntu-latest
16 steps:
17 - run: echo "deploy - Task 6 deploys the image here"
18
19 smoke-test:
20 needs: deploy
21 runs-on: ubuntu-latest
22 steps:
23 - run: echo "smoke-test - Task 6 calls the deployed app here"
on: pushtomain: GitHub records a merge as a push, so this is the merge's event.- The jobs are one chain:
image-pushbuilds the image from the merged commit and tags it with its SHA;deployputs that image on AWS;smoke-testcalls the deployed app:GET /healthz, then one book created, read and deleted.
Push the branch, and read its run
This is the heart of the task: a push, and the run it starts.
Commit the three files, and push the branch:
1$ git add .github/workflows/branch.yml .github/workflows/pr.yml .github/workflows/merge.yml 2$ git commit -m "Add the empty pipeline" 3$ git push -u origin ci/3-empty-pipelineOpen the repository's Actions tab:
- one run is in the list,
Add the empty pipeline, under the workflow Branch, with the branch's name beside it; - open it: the two jobs,
lintandunit-tests, side by side, since neither needs the other; - open a job: its steps, and under
Run echo …the line it printed.
- one run is in the list,
- GitHub reads the workflow files of the commit you pushed. It checks each file's
on:against the event:branch.ymlmatches a push to a branch that is notmain, so it runs;pr.ymlwaits for a pull request, andmerge.ymlfor a push tomain, so neither runs.
- Each job ran on a fresh runner, a machine GitHub started for that job alone and deleted after it.
- Push another commit to the branch: another run of Branch, and nothing else.
Open a pull request, and read its checks
- On GitHub, open a pull request from
ci/3-empty-pipelinetomain, withCloses #3in its description. - Its Checks tab, and the Actions tab, show a run of Pull request:
- the five jobs, each a check on the pull request;
- the graph:
lintandunit-tests, thenimage-build, thenintegration-testsandsecurity.
- The run tests the code as
mainwould hold it after the merge: GitHub merges the branch intomainon a trial basis, and checks that commit out. - Leave the pull request open, and do not press Merge: the next step brings its commits to
main.
Push to main, and read the merge run
In the finished pipeline, merge.yml runs when an approved pull request is merged; the approval arrives in
Task 3. A push to main is the same event, so here you merge on your own machine, and push.
Merge the branch into
main, and pushmain:1$ git switch main 2$ git merge ci/3-empty-pipeline 3$ git pushThe Actions tab shows a run of Merge:
- its three jobs in a chain:
image-push, thendeploy, thensmoke-test; - each starts only once the one before has passed.
- its three jobs in a chain:
branch.ymlignoresmain, and no pull request changed, so the merge workflow is the only one to run.- The pull request now shows as merged: GitHub sees its commits on
main. - From Task 3 on,
mainrefuses this push, and the merge button is the only way in.