It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Defining Conditional Steps Edited

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

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?

maybe I posted the question wrong.

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

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.

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.

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket Pipelines

What We Learned When We Researched Open Source Vulnerabilities in 7 Popular Coding Languages

...hey are a part of us, shaping how we interact with the world around us. The same holds true for programming languages when we think about how different kinds of vulnerabilities raise their heads in t...

1,038 views 0 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you