Conditional Execution of Steps

For each step in a codefresh.yml file, you can define a set of conditions which need to be satisfied in order to execute the step. (An introduction to the codefresh.yml file can be found here.)

There are currently two main methods to define conditions: branch conditions and expression conditions.

Branch Conditions

Usually, you’ll want to define a branch condition, be it of the type ignore for blacklisting a set of branches or of the type only for whitelisting a set of branches. Each branch specification can either be an exact branch name, e.g. master, or a regular expression, e.g. /hotfix$/. Case insensitive regexps (/^FB-/i) are also supported.

Here are some examples:

Only execute for the master branch: only-master-branch

build-step:
  description: Building the image.
  type: build
  dockerfile: Dockerfile
  image-name: someRepo/someUser
  when:
    branch:
      only:
        - master

Only execute for branches whose name begins with FB- prefix (feature branches):

only-feature-branches

build-step:
  description: Building the image.
  type: build
  dockerfile: Dockerfile
  image-name: someRepo/someUser
  when:
    branch:
      only:
        - /^FB-.*/i

Ignore the develop branch and master branch:

ignore-master-and-develop-branch

build-step:
  description: Building the image.
  type: build
  dockerfile: Dockerfile
  image-name: someRepo/someUser
  when:
    branch:
      ignore:
        - master
        - develop
Regular Expressions Flavour

The JavaScript regular expressions flavour is the one used in branch conditions.

Expression Conditions

Alternatively, you can use more advanced expression conditions.

This follows the standard expression condition syntax. In this case, you can choose to execute if all expression conditions evaluate to true, or to execute if any expression conditions evaluate to true.

Here are some examples. Execute if the string [skip ci] is not part of the main repository commit message AND if the branch is master

all-conditions

build-step:
  description: Building the image.
  type: build
  dockerfile: Dockerfile
  image-name: someRepo/someUser
  when:
    condition:
      all:
        noSkipCiInCommitMessage: 'includes(lower("${{CF_COMMIT_MESSAGE}}"), "skip ci") == false'
        masterBranch: '"${{CF_BRANCH}}" == "master"'

Execute if the string [skip ci] is not part of the main repository commit message, OR if the branch is not a feature branch (i.e. name starts with FB-)

any-condition

build-step:
  description: Building the image.
  type: build
  dockerfile: Dockerfile
  image-name: someRepo/someUser
  when:
    condition:
      any:
        noSkipCiInCommitMessage: 'includes(lower("${{CF_COMMIT_MESSAGE}}"), "skip ci") == false'
        notFeatureBranch: 'match("${{CF_BRANCH}}", "^FB-", true) == false'