About Conditions

Updated 2 years ago by Admin

Conditions can be used to limit pipeline step execution at runtime. The condition syntax is common across all pipeline types.

Example limits step execution by branch:

1  kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: build
7 image: golang
8 commands:
9 - go build
10 - go test
11 when:
12 branch:
13 - master
14 - feature/*

You can use wildcard matching in your conditions. Note that conditions use glob pattern matching, not regular expressions.

11 when:
12 ref:
13 - refs/heads/master
14 - refs/heads/**
15 - refs/pull/*/head

You can also combine multiple conditions. Note that all conditions must evaluate to true when combining multiple conditions.

11 when:
12 branch:
13 - master
14 event:
15 - push

By Branch

The branch condition limits step execution based on the git branch. Please note that the target branch is evaluated for pull requests; and branch names are not available for tag events.

Note that you cannot use branch conditions with tags. A tag is not associated with the source branch from which it was created.
11 when:
12 branch:
13 - master
14 - feature/*

Example include syntax:

11 when:
12 branch:
13 include:
14 - master
15 - feature/*

Example exclude syntax:

11 when:
12 branch:
13 exclude:
14 - master
15 - feature/*

By Event

The event condition limits step execution based on the drone event type. This can be helpful when you want to limit steps based on push, pull request, tag and more.

11 when:
12 event:
13 - push
14 - pull_request
15 - tag
16 - promote
17 - rollback

Example include syntax:

11 when:
12 event:
13 include:
14 - push
16 - pull_request

Example exclude syntax:

11 when:
12 event:
13 exclude:
14 - pull_request

By Reference

The reference condition limits step execution based on the git reference name. This can be helpful when you want to glob match branch or tag names.

11 when:
12 ref:
13 - refs/heads/feature-*
14 - refs/tags/*

Example include syntax:

11 when:
12 ref:
13 include:
14 - refs/heads/feature-*
15 - refs/pull/**
16 - refs/tags/**

Example exclude syntax:

11 when:
12 ref:
13 exclude:
14 - refs/heads/feature-*
15 - refs/pull/**
16 - refs/tags/**

By Repository

The repository condition limits step execution based on repository name. This can be useful when Drone is enabled for a repository and its forks, and you want to limit execution accordingly.

11 when:
12 repo:
13 - octocat/hello-world

Example include syntax:

11 when:
12 repo:
13 include:
14 - octocat/hello-world
15 - spacebhost/hello-world

Example exclude syntax:

11 when:
12 repo:
13 exclude:
14 - octocat/hello-world
15 - spacebhost/hello-world

Example using wildcard matching:

11 when:
12 repo:
13 include:
14 - octocat/*

By Instance

The instance condition limits step execution based on the Drone instance hostname. This can be useful if you have multiple Drone instances configured for a single repository, sharing the same yaml file, and want to limit steps by instance.

11 when:
12 instance:
13 - drone.instance1.com
14 - drone.instance2.com

Example include syntax:

11 when:
12 instance:
13 include:
14 - drone.instance1.com
15 - drone.instance2.com

Example exclude syntax:

11 when:
12 instance:
13 exclude:
14 - drone.instance1.com
15 - drone.instance2.com

Example using wildcard matching:

11 when:
12 instance:
13 include:
14 - *.company.com

By Status

The status condition limits step execution based on the pipeline status. For example, you may want to configure Slack notification only on failure.

11 when:
12 status:
13 - failure

Execute a step on failure:

11 when:
12 status:
13 - failure

Execute a step on success or failure:

11 when:
12 status:
13 - success
14 - failure

The following configuration is redundant. The default behavior is for pipeline steps to only execute when the pipeline is in a passing state.

11 when:
12 status:
13 - success

By Target

The target condition limits step execution based on the target deployment environment. This only applies to promotion and rollback events.

11 when:
12 target:
13 - production

Example include syntax:

11 when:
12 target:
13 include:
14 - staging
15 - production

Example exclude syntax:

11 when:
12 target:
13 exclude:
14 - production

By Cron

The cron condition limits step execution based on the cron name that triggered the pipeline. This only applies to cron events.

11 when:
12 event:
13 - cron
14 cron:
15 - nightly

Example include syntax:

11 when:
12 event:
13 - cron
14 target:
15 include:
16 - weekly
17 - nightly

Example exclude syntax:

11 when:
12 event:
13 - cron
14 target:
15 exclude:
16 - nightly


How did we do?