Conditional Expression
Conditional expression allows a task or step that only runs if the condition is met. It can be defined on a task or on a step.
Conditional task¶
A conditional task consists of steps with a key when. The purpose of the when steps is customizing commands and job configuration to run on custom conditions (determined at compile time) that are checked before a workflow runs.
tasks:
build-image:
steps:
...
when: branch == 'master'
Conditional step¶
A conditional step consists a step with a key if. The step will only run if the expression evaluates to true.
tasks:
build-image:
steps:
- checkout
- run: sudo apt-get update
...
- docker/build:
image: example.com/demo
if: branch == 'master'
The expression has access to the workflow related context objects branch, tag, platform and event. It can contain following tokens -
branch- refers to the git branchtag- refers to the git tag if presentevent- refer to the trigger event. Should be one ofpush,pull_requestandtag.platform- refers to the target operating system and architecture. for examplelinux/amd64. Refer$GOOSand$GOARCHfor the possible values here.
You can provide more complex expression using operators and modifiers
branch == 'master'
branch in ('master', 'staging') || branch =~ 'hotfix' || tag
event == 'deployment'
platform == 'linux/amd64'