Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bamboo - Pass Environmental Variables Between Tasks/Scripts

Tags:

shell

bamboo

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
like image 807
AlecBoutin Avatar asked Jan 28 '16 17:01

AlecBoutin


2 Answers

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

like image 199
Vincent Gerris Avatar answered Oct 03 '22 19:10

Vincent Gerris


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

like image 31
PBI Avatar answered Oct 03 '22 21:10

PBI