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

**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();vd.setPlan(plan);vd.setVariableType(VariableType.PLAN);vd.setKey(k);vd.setValue(v);variableDefinitions.add( vd ); variableDefinitionManager.saveVariableDefinitions(variableDefinitions);
```

<br />

<br />

<br />

**What currently works:**   

\* Pass variables between tasks  

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

<br />

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

\* Pass variables between a stages

<br />

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

**Note**

I opened <https://jira.atlassian.com/browse/BAM-10899> to request the feature, but if someone has a magic trick to share, please do !


Answers:
author: Przemek Bruski
body: 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.

comments: author: Kepio
body: 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 );        variableDefinitionManager.saveVariableDefinitions(variableDefinitions);
```

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}


Tags: artefact

variable