Conditions
Conditions limit pipeline step execution at runtime. Harness Open Source sets variables that can be used in conditions.
Harness Open Source supports multiple pipelines per repository. Creating a pipeline per trigger (push, pull request, tag) can reduce the need for conditions.
The following operators are supported:
| Type | Operator |
|---|---|
| Comparison | ==, != |
| Logical | not, and, or |
| Regex | matches |
| String | contains, startsWith, endsWith |
The following functions are supported:
| Type | Syntax |
|---|---|
| Always | always() |
| Failure | failure() |
This pipeline runs the test step only for pull request events where the target branch is main.
kind: pipeline
spec:
stages:
- type: ci
spec:
steps:
- name: test
type: run
when: |
build.event == "pull_request"
and
build.target == "main"
spec:
container: ruby
script: |-
bundle install --jobs=3 --retry=3
rake
This condition runs the step for pull request events where the target branch is not main.
when: |
build.event == "pull_request"
and
build.target != "main"
This condition runs the step only when a pull request is created based on the action.
when: build.action == "pullreq_created"
Branch
Limit execution based on the target branch.
This pipeline runs the build step when the target branch is main, or starts with feature/.
kind: pipeline
spec:
stages:
- type: ci
spec:
steps:
- name: build
type: run
when: |
build.target == "main"
or
build.target startsWith "feature/"
spec:
container: golang
script: |-
go build
go test
This condition uses a regular expression to achieve the same behavior.
when: build.target matches "main|feature/.*"
Event
Limit execution based on the event.
This pipeline runs the clean cache step only for manually triggered pipelines.
kind: pipeline
spec:
stages:
- type: ci
spec:
steps:
- name: clean cache
type: run
when: build.event == "manual"
spec:
container: node:18
script: |-
npm cache clean --force
Reference
Limit execution based on the git reference.
This pipeline runs the build step for branch names that start with feature-, or for tags.
kind: pipeline
spec:
stages:
- type: ci
spec:
steps:
- name: build
type: run
when: |
build.ref startsWith "refs/heads/feature-"
or
build.ref startsWith "refs/tags/"
spec:
container: golang
script: |-
go build
go test
Status
Limit execution based on the pipeline status.
This pipeline runs the notify step only when the test step fails.
kind: pipeline
spec:
stages:
- type: ci
spec:
steps:
- name: test
type: run
spec:
container: gradle:jdk10
script: |-
gradle assemble
gradle check
- name: notify
type: plugin
when: failure()
spec:
name: slack
inputs:
webhook: ${{ secrets.get("slack_webhook") }}
This condition will always run the step.
when: always()