bamboo-specs parsing variables in yaml file

Alexander Gorshtein May 29, 2020

Hello,

i was looking for a way to parse variables in YAML files, in order to use some kind of template and just change 2 or 3 specific parameters

for example, i have 3 environments (DEV, PREPROD, PROD)  which should use same YAML file Template,  and will just need to replace environment itself

Example:

---
version: 2
deployment:
  name: STATUS
  source-plan: "ROCKET-STATUS"
release-naming:
  next-version-name: "Run"

environments:
  - DEV

DEV:
  tasks:
    - script:
        interpreter: shell
        scripts:
        - echo dev

 

 

i have same for PREPROD and PROD

 

i would like to put in beggining of the file  STAGE=DEV    and stage=dev

 

and then just use $STAGE or $stage

 

please help

2 answers

1 vote
Jimmy Seddon
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 29, 2020

Hi @Alexander Gorshtein,

I've been wanting the same type of thing.  The best I was able to do is plan based variables, but that I mean I did this:

---
version: 2

variables:
projectname: MyProject

plan:
project-key: Test
key: MP
name: My Test Project

Then further down I'm able to use that:

Build:
key: BLD
tasks:
- clean
- script:
interpreter: powershell
scripts:
- Find-Module -Name "MyCompany.MyModule" | Install-Module
- Build-Application -ProjectFile "${bamboo.projectname}.csproj"

It's not perfect, but it allows you to use a common variable name that you can substitute near the beginning of each plan and you can reuse the same variable name in each plan.

Also, I think you might be interested in voting for and commenting on this issue in Atlassian's public defect/feature request tracker: https://jira.atlassian.com/browse/BSERV-10945

It looks like it's requesting something similar that you might be able to provide your specific use case on in the comments.

I hope that helps a little bit.  Sorry there isn't a good answer to this question.

-Jimmy

Alexander Gorshtein May 29, 2020

huh, thanks alot

 

but when you use:

variables:
projectname: MyProject

and then: 

"${bamboo.projectname}.csproj"

 

it would look only within the plan?

 

because for example i have 3 different plans per stage, will include the variable thing, which you show, then ill have same deployment template (will use variables) and it would work?

Jimmy Seddon
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 29, 2020

Just to make sure I'm using the same terminology you are.  Build Plans contain Stages.  Are you trying to use the same variable for multiple stages within the same plan?

If so, I don't believe YAML specs offers that type of variable substitution.

One other option that you might want to consider is using Java specs instead (not sure how comfortable you are with Java) but that type of variable substitution is 100% available in Java specs.  I've ended up using a mixture of both depending on how complex my build plans are.

YAML is great for simple one or two step build plans but Java is better for the more complex items.

-Jimmy

Alexander Gorshtein May 29, 2020

i have yaml file for each stage, but want each yaml to be some kind of template, and in top of it to describe variables

 

in BASH and Python it will work, but here, not sure

 

regarding your variable, i think this way it goes into bamboo global variables, so i think it will not work

 

i need variable to be set only within yaml file

0 votes
Michael Kenneth Zromkoski May 29, 2020

--- Example is in a whole nother playground buddy one i developed and still developing so i understand you need to change the 3 enviroments from being 1 to 3. the task isnt impossible but you need the right people on the case etc,. workers.

Michael Kenneth Zromkoski May 29, 2020

also the echo may be your problem just like echo hello world

for your task in your sysytem

Alexander Gorshtein May 29, 2020

dont pay attention to the echo, its different script:

 

ansible-playbook -i inventory/dev    start_websphere.yml     and etc.....

 

i want to put variable   ansible-playbook -i inventory/$stage  start_websphere.yml 

in order it automatically recognize environment

 

so also it will be replaced on other parameters,   example what i thought:

 

 ###  you put variables at the top, and below template is same everywhere so  for easier modifying

STAGE=DEV   

stage=dev

---
version: 2
deployment:
name: STATUS
source-plan: "ROCKET-STATUS"
release-naming:
next-version-name: "Run"

environments:
- $STAGE

$STAGE:
tasks:
- script:
interpreter: shell
scripts:
- ansible-playbook -i inventory/$stage  start_websphere.yml 

Michael Kenneth Zromkoski May 29, 2020

that kinda makes sense considering you have no other options

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events