Is it possible to use a logic statement when calling a job within a workflow? For example:
myjob:
when: << pipeline.parameters.job_parameter >> # this works
jobs:
- checkout_code:
executor: executor
filters:
branches:
only: testbranch
when: # this does not work
condition:
equal: [ "develop", << pipeline.parameters.job_env >> ]
The use case here is that I am attempting to trigger a pipeline via a webhook in another app, however I only want it to trigger when a certain environment string is passed as a parameter in the payload. This way I can trigger builds with different contexts, depending on the environment passed. As of right now, running circleci config validate
tells me that the second when
is an unexpected argument. Is there a way to get this to work? Or do I have to create a separate workflow for each environment?
Yes, this is possible at least to a degree: https://support.circleci.com/hc/en-us/articles/360043638052-Conditional-steps-in-jobs-and-conditional-workflows
Your sample code is a bit confused though. You seem to have a mashup of a job and a workflow. I think your configuration should look something like this:
executors:
executor:
...
workflows:
conditional-workflow:
when: << pipeline.parameters.job_parameter >>
jobs:
- conditional-job:
filters:
branches:
only:
- testbranch
jobs:
executor: executor
conditional-job:
steps:
- when:
condition:
equal: [ "develop", << pipeline.paramaters.job_env >> ]
steps:
- checkout_code
This configuration will go through the following checks:
- If the pipeline parameter
job_parameter
is truthy, create an instance of conditional-workflow
. Otherwise no workflow is created and we’re done.
- If the pipeline is running on the
testbranch
branch, the workflow will include the conditional-job
job. Otherwise, that job will be excluded from the workflow, and because there are no other jobs, we’re done.
- We now have started a job! It will provision an executor
executor
, the go into it’s steps. If the pipeline parameter job_env
is equal to the string develop
, then the steps
in the conditional will be used for the job and run. If not, we skip all of those steps, and because there are no more, we’re done.