Access Bamboo variables in bash script

I want to access the global variables set in bamboo in a bash script that I am running on my local. How can I access those variable?

2 answers

The following works for me in a "script" task:

#!/bin/bash
bash /path/to/some/shell/script/on/bamboo/server/script.sh

where /path/to/some/shell/script/on/bamboo/server/script.sh looks something like:

#!/bin/bash
# Note underscore in definition
echo "worldName was set to: ${bamboo_worldName}"
exit 0

And in the "Variables" tab of a plan, I set worldName to "foo".  When bamboo runs, it outputs:

worldName was set to: foo

Note that in the script task itself, it correctly reads and interprets a bamboo.variable, but when you run in a sub-shell (say, calling/running an external shell script from the "script" task), you just have to translate it to bamboo_variable.

Also, sorry for the very late reponse.

EDIT:  I only now read your last comment, which was basically, "can you modify the global variables set in Bamboo by running a script locally inside Bamboo?"  The answer is probably "no" - a cursory scan through the REST API showed that you can change them on a per-instance case (if they're a queued plan), or trigger a build/deploy via the REST API overriding existing variables, however, I didn't see a way to actually set the "Global Variables" in Bamboo overall directly.  Note that with some things, I've had to scrape HTML to figure out what's going on.  Essentially, create a Selenium "test" that automatically clicks through various buttons and whatnot to post the relevant information.  It's a terrible hack and highly fragile, and likely to break something if you're not careful.

0 votes
Marcin Oles Atlassian Team Oct 01, 2015

Hi Hardik,

to do so use this syntax:

${bamboo.<variableName>}

for example

#!/bin/sh
echo "Hello, ${bamboo.worldName}!"
exit ${bamboo.exitCode}

Such variables will not get evaluated by shell, because prior to being executed Bamboo will replace all identified variables with their values.

More detailed guide can be found here: https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html

---------- UPDATE ----------

As I mentioned in the comment, substituting variables will not work for script tasks run from a file. Only inline scripts will be substituted.

A workaround for this would be to use variables during configuring the Script task, as script arguments or environment variables. See the screenshot for more info:

script-task-variables.png

Cheers,
Marcin 

Hello Marcin, I have created a global variable for demo purpose. The variable is called demovariable I created a bash script as follows: #!/bin/bash echo ${bamboo.demovariable} I am getting the error '${bamboo.demovariable}: bad substitution' Am I doing anything wrong? P.S. Just to divert your attention to this important fact, I am trying to access these variables in a bash script running on my local machine and not as Bamboo inline script

Marcin Oles Atlassian Team Oct 04, 2015

"bad substitution" is an error message from bash. The variables are not being substituted, so they are left in the script which then fails to execute (bash and sh can't handle the dot in variable names). I've verified that variables will not be substituted in script files, only for inline scripts. That's probably the reason. As a workaround, you can use variables in "script arguments" and "environment variables" in the task configuration, and then use the arguments or env vars in the script. Cheers, Marcin

Hello Marcin, I did get the point wherein you mentioned why cannot the bash access the variable using the dot. But I am unable to understand the workaround that you have mentioned. Can you please explain that in greater detail?

Marcin Oles Atlassian Team Oct 05, 2015

Updated my answer with more details and a screenshot of an example.

Hello Marcin, Thanks for the prompt reply. I think we both are probably proceeding on different tasks I shall explain the exact requirement: I have bamboo configured on my machine. I have set some predefined global variables in Bamboo. I have a bash script running on my local machine performing some operations. Some results are being generated. I want to now updates these results into the global variables that are present in my bamboo automatically from my local bash script itself. Can you tell me how should I be doing that? Regards, Hardik

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked Dec 06, 2018 in Jira Ops

I'm John Allspaw, Ask Me Anything about incident analysis and postmortems

I'm John Allspaw, co-founder of   Adaptive Capacity Labs, where we help teams use their incidents to learn and improve. We bring research-driven methods and approaches to drive effective inciden...

5,654 views 22 17
View question

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you