It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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.

In inline script you may have used bamboo.variable similarly use bamboo_variable. 

True - I think the difference is that using `bamboo.variable` means that the Bamboo Java Process does a bulk string find/replace _before_ it creates the shell script that then gets executed.  We've shied away from using that, as we've mostly stopped doing "things" in the "Script" task - instead adding them to a shell script in the code repo, and executing from there.

In that case, `bamboo.variable` is not a valid env variable in shell or bash, so those fail.

1 vote
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
Published in Bamboo

Unable to add or edit Bitbucket Cloud repository in Bamboo

On 31 May, a GDPR-related change went live in the Bitbucket Cloud API that resulted in users not being able to create or edit Bitbucket Cloud Linked repositories in Bamboo. This API update removed t...

812 views 2 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you