Defining Conditional Steps

LiorGonda March 10, 2020

NOTE: this is not much of a question; but a suggestion/request for consideration of new functionality

 

I want to run some steps only in particular cases.

My expectation would be like:

definitions:
steps:
- step: &do-some-deployemnt
name: Do some Deployment
script:
- echo "this happens only if condition is met"

pipelines:
pull-requests:
'**':
- step:
script:
- echo "this happens only if condition is met"
when: <condition>
custom:
some-custom-pipeline-1:
- variables:
- name: VAR_NAME
- step:
<<: *do-some-deployemnt
deployment: Production
when: <condition>
some-custom-pipeline-2:
- variables:
- name: VAR_NAME
- step:
<<: *do-some-deployemnt
deployment: Test
when: <condition>
- step:
<<: *do-some-deployemnt
deployment: Production-1
when: <condition>
- step:
<<: *do-some-deployemnt
deployment: Production-2
when: <condition>

for the purpose of clarity, lets say in Bit-Bucket setting we created "deployment variables"

on here

  • pull-requests uses git diff
  • some-custom-pipeline-1 uses "custom variables"
  • some-custom-pipeline-2 uses both "custom variables" and "deployment variables"

now, condition could be anything which evaluates to true/false at the end

when: $(git diff --name-only --diff-filter=AMDR HEAD^ some-directory-or-file) != ""

we have multiple steps, only deploy the one if a deployment variable matches the user input variable

when: ENVIRONMENT_NAME = VAR_NAME # if deployment variable matched custom variable

based on a custom variable input by the user. On example some-custom-pipeline-2, there is 1 test deployment and 2 different production deployments. User might want to deploy Test, or both Productions.

when: VAR_NAME = 'production' # if custom variable matched string

NOTE:

a similar question was asked at:

https://community.atlassian.com/t5/Bitbucket-Pipelines-questions/Defining-Conditional-Steps/qaq-p/1192816?tempId=eyJvaWRjX2NvbnNlbnRfbGFuZ3VhZ2VfdmVyc2lvbiI6IjIuMCIsIm9pZGNfY29uc2VudF9ncmFudGVkX2F0IjoxNTgzODY0ODE3NDU2fQ%3D%3D

but this question was routed ONLY to file changes

1 answer

0 votes
ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 10, 2020

As far as I know steps can not have conditions (e.g. the when in your question, like the when in Ansible probably).

But steps can have a script and within a script you can encode all sort of conditional expressions. So maybe moving the condition into the step could do it in your case already?

Lior G March 10, 2020

maybe I posted the question wrong.

It's less a question, and more a request for possible future functionality

ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 10, 2020

this would be a before script then, which I think should not be necessary as you could do it at the very beginning of the script and exit 0 otherwise. that should work.

a feature request for a when would merely result in a different expression language, not the shell expressions you have as examples in your question.

but I'm not from Atlassian, so just what pops into my limited mind. Someone from Atlassian will also tell you in an answer how/where you can file feature requests.

LiorGonda March 10, 2020

the difference being

if a step runs, and at the start of the script you implement your conditional, the step still runs. The initial "Build setup" takes place, consumes resources, and shows the step as run..  even if with an exit as it's first command

If the conditional is run before, then if false, the step does not even run. On the case of example some-custom-pipeline-2, either 1 or 2 steps would run, but not all 3. And with some extra logic, if no steps are run in a pipeline, then the entire pipeline might not even run.

ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 10, 2020

And what would be your understanding where the when condition would be executed?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events