Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Bamboo variable for branchname without prefix

paulvandrunendigitaslbi January 23, 2018

Hi we would like to pass the branchname in a buildtask. But currently if it is a feature brach (Bitbucket). It starts with feature/branchname and therefore our builds are not working. Is there a way to not use the prefix or to rewrite the / to a - character.

Hopefully someone can help me.

We are using Gulp tasks to create a package and upload this package to our server.

 

Thank you in advance.

3 answers

0 votes
Bruno Marotta _ease solutions_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 29, 2022

A Bash script to clean the folders in the branch name:

branchNameClean=${bamboo.planRepository.branchName}
echo "Current branch name: ${branchNameClean}"
branchNameClean=${branchNameClean#*/}
echo "Clean branch name: ${branchNameClean}"

If you expect more than one folder you can just repeat the line many times

branchNameClean=${branchNameClean#*/}
branchNameClean=${branchNameClean#*/}
branchNameClean=${branchNameClean#*/}


0 votes
paulvandrunendigitaslbi January 24, 2018

I have solved it already. 

 

The following code is working for me.

 

New-Variable -Name "bbranch" -Value ${bamboo.planRepository.branchName}
echo "nieuwe : $bbranch"
If($bbranch -Match 'feature'){
New-Variable -Name "branchprefix" -Value 'feature'
echo "branch prefix = : $branchprefix "
$bbranch -replace 'Luxe/feature/',''
New-Variable -Name "branchname" -Value test
$branchname = $bbranch -replace 'Luxe/feature/',''
echo "branchname = : $branchname"

}ElseIf ($bbranch -Match 'release'){
New-Variable -Name "branchprefix" -Value 'release'
echo "branch prefix = : $branchprefix "
$bbranch -replace 'Luxe/release/',''
New-Variable -Name "branchname" -Value test
$branchname = $bbranch -replace 'Luxe/release/',''
echo "branchname = : $branchname"

}ElseIf ($bbranch -Match 'hotfix'){
New-Variable -Name "branchprefix" -Value 'hotfix'
echo "branch prefix = : $branchprefix "
$bbranch -replace 'Luxe/hotfix/',''
New-Variable -Name "branchname" -Value test
$branchname = $bbranch -replace 'Luxe/hotfix/',''
echo "branchname = : $branchname"

}
ElseIf ($bbranch -Match 'Luxe_Development'){
New-Variable -Name "branchprefix" -Value 'Development'
echo "branch prefix = : $branchprefix "
$bbranch -replace 'Luxe/',''
New-Variable -Name "branchname" -Value test
$branchname = $bbranch -replace 'Luxe/',''
echo "branchname = : $branchname"

}

0 votes
Steffen Opel _Utoolity_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2018

Assuming you are using the 'bamboo.planRepository.branchName' Bamboo variable to get the branchname, you can manipulate the variable value in a Script task, write the adjusted value to a file, and then inject it again as a new variable by means of one of the following tasks:

You can then refer to the newly injected variable from subsequent tasks.

paulvandrunendigitaslbi January 23, 2018

HI @Steffen Opel _Utoolity_,

 

Thank you for your answer. How would you save it to a file via a script task?

 

thanks in advance.

Steffen Opel _Utoolity_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2018

Hi @paulvandrunendigitaslbi - all Bamboo variables are available as environment variables in the Script task, with details depending on which shell you are using. Unfortunately this is not well documented by Atlassian indeed, please refer to e.g. Utoolity's section on Environment variables for the specific syntax that applies to your scenario.

Once you have access to the original variable in your shell, you can derive a new value with the shell specific tooling for string manipulation and redirect the output to a new file that will be used for the variable injection - for example from PowerShell you could create the Bamboo Variables task format as follows:

PowerShell

# either replace '/' infix with '-' ...
$bamboo_planRepository_branchName.Replace('feature/', 'newBranchName=feature-') > variables.properties

# ... or drop 'feature/' prefix
$bamboo_planRepository_branchName.Replace('feature/', 'newBranchName=') > variables.properties

 

paulvandrunendigitaslbi January 24, 2018

Hi @Steffen Opel _Utoolity_,

 

I have created the following script but for some reason it does not work.

 

If(${bamboo.planRepository.branchName} -Match 'feature'){
$branchprefix = 'feature'
echo "branch prefix = : $branchprefix "
$branchname = $bamboo_planRepository_branchName -replace 'Luxe/feature/',''
echo "branchname = : $branchname"
}ElseIf (${bamboo.planRepository.branchName} -Match 'release'){
$branchprefix = 'release'
echo "branch prefix = : $branchprefix "
$branchname = $bamboo_planRepository_branchName -replace 'Luxe/release/',''
echo "branchname = : $branchname"
}ElseIf (${bamboo.planRepository.branchName} -Match 'hotfix'){
$branchprefix = 'hotfix'
echo "branch prefix = : $branchprefix "
$branchname = $bamboo_planRepository_branchName -replace 'Luxe/hotfix/',''
echo "branchname = : $branchname"
}

 I am getting the following error in the log. 

 

error 24-Jan-2018 10:08:16 Luxe/release/20180124 : The term 'Luxe/release/20180124' is not recognized as t
error 24-Jan-2018 10:08:16 he name of a cmdlet, function, script file, or operable program. Check the spel
error 24-Jan-2018 10:08:16 ling of the name, or if a path was included, verify that the path is correct an
error 24-Jan-2018 10:08:16 d try again.
error 24-Jan-2018 10:08:16 At D:\Bamboo-home\temp\LPW-TB10-JOB1-2-ScriptBuildTask-3251863757090287516.ps1:
error 24-Jan-2018 10:08:16 4 char:4
error 24-Jan-2018 10:08:16 + If(Luxe/release/20180124 -Match 'feature'){
error 24-Jan-2018 10:08:16 +    ~~~~~~~~~~~~~~~~~~~~~
error 24-Jan-2018 10:08:16     + CategoryInfo          : ObjectNotFound: (Luxe/release/20180124:String) [ 
error 24-Jan-2018 10:08:16    ], CommandNotFoundException
error 24-Jan-2018 10:08:16     + FullyQualifiedErrorId : CommandNotFoundException
error 24-Jan-2018 10:08:16  



Do you have an idea on how i can solve this?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events