Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

YAML anchors and variables - code reuse question

So, I'm familiar with the use of YAML anchors to create a "default" step, like so:

definitions:
steps:
- step: &baseline-step
name: Build Front-End app on non-master branch
script:
- echo "hello world"


branches:
master:
- step:
<<: *baseline-step
name: Build Front-End app on Master branch

 But I'm not quite sure how to use variables in this context.

For example, I have 3 or 4 tasks that use exactly the same code, except that the object name is different, or a keyword arg value is different. I want to do something like this:

definitions:
steps:
- step: &baseline-step
name: Build Front-End app on non-master branch
script:
- echo "hello world"
- ./my_script.sh -i $MY_VAR


branches:
master:
- step:
<<: *baseline-step
name: Build Front-End app on Master branch
MY_VAR: Prod # would generate './my_script.sh -i Prod'
dev:
- step:
<<: *baseline-step
name: Build Front-End app on Dev branch
MY_VAR: Dev # would generate './my_script.sh -i Dev'

 

I feel like I'm misunderstanding variables. Like, there's a missing piece here. My goal is code reusability, but it seems like without enabling...Deployments, maybe?...I can't use variables.

Can someone explain to me how to achieve this? I'm not the first person to ask this question, and it seems that nobody else has gotten a clear answer on this, either:

YAML-anchors-Overriding-Reusing-deployment-variables 

Variables-per-step-yaml-anchors/ 

Is-it-possible-using-YAML-anchors-to-define-variables-that-you 

1 answer

@inger_klekacz  you can use if loop to change the variable value based on your condition, so based on the condition satisfied $MY_VAR will pick that value and perform the action.

Suggest an answer

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

Bitbucket Pipelines Runners is now in open beta

We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...

3,695 views 63 19
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