bamboo-specs parsing variables in yaml file
Asked by Alexander Gorshtein:

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


Answers:
author: Jimmy Seddon
body: Hi [@Alexander Gorshtein](/t5/user/viewprofilepage/user-id/3914910),

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

comments: author: Alexander Gorshtein
body: 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?


author: Jimmy Seddon
body: 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


author: Alexander Gorshtein
body: 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


author: Michael Kenneth Zromkoski
body: --- 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.

comments: author: Michael Kenneth Zromkoski
body: also the echo may be your problem just like echo hello world

for your task in your sysytem


author: Alexander Gorshtein
body: 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


author: Michael Kenneth Zromkoski
body: > that kinda makes sense considering you have no other options


Tags: spec

yaml