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
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:
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).
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With