How I can access global and/or plan specific variables from python scripts, if they are called by a remote agent?

Hello. I'm currently evaluating Bamboo for our build equipment. We have mostly python scripts to run different steps. My current issue is, how I can access global and/or plan specific variables by the python scripts, if they are called by a remote agent?

I read your documentation about the usage of global variables but I don't see the link to python. I wouldn't like to use an environment variable because this must be defined for each task. I'm looking for a solution which works for a entire plan, where I can access e.g. on the subversion revision number or the build number.

Thanks for any help.

1 answer

1 accepted

Hi Michael,

Bamboo wil pass down any build specific variable/global variable you have defined to your build. If you don't want to specify a global Bamboo variable then you can specify a plan specific variable as per;

http://confluence.atlassian.com/display/BAMBOO/Defining+Plan+Variables

You can then access this variables using a simlar code;

import os
print os.environ['foo']

Hope that help! Let me know if you have any question?

Regards

Ajay.

Hi Ajay. Thanks so far for the fast answer. To ensure I understand it right, if I create a bamboo variable called 'planx.myvariable' I can access on it by python with os.environ['planx.myvariable']? This would also mean, that I can access on bamboo specific variables like os.environ['bamboo.repository.revision.number']. Right? Regards Michael
Hi Ajay.
Thanks for the fast answer. To ensure I understand it right, if I create a plan specific variable called 'planx.myvariable' I can access on it by python with os.environ['planx.myvariable']?
This would also mean, that I can access on bamboo specific variables like os.environ['bamboo.repository.revision.number']. Right?
Regards
Michael
Hi Michael, I converted your answer to a comment, since it's better placed here inline with Ajay's answer.
Hi Michael, yes you can. However all variables in bamboo are prefixed with 'bamboo.'. So your 'planx.myvariable' becomes 'bamboo.planx.myvariable'

os.environ['bamboo.repository.revision.number'] does not work for me in python. I am getting the below error.

bwd=os.environ['bamboo.build.working.directory']

File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__

raise KeyError(key)

KeyError: 'bamboo.build.working.directory'


For anyone who's bothering with the similar problem: in Python ran on Bamboo the os.environ contains Bamboo's variables in form where every dots are replaced with underscores, e.g. variable "bamboo.build.working.directory" should be resolved as: os.environ["bamboo_build_working_directory"]

i still get error not sure why.

i get error
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/lib64/python2.6/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: ''

Is there a way to successfully invoke bamboo plan variables within a python inline script just like it works for a shell or batch script.

Any help here would be greatly appreciated.

Cheers,

Ashley

Is this issue resolved now? I am still looking for a proper solution to this.

Suggest an answer

Log in or Join to answer
Community showcase
Renan Battaglin
Published May 18, 2017 in Bamboo

FAQ: How to Upgrade Bamboo Server

Bamboo 5.9 will no longer be supported after June 12, 2017. What does this mean? As part of our End of Life policy, Atlassian supports major versions for two years after the first major iteratio...

1,075 views 0 5
Read article

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot