Workflow files: triggers, jobs and steps
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A robot can build and test every ShopLite change, but GitHub runs nothing until you say when to start, on which machine and what to do. Where do you write that down, and how do you read it back?
ci.yml: name and on
A trigger is an event that starts a workflow, such as a push or a pull request. The on: key of the workflow file lists the triggers.
Every workflow begins with a name and its triggers. This one starts after a merge to main, on every pull request that targets main, and by hand.
name: CIon: push: branches: [main] # after merging to main pull_request: branches: [main] # every PR that targets main workflow_dispatch: # "Run workflow" buttonGitHub runs every .yml file in .github/workflows/, and name: is the label shown in the Actions tab. The triggers used here:
push: a commit is pushed. Withbranches: [main]only pushes tomaincount.pull_request: a pull request is opened or updated.branches: [main]means "pull requests that targetmain".workflow_dispatch: adds a "Run workflow" button in the Actions tab, for manual runs.
Without branch filters, push fires for every branch, so each pull request would be built twice: once for the push, once for the pull request.
Predict the output
Does the workflow start?
Read the trigger, then pick the answer.
Round 1 of 3
A developer pushes a commit to the branch feature/coupons. Does the workflow run?
jobs, runs-on and steps: uses vs run
A job is a set of steps that runs on one fresh machine, called a runner. Steps run one after another and share the machine's files. When the job ends, the machine is thrown away.
This workflow has one job with four steps. The job gets a fresh Ubuntu machine, then the steps run on it in order.
Example 1 · .github/workflows/ci.yml: one job, four steps
Runs in: a GitHub repository with the shoplite-api project
name: CIon: push: branches: [main] pull_request: branches: [main] workflow_dispatch:jobs: build-test: # job id: shown in the UI and used by needs runs-on: ubuntu-latest # fresh GitHub-hosted Ubuntu machine steps: - name: Check out the code uses: actions/checkout@v5 # action: clone this repository - name: Set up JDK 25 uses: actions/setup-java@v5 # action with inputs with: distribution: temurin java-version: '25' - name: Show versions run: | # shell commands java -version echo "Building commit ${{ github.sha }} on ${{ github.ref_name }}" - name: Build and test run: ./mvnw -B verifybuild-test
✓ Set up job
✓ Check out the code
✓ Set up JDK 25
✓ Show versions
openjdk version "25.0.1" 2025-10-21 LTS
Building commit 4e1c9a7d0b2f63a8c5e94d1f07a2b6c3e8d9f012 on main
✓ Build and test
[INFO] BUILD SUCCESS
✓ Post Set up JDK 25
✓ Post Check out the code
✓ Complete jobWhat this code does
- One job with four steps that share one machine: the checkout puts the code there,
setup-javainstalls a JDK, and therunsteps use both.run: |starts a block of several shell lines. - GitHub adds the Set up, Post and Complete entries itself.
${{ github.sha }}and${{ github.ref_name }}were filled in before the shell saw the line.
A step is one task inside a job. It does one of two things:
uses:runs an action, a ready-made, versioned step from another repository, such asactions/checkout@v5(clone your code) oractions/setup-java@v5(install a JDK).with:passes its inputs.run:runs shell commands, such as./mvnw -B verify.
runs-on: ubuntu-latest asks for a GitHub-hosted Ubuntu machine with Git, Docker and common tools installed.
Fix the error
Fix the workflow file
Pick the line that replaces the broken one.
Round 1 of 2
The run fails before the build. Which line replaces line 3?
Error
Error: Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '.../mvnw -B verify'
Parallel jobs, needs and a matrix
needs makes a job start only after the jobs it lists have succeeded. Without needs, jobs run in parallel on separate machines, and a job whose needed job failed is skipped.
These two examples show a matrix that runs one job twice, and a small program that works out which jobs run at the same time.
Example 2 · A matrix and a job that needs it
Runs in: a GitHub repository with the shoplite-api project
jobs: build-test: runs-on: ubuntu-latest strategy: matrix: java: ['21', '25'] # two jobs, side by side steps: - uses: actions/checkout@v5 - uses: actions/setup-java@v5 with: distribution: temurin java-version: ${{ matrix.java }} - run: ./mvnw -B verify report: needs: build-test # waits for BOTH matrix jobs to succeed runs-on: ubuntu-latest steps: - run: echo "ShopLite builds on Java 21 and 25 at ${{ github.sha }}"build-test (21) ✓ 3m 41s build-test (25) ✓ 3m 38s report ✓ 4s (started after both build-test jobs)
What this code does
- The matrix turns one job definition into two jobs named after their value. They run in parallel on separate machines, so the total wait is about one build, not two.
needs: build-testwaits for every job of the matrix. If either Java version fails,reportis skipped.
Example 3 · How needs decides what runs in parallel
import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;
public class JobWavesDemo { public static void main(String[] args) { Map<String, List<String>> needs = new LinkedHashMap<>(); needs.put("lint", List.of()); needs.put("build-test", List.of()); needs.put("image", List.of("build-test")); needs.put("deploy", List.of("image", "lint"));
List<String> done = new ArrayList<>(); int wave = 1; while (done.size() < needs.size()) { List<String> ready = new ArrayList<>(); needs.forEach((job, deps) -> { if (!done.contains(job) && done.containsAll(deps)) { ready.add(job); } }); System.out.println("wave " + wave++ + " (in parallel): " + ready); done.addAll(ready); } }}wave 1 (in parallel): [lint, build-test] wave 2 (in parallel): [image] wave 3 (in parallel): [deploy]
What this code does
- Jobs with no
needsstart together in the first wave. A job starts as soon as everything it needs has finished, soimagewaits forbuild-test, anddeploywaits for bothimageandlint. - This is a simplified model: GitHub starts each job the moment its own needs are done, not in strict waves, but the order is the same.
Because a job that needs a failed job is skipped, ShopLite's image is built only after the tests pass.
A matrix runs the same job once per value. strategy.matrix.java: ['21', '25'] creates two jobs that run side by side, and ${{ matrix.java }} is the current value inside the job. Use it to prove ShopLite builds on both Java versions.
Predict the output
Which jobs run, and when?
Read the jobs, then pick the answer.
Round 1 of 3
Which jobs start first?
Contexts and expressions: github, secrets, if
A context is an object with information about the run, read inside ${{ ... }}, for example github (the commit, the branch, the event), secrets, matrix and runner.
This program applies one rule, "publish only after a push to main", to four different runs and shows which ones would publish the image.
Example 4 · Evaluating if: conditions for different events
public class IfConditionDemo { record Run(String eventName, String ref) {}
// if: github.event_name == 'push' && github.ref == 'refs/heads/main' static boolean pushToMain(Run github) { return github.eventName().equals("push") && github.ref().equals("refs/heads/main"); }
public static void main(String[] args) { Run[] runs = { new Run("pull_request", "refs/pull/42/merge"), new Run("push", "refs/heads/main"), new Run("push", "refs/heads/feature/coupons"), new Run("workflow_dispatch", "refs/heads/main") }; for (Run r : runs) { System.out.printf("%-17s %-27s -> push image: %s%n", r.eventName(), r.ref(), pushToMain(r) ? "yes" : "skipped"); } }}pull_request refs/pull/42/merge -> push image: skipped push refs/heads/main -> push image: yes push refs/heads/feature/coupons -> push image: skipped workflow_dispatch refs/heads/main -> push image: skipped
What this code does
- For a pull request,
github.refisrefs/pull/<number>/merge, a temporary merge of the PR into main, so the condition is false and nothing is published from unmerged code. - A manual run on main is
workflow_dispatch, notpush, so it is skipped too; add|| github.event_name == 'workflow_dispatch'if manual runs should publish.
An expression is written ${{ ... }} and is filled in by GitHub before the step runs. Common values:
${{ github.sha }}: the commit being built;github.ref: the branch, such asrefs/heads/main;github.event_name:push,pull_requestand so on.${{ secrets.X }}: a secret stored in the repository settings; GitHub hides its value in logs.
if: on a job or step runs it only when the expression is true. In if: you may leave out the ${{ }}.
Find the bug
Which condition is wrong?
Tap the line with the wrong condition.
The image gets pushed from unmerged pull requests. Which line is the problem?
Round 1 of 2Reading the Actions tab and common errors
The Actions tab is the page of a GitHub repository that lists every workflow run with a green tick or a red cross. Open a run, then a job, to read the log of each step.
Two errors stop a workflow before anything runs. They appear at the top of the run, not in a step log.
Invalid workflow file: .github/workflows/ci.yml#L14You have an error in your yaml syntax on line 14Error: Unable to resolve action `actions/chekout@v5`, repository not foundError: Unable to resolve action `actions/checkout@v99`, unable to find version `v99`In a run, the failing step is opened for you. Re-run jobs runs the same commit again; Re-run failed jobs repeats only the red ones.
Invalid workflow file means the YAML is broken or a key is wrong, and it gives the line number. Unable to resolve action means a uses: name or version does not exist: check the spelling and the tag after the @.
Next step
Check your understanding
12 quick questions