Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

How do you add bamboo variable in powershell?

Hello All,

I'm having hard times to figure this out, I have the following powershell script , simple one:

$BambooPath = 'C:\StartFolder'
$RemoteWebPath = 'C:\DestFolder'
Copy-Item -Path $BambooPath -Destination $RemoteWebPath -Recurse

I wan't to replace in $BambooPath the C:\StartFolder with the bamboo.working.directory variable...anyone has an ideea how to do that?

4 answers

How to use Bamboo Variables in an EXTERNAL file powershell script.

This works when your powershell script is file based. You are calling your script using a Script Task.

Interpreter = Windows Powershell
Script location = File
Script file = [full path and filename of your powershell script]

From within your file based powershell script:

# notice the use of quotes here if you want to use the .replace extension method.
$mybranch = "$Env:bamboo_planRepository_branch".replace("/","-")

$buildNumber = $Env:bamboo_buildNumber

$myCustomVariable1 = $Env:bamboo_myCustomVariable1

The main difference between INLINE and EXTERNAL file base powershell scripts is the way you access the variable. In this example, simply prefix the bamboo variable with $Env: and replace the dots with underscores.

I hope this simple clarification with examples will help others stumbling over this question in the future.

How to use Bamboo Variables in an INLINE powershell script.

Critically, this ONLY works if your powershell script is INLINE. If your powershell script is in a file somewhere, then this will not work

Examples for inline use of bamboo variables in a powershell script:

$mybranch = "${bamboo.planRepository.branch}".replace("/","-")

$buildNumber = "${bamboo.buildNumber}"

$myCustomVariable1 = "${bamboo.myCustomVariable1}"

Whats going on:

$mybranch is a local variable, and is assigning the value of bamboo.planRepository.branch variable. In this example, we are replacing the 'forward slashes' with dashes. I thought that might help demonstrate use of the variables too.

One solution we found rather easy is to use the variables as arguments.

In your case you should use 

$BambooPath = $args[0]; 

And enter as argument : ${bamboo.build.working.directory}

This is not very helpful.  Maybe you can provide more detailed explanation how you can set it. I am trying to set some build variable from PS script

Hi Jacek,

 

We use a script that needs to know in which state it's used (build or deploy) and another parameter. In the Argument we have:

build ${bamboo.repository.git.branch}

Note that the parameters are split with a space.

 

For getting the parameters in your Powershell code use this.

$state= $args[0]; //returns "build"
$branch = $args[1]; //returns something like "master" or "feature/feature-name"

//Followed by your own code
For task configuration fields, use the syntax ${bamboo.myvariablename}. For inline scripts, variables are exposed as shell environment variables which can be accessed using the syntax $bamboo_MY_VARIABLE_NAME (Linux/Mac OS X) or %BAMBOO_MY_VARIABLE_NAME% (Windows).

If it were that easy...It's not working. sad

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bamboo

Bamboo Data Center 8.1 is now available

G’day Bamboo customers, Bamboo DC 8.1 is now available with it the following features and programs: SAML 2.0, OpenID Connect, and Crowd SSO In order to help admins with a simplified user manage...

204 views 0 4
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