Sub topic 2
Skipping and running individual steps
This chapter assumes you can read a workflow file with on, jobs, and steps. You should know that a job is a group of steps that share one runner, and that a step is a single command or action. Here you add an if to a step so it runs only when you want it to.
How a step-level if works
By default, a step runs when every earlier step in the job has succeeded. Adding if to a step adds a second requirement. The runner evaluates the expression just before the step would start. If the result is true, the step runs. If it is false, the runner marks the step as skipped and moves on.
A skipped step does not fail the job. The job stays green as long as no other step fails.
One job, three behaviors
The workflow below builds on every trigger. The preview step runs only for pull requests, and the publish step runs 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 condition reads two values. github.event_name is the event that started the run. github.ref is the full name of the branch or tag. The && operator requires both comparisons to be true.
The next diagram shows how a runner treats each if step.
Running steps after a failure
Without an if, a step is skipped once an earlier step has failed. This is usually what you want, but not for log upload or cleanup. Status check functions change the default.
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: ./run-tests.sh
- name: Upload logs on failure
if: failure()
uses: actions/upload-artifact@v4
with:
name: test-logs
path: logs/
- name: Cleanup
if: ${{ always() }}
run: ./cleanup.shfailure()is true only when an earlier step in the job failed. The log upload runs after a failed test and is skipped when the tests pass.always()is true no matter what happened before, so cleanup runs after success and failure alike.
This chapter only introduces these two functions. A later chapter covers all four status functions in detail, including cancelled().
Common mistakes
- Forgetting the default. A step with no
ifis skipped after a failure. If a step must run after a failure, give itif: always()orif: failure(). - Reading a skip as a failure. A skipped step shows a grey icon in the log and does not stop the job. Check the condition before you suspect the runner.
- Putting the condition on the wrong level. An
ifplaced on a step affects only that step. The steps around it still follow their own rules.
Exercise
A job has three steps in this order: Build, Test, and a step named Notify with if: failure(). Build succeeds and Test fails. What happens to Notify? What happens if Test passes?
Show answer
When Test fails, failure() is true, so Notify runs. When Test passes, failure() is false, so Notify is skipped and the job still succeeds.
Exercise
Write the if line for a step that must run on every push to the develop branch and on nothing else.
Show answer
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'