How do we conditionally run a CircleCI workflow?
Asked Answered
A

1

5

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?

Aylmer answered 13/11, 2020 at 6:11 Comment(0)
S
8

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)

Siusan answered 18/11, 2020 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.