Is it possible to set Bamboo variables in one stage ( via custom task) and retrieve it in a different stage

Use case
A task may define some *custom build variable* that would need to later be retrieved in subsequent tasks.
The current mechanism to handle this is to share artifacts between the tasks - the problem with this approach is that the "native" bamboo tasks, such as the ant task could not access this "custom build variables"; we would need to write a wrapper script that parses the artifact then invoked the ant command. Furthermore, using artifacts requires to add extra logic in every task to read/write to the artifact.

Is it possible to set a bamboo build variable ( attach it to the plan, see code below) and access it in later stages - without going through artifacts?

Tested case
I created custom build variables using the variableDefinitionManager

VariableDefinition vd = new VariableDefinitionImpl();
variableDefinitions.add( vd );

What currently works:

* Pass variables between tasks

Stage 1
task 1A: set_plan_variable myVar
task 1B : print_variable myVar

What does not work:
* Pass variables between a jobs (expected not to work since jobs can be concurrent)

* Pass variables between a stages

Stage 1
task 1A: set_plan_variable myVar
Stage 2:
task 2A : print_variable myVar


I opened to request the feature, but if someone has a magic trick to share, please do !

1 answer

1 accepted

3 votes

I think the only way is to have a finalising task that puts all variables into a file defined as an artifact and a task that fetches them from that file in another stage/Job.

As you've mentioned, by design, you won't be able to do that between Jobs in the same Stage. That's why we've introduced Stages in the first place - to serialise Jobs and allow them to pass information between one another.

Thanks Przemek - that makes perfect sense - I wrote a task that dump the custom build environment variables into a java property file and another task to load it at a later stage ( the property file is defined as artifact) - the problem I have is that the variables get loaded as environment variables and not as a bamboo build variables - as such, they can't be used as arguments into tasks - for instance, to configure the working sub-directory, I can't use $my_subdir. If however, I run a simple bash task that "echo $my_subdir" it will echo it properly.

I'm using the below routine to load the variables into the environment :

List<VariableDefinition> variableDefinitions = new ArrayList<VariableDefinition>();
VariableDefinition vd = new VariableDefinitionImpl(key,value,thisPlan,VariableType.PLAN);
variableDefinitions.add( vd );

Is there a better way to load a pair of "key/value" into a build variable from a task and access it later via $(key) or $(bamboo.key}

Suggest an answer

Log in or Join to answer
Community showcase
Renan Battaglin
Published May 18, 2017 in Bamboo

FAQ: How to Upgrade Bamboo Server

Bamboo 5.9 will no longer be supported after June 12, 2017. What does this mean? As part of our End of Life policy, Atlassian supports major versions for two years after the first major iteratio...

1,066 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot