Search code examples
devopspipelinecirclecicircleci-2.0circleci-workflows

How do we conditionally run a CircleCI workflow?


I have followed the guide described in Conditional steps in jobs and conditional workflows and written the below code for my CircleCI pipeline.

version: 2.1

workflows:
 version: 2.1
 workflowone:
  when:
   condition: false
  jobs:
   - samplejob:

 workflowtwo:
  when:
   condition: true
  jobs:
   - jobone


jobs:

 samplejob:
  docker:
   - image: buildpack-deps:stable
  steps:
   - run:
       name: Sample Job in WF 1
       command: |
         echo "This job is in workflowone and the workflow should not run"

 jobone:
  docker:
   - image: buildpack-deps:stable
  steps:
   - run:
       name: Sample Job in WF 2
       command: |
         echo "This job is in workflowtwo and the workflow should run"

When I run the above code the output is not what is expected. First workflow should not run because the condition is false. Both worflows start running when the pipeline in triggered. Can anyone point out the missing piece here?


Solution

  • According to the CircleCI docs, workflows (specifically) does not accept the condition key:

    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).

    See here logic-statement-examples (scroll to the bottom of this section to see the note)