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

YAML anchors and variables - code reuse question

inger_klekacz
Contributor
June 24, 2021

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

0 votes
BHARATHKUREK June 27, 2021

@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
AUG Leaders

Atlassian Community Events