13

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
John Weldon
  • 39,849
  • 11
  • 94
  • 127
AlecBoutin
  • 181
  • 1
  • 1
  • 4

2 Answers2

10

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).

Vincent Gerris
  • 7,228
  • 1
  • 24
  • 22
4

Environment variables can only be passed from parent to child processes. To get them into an unrelated program, you could write them into a file, and then source the file in the other script:

...
echo export GOROOT=$GOROOT >>$GOROOT/.vars
echo export GOPATH=$GOPATH >>$GOROOT/.vars
echo export PATH=$PATH >>$GOROOT/.vars

Then other scripts which start later on, should have this near the beginning (assuming they start with their working directory in the GOROOT directory):

source .vars

(Or add it in the script that starts those other scripts.)

PBI
  • 335
  • 1
  • 4
  • Hello Guys, Is this is the only way available till now ?, As i was seeking a way to send the result of one task to the next task but not able to find any default way provided by Bamboo. Can someone who have achieved this successful can elaborate on this....in context to the latest version of Bamboo CI Server. Any help would be appreciated. Thnx – Rohit Jul 10 '17 at 06:12