Bamboo - Pass Environmental Variables Between Tasks/Scripts

AlecBoutin picture AlecBoutin · Jan 28, 2016 · Viewed 8.9k times · Source

Is it possible to pass environment variables set in one script to another in Bamboo?

For example, I set up Go as below, and would like subsequent Tasks (scripts) to have access to the PATH and GOPATH I export here.

set -e

if [ ! -d "go" ]; then
    wget -q https://storage.googleapis.com/golang/go1.5.linux-amd64.tar.gz
    tar -xzf go1.5.linux-amd64.tar.gz
fi

export GOROOT=$(pwd)/go

mkdir -p gopath/
export GOPATH=$(pwd)/gopath
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH

Answer

Vincent Gerris picture Vincent Gerris · Aug 28, 2017

This has been implemented using the Inject Variables plugin, which is bundled since 5.7: https://marketplace.atlassian.com/plugins/com.atlassian.bamboo.plugins.bamboo-variable-inject-plugin/server/overview

The way to do it is the following:

  • in an initial task, have a script store the state to a file (in key=value format), something like:

echo "MYVAR=$(cat some_variable_info_file)" >> build/docker.properties

  • configure a following Inject task to read the properties file from the previous step into Bamboo variables. Set the PATH to the properties file (e.g. build/docker.properties) and set a namespace, say docker

  • to use this variable in the next script task*, one can refer to it as: $bamboo_docker_MYVAR where docker is the namespace and MYVAR is the key of the property in the property file. It can be referred to, for example, as:

echo $bamboo_docker_MYVAR

This means you can still used the file-based approach, just make sure the data in there is of type:

some_key1=some_value1
some_key2=some_value2

etc.

*Note that from the Bamboo documentation, the underscores are the way to use it: https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html

Using variables in bash

Bamboo variables are exported as bash shell variables. All full stops (periods) are converted to underscores. For example, the variable bamboo.my.variable is $bamboo_my_variable in bash. This is related to File Script tasks (not Inline Script tasks).