Sub topic 14
Reading the event and branching on it
This chapter assumes you can write a workflow with several triggers in the on key. You should also know basic if: syntax and what a job and a step are. When one workflow listens to several events, it needs a way to tell them apart. GitHub gives each run a context of data about the event, and you can use that data to decide what runs.
The github context
Every run has a github context. Three fields matter most here:
github.event_nameis the name of the event that started the run, such aspush,pull_request,scheduleorworkflow_dispatch.github.refis the full Git ref that the run is for, such asrefs/heads/main.github.eventholds the full webhook payload. Its fields depend on the event. For example,github.event.pull_request.numberexists only for pull request events.
Reading a field that does not exist in the current event gives an empty value, not an error. That is useful for conditions, but it can hide typos.
Branching with if
Add if: to a step or a job to run it only for certain events. The expression does not need the ${{ }} wrapper in an if: field.
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: '0 6 * * 1'
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Event: ${{ github.event_name }}"
- name: Weekly-only step
if: github.event_name == 'schedule'
run: echo "Running the weekly extra checks"
- name: Pull request number
if: github.event_name == 'pull_request'
run: echo "PR ${{ github.event.pull_request.number }}"An if: on a job skips the whole job. This is cheaper than skipping each step, because no runner is started.
jobs:
deploy:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "Deploying from main"Here the job runs only for pushes to main. Pull requests and scheduled runs skip it.
Choosing the right check
| Question | Expression |
|---|---|
| Was this a manual run? | `github.event_name == 'workflow_dispatch'` |
| Is this either of two events? | `contains(fromJSON('["push","schedule"]'), github.event_name)` |
| Is the branch `main`? | `github.ref == 'refs/heads/main'` |
| Was the pull request a draft? | `github.event.pull_request.draft == false` |
Common errors
- Wrong ref on pull requests. For
pull_request,github.refisrefs/pull/<number>/merge, not the branch name. Usegithub.base_reffor the target branch andgithub.head_reffor the source branch. Both are empty for other events. - Comparing with the wrong case or name. Event names are lowercase with underscores.
github.event_name == 'pullRequest'is never true. - Putting
${{ }}around a wholeif:with text. A string such asif: ${{ github.event_name }} == 'push'becomes a non-empty string and is always true. Write the full expression without the wrapper. - Using event data in shell scripts. Values like a pull request title are controlled by other people. Pass them through
env:and read them as shell variables, instead of placing${{ }}insiderun:.
Exercise
A workflow listens to push and pull_request. You want a deploy job that runs only when code is pushed to main, never for pull requests. Which if: expression on the job do you write, and why is github.ref == 'refs/heads/main' alone risky?
Show answer
Use if: github.event_name == 'push' && github.ref == 'refs/heads/main'. On a pullrequest event, github.ref is refs/pull/<number>/merge, so the ref check alone would not match today, but it depends on that detail and does not state your intent. Checking `github.eventname` makes the rule explicit and keeps it correct if you add other triggers, such as workflow_dispatch run from main.