Task 2: The Pipeline, Empty

In this task you create three files in .github/workflows/, one workflow per event:

FileRuns onIts part
branch.ymla push to a topic branchthe developer's own loop: fast feedback while they work; it blocks nothing
pr.ymla pull request to main, and every new commit on itthe gate: its checks and the approval decide whether a change reaches main
merge.ymla merge into maindelivery: 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 main once Task 3 protects it: the only writes to main are merges of approved pull requests. GitHub records a merge as a push, so the merge workflow says on: push to main.
  • 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

  1. Open an issue, "Add the empty pipeline". GitHub numbers it: here it is #3.

  2. 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 but main. A push to main belongs 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_request to main: the pull request's opening, and every push to its branch after it.
  • needs: draws a graph of three columns:
    • lint and unit-tests start together;
    • image-build waits for both: no image is built from code that fails them;
    • integration-tests and security wait 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: push to main: GitHub records a merge as a push, so this is the merge's event.
  • The jobs are one chain:
    • image-push builds the image from the merged commit and tags it with its SHA;
    • deploy puts that image on AWS;
    • smoke-test calls 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.

  1. 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-pipeline
    
  2. Open 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, lint and unit-tests, side by side, since neither needs the other;
    • open a job: its steps, and under Run echo … the line it printed.
  • GitHub reads the workflow files of the commit you pushed. It checks each file's on: against the event:
    • branch.yml matches a push to a branch that is not main, so it runs;
    • pr.yml waits for a pull request, and merge.yml for a push to main, 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

  1. On GitHub, open a pull request from ci/3-empty-pipeline to main, with Closes #3 in its description.
  2. 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: lint and unit-tests, then image-build, then integration-tests and security.
  • The run tests the code as main would hold it after the merge: GitHub merges the branch into main on 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.

  1. Merge the branch into main, and push main:

    1$ git switch main
    2$ git merge ci/3-empty-pipeline
    3$ git push
    
  2. The Actions tab shows a run of Merge:

    • its three jobs in a chain: image-push, then deploy, then smoke-test;
    • each starts only once the one before has passed.
  • branch.yml ignores main, 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, main refuses this push, and the merge button is the only way in.

Translations: