How do you add bamboo variable in powershell?

Mihai Sava November 8, 2016

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

0 votes
fabcrm January 22, 2021

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.

0 votes
fabcrm January 22, 2021

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.

0 votes
Thijs Kuperus March 13, 2018

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}

Jacek Szybisz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 7, 2019

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

Thijs Kuperus January 7, 2019

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
0 votes
Timothy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 8, 2016
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).
Mihai Sava November 10, 2016

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events