Simple conditional workflow (single condition)

I noticed there are options to conditionally run a workflow. However, I am having issues with a single condition. Simply put I want to run a build workflow on anything but main and a deploy workflow only on main. The below example errors out. I have also tried adding an and or an or stanza even though I have a single condition. That also errors out. Thanks for the help!

FWIW: all the examples I can find of conditional workflows all detail multiple conditions:

workflows:
  build:
    when:
      - not: [ main, << pipeline.git.branch >> ] # <- pretty sure this expression is wrong
    jobs:...

  deploy:
    when:
      - equal: [ main, << pipeline.git.branch >> ]
    jobs:...
1 Like

I think I got it, yaml syntax oops. Though it seems weird to use and on a single condition.

workflows:
  build:
    when:
      and:
        - not:
            equal: [ main, << pipeline.git.branch >> ]
    jobs:

  deploy:
    when:
      and:
        - equal: [ main, << pipeline.git.branch >> ]
    jobs:
2 Likes

As stated here :

Note: When using logic statements at the workflow level, do not include the condition: key (the condition key is only needed for job level logic statements).

1 Like

I had the similar issue with single condition. I had

workflows:
  build:
     when:
          -  equal: [ "matchingstring", << pipeline.parameters.xvalue >> ]
     jobs:
     ......
  test:
     when:
          -  equal: [ "matchingstring2", << pipeline.parameters.xvalue >> ]
     jobs:
     ......

Had errors parameter expected type: String, found: Sequence and no subschema matched out of the total 4 subschemas . Though the yaml validator did not give any errors.

Finally adding “and:” fixed the issue.

workflows:
  build:
      when:
           and:
                - equal: [ "matchingstring", << pipeline.parameters.xvalue >> ]
      jobs:
      ......
  test:
     when:
          and:
             -  equal: [ "matchingstring2", << pipeline.parameters.xvalue >> ]
     jobs:
       ......

@jpi @brunda27anaji I found it similarly frustrating that the docs only showed “complex” conditional logic (“and” and “or”) and not any examples of simpler “single” conditions.

I did some experimenting, and it seems like single conditions can indeed be done without using and. I believe the issue that both of you ran into was to include a YAML list below the when. In other words, if you remove the dash (-) and just include the condition, it looks like it works. For example (using @jpi‘s initial example):

workflows:
  build:
    when:
      not:
        equal: [ main, << pipeline.git.branch >> ]
    jobs:
      ...
  deploy:
    when:
      equal: [ main, << pipeline.git.branch >> ]
    jobs:
      ...
 

Hi @cjerdonek , product manager at CircleCI here.
On this Docs page you can find a few examples of simplified when syntax that does not require you to use and: and or:.
In your case, you can now write

workflows:
build:
when: pipeline.git.branch != “main”
jobs:

1 Like

Great, thanks!

1 Like