Bamboo variable for branchname without prefix
Asked by paulvandrunendigitaslbi:

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.


Answers:
author: Bruno Marotta _ease solutions_
body: 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#*/}


```


author: paulvandrunendigitaslbi
body: 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"
 
}

```


author: Steffen Opel _Utoolity_
body: Assuming you are using the *'bamboo.planRepository.branchName'* [Bamboo variable](https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html#Bamboovariables-Build-specificvariables) to get the branchname, you can manipulate the variable value in a [Script task](https://confluence.atlassian.com/bamboo/script-289277046.html), write the adjusted value to a file, and then inject it again as a new variable by means of one of the following tasks:

* the built-in [Bamboo Variables task](https://confluence.atlassian.com/bamboo/configuring-a-variables-task-687213475.html)
* David Ehringer's [YAML and JSON Variables Loader](https://marketplace.atlassian.com/plugins/org.gaptap.bamboo.yaml.yaml-variables-loader/server/overview)
* llucIT Software's [Variable Extractor Tasks for Bamboo](https://marketplace.atlassian.com/plugins/com.illucit.bamboo-variables-extractor-plugin/server/overview)

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

comments: author: paulvandrunendigitaslbi
body: HI [@Steffen Opel _Utoolity_](/t5/user/viewprofilepage/user-id/781567),

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

thanks in advance.


author: Steffen Opel _Utoolity_
body: Hi [@paulvandrunendigitaslbi](/t5/user/viewprofilepage/user-id/626337) - 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](https://utoolity.atlassian.net/wiki/spaces/IFAWS/pages/42434587/Using+the+AWS+Credentials+Variables+task+in+Bamboo#UsingtheAWSCredentialsVariablestaskinBamboo-Environmentvariables) 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](https://confluence.atlassian.com/bamboo/configuring-a-variables-task-687213475.html?_ga=2.153942906.39561955.1515673801-951020681.1502237296) 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
```


author: paulvandrunendigitaslbi
body: Hi [@Steffen Opel _Utoolity_](/t5/user/viewprofilepage/user-id/781567),

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 terror 24-Jan-2018 10:08:16 he name of a cmdlet, function, script file, or operable program. Check the spelerror 24-Jan-2018 10:08:16 ling of the name, or if a path was included, verify that the path is correct anerror 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:4error 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    ], CommandNotFoundExceptionerror 24-Jan-2018 10:08:16     + FullyQualifiedErrorId : CommandNotFoundExceptionerror 24-Jan-2018 10:08:16  
```

<br />

<br />

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