Conditional workflow using when: and matches:

Hey folks!

I want to be able to skip a workflow and not display any steps when I use [disable] in the commit message. I’m aware I can use [ci skip], but I also need the workflow to not display any steps.

Unfortunately documentation on using when is rather poor.

Here’s what I’ve tried to far, but it doesn’t even validate:

parameters:
  commit_message:
    type: string
    default: $(git log --format=oneline -n 1 $CIRCLE_SHA1)
...
workflows:
  version: 1
  app-build:
    when:
      matches:
            pattern: "^.*\[disable].*$"
            value: << pipeline.parameters.commit_message >>
    jobs:
      - build
...

nor:

...
  app-build:
    when:
      matches: ["^.*\[disable].*$", << pipeline.parameters.commit_message >>]
...

nor:

...
  app-build:
    when:
      condition:
          matches: ["^.*\[disable].*$", "<< pipeline.parameters.commit_message >>"]
...

I can’t provide the full background on how (and why) the condition system works as I’ve not used it enough to fully understand the quirks caused by trying to express such logic in yaml, which was never designed to do such things, but I can

  • say that you have the structure of ‘matches’ wrong, there are 2 comparison statements, and they have different ways of expressing the values/parameters. You seem to be using the structure for ‘equal’.
  • ‘condition’ is not added when doing a check for a workflow.

Below is an example that does work.

version: 2.1

executors:

  hosted_node:
    machine:
      image: ubuntu-2204:current
    resource_class: medium
    
jobs:
  build:
    executor: hosted_node
    steps:
      - run: echo 'work done'
      
workflows:
  build-and-deploy:
    when:
        matches: { pattern: "main", value: << pipeline.git.branch >> }
          
    jobs:
      - build

This will execute the job ‘build’ if pipeline.git.branch is ‘main’.

The main docs on logic statements can be found here

I hope this helps.