Sub topic 1
What if does and where you can use it
Prerequisites
This chapter assumes you can read basic YAML and know the layout of a GitHub Actions workflow file: on, jobs, and steps. You should also know that a job is a group of steps that runs on one runner, and that a step is a single command or action inside a job. You should have triggered a workflow from a push or a pull request at least once.
What the if keyword does
By default, every step in a job runs in order, and every job in a workflow runs. The if keyword adds a condition. GitHub evaluates the condition, and if the result is true, the step or job runs. If the result is false, GitHub skips it.
You can place if in two spots:
- On a step, to skip that one step while the rest of the job continues.
- On a job, to skip the whole job, including all of its steps.
A first example
The workflow below builds on every push and pull request. It runs a preview step only for pull requests, and a publish step only for pushes to main.
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: echo "Building"
- name: Deploy preview
if: github.event_name == 'pull_request'
run: echo "Deploying preview"
- name: Publish
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: echo "Publishing"The Build step has no if, so it always runs, provided the earlier steps succeeded. github.event_name holds the name of the event that started the run, such as push or pull_request. github.ref holds the full name of the branch or tag, such as refs/heads/main.
On a pull request, the Deploy preview step runs and Publish is skipped. On a push to main, the reverse happens. On a push to any other branch, both are skipped.
A job-level condition
An if on a job is evaluated before the job starts. If it is false, GitHub never starts a runner for that job.
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: ./run-tests.sh
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: ./deploy.shHere deploy runs only on main. On other branches the workflow shows deploy as skipped, and test still runs. A skipped job also affects the jobs that depend on it through needs. A later chapter covers that.
What you can write in a condition
The condition is an expression. In this chapter's examples it uses three kinds of parts:
- Contexts, such as
github, which hold information about the run. - Operators, such as
==,&&, and!=. - Functions, such as
startsWith()andfailure().
Later chapters cover each kind in detail.
The default when there is no condition
A step without an if behaves as if it had if: success(). It runs only when all earlier steps in the job succeeded. After a failed step, the remaining steps are skipped. Overriding this default is the job of the status check functions, which a later chapter covers.
Exercise
A workflow has a job named release with the line if: github.ref == 'refs/heads/main'. You push a commit to a branch named feature-x. What happens to the release job, and do other jobs in the workflow still run?
Show answer
The release job is skipped, because the condition is false for refs/heads/feature-x. Other jobs without a false condition, and that do not depend on release, still run normally.
Exercise
In one job, step 2 has if: github.event_name == 'pull_request'. The workflow is triggered by a push. Do steps 3 and 4, which have no if, still run?
Show answer
Yes. Skipping a step because its condition is false is not a failure. Steps 3 and 4 have the implicit success() condition, and the skipped step does not count as a failed step, so they run.