0

I am using team city with git. I am trying to take different action on the basis of the branch name from build is getting triggered. Build gets triggered from a branch in which a change gets checked in.

I have branch name like US12345, F12345, DE12345 and I want to perform different tests on these different builds.

For this I have added a command line step and try to run a custom script (Windwos batch script). Here is the script:

@ECHO OFF

echo "BRANCH Name: %teamcity.build.branch%"
SET BranchName="%teamcity.build.branch%"

if /I %%BranchName:~0,1%%==F (echo Working on a feature branch && <take action 1>) 
if /I %%BranchName:~0,2%%==US (echo Working on a story branch && <take action 2>)
if /I %%BranchName:~0,2%%==DE (echo Working on a defect branch && <take action 3>)
if /I %%BranchName:~0,2%%==QA (echo Working on a QA1 branch && <take action 4>)

In the above script, first I am assigning value of current branch name to a varialbe "BranchName".

Then in the first if condition, I am taking out first character of that variable and check if that character is equal to F. If that is true then I want to perform some set to tasks.

In the second if condition, I am taking out first two characters of "BranchName" and check if it is equal to US? If it is then I perform another different set of actions.

In the same way I want to do in third and fourth if condition.

Now the problem is, I am not able to get first few characters of a variable in the custom script because teamcity treats everything inside a "%%" as a reference parameter and we have to add those parameters in the build configuration.

Has anyone worked in these use case? Any help would be greatly appreciated.

  • I think you need use if /I %%%%BranchName.... see (https://stackoverflow.com/questions/4389946/using-the-percent-sign-in-teamcity-build-scripts) – Max Markov May 23 '17 at 15:52

2 Answers2

0

If you want to pass % to TeamCity, you should escape it with other %, i.e. for % it must be %%, for %% it must be %%%%

grundic
  • 4,641
  • 3
  • 31
  • 47
  • All I wanted to do is get first few character of the variable "BranchName". E.g. If I wanted to get first character of a feature branch in batch script, I would have written like: IF /I "%BranchName:~0,1%"=="F" (do something). – user2775279 May 23 '17 at 16:06
  • What if you change `SET BranchName="%teamcity.build.branch%"` to `SET BranchName="%%%teamcity.build.branch%:~-1%%%"`? Though I doubt. Try PowerShell, it might be easier. – grundic May 23 '17 at 16:14
  • But I cannot write the same script in team city custom script, because it will then start treating "BranchName:~0,1" as a new reference parameter and then I would have to add value for this. I thought that keeping the entire thing like %%BranchName:~0,1%%==F would not require to have BranchName:~0,1 specified as reference parameter. But this does not returns me first character of the BranchName. – user2775279 May 23 '17 at 16:19
  • Can you create a file and run it as a script? In this case `%` would not be treated as special TeamCity character. You can put it in your repo and checkout during the build. – grundic May 23 '17 at 16:22
0

You are cramming all your different branch builds into a single configuration, then using a bat file to trigger what gets build...that sounds complicated, and also a good choice for VSC trigger in TeamCity. I can a suited approach:

  1. Create one build configuration, then create a VCS trigger for your desired branch name to trigger the build.
  2. Copy that configuration N-times, one for each branch you want to build, then reconfigure the VSC trigger to have the correct branch name.

If you want to keep doing what you are doing, use PoSh instead of BAT script. Add a PowerShell step, change the script to be "Source Code" and enter this under "Script source":

$branch = "%teamcity.build.branch%"

if ($branch.StartsWith('F')) {
     echo "Working on a feature branch $branch"
}
elseif ($branch.StartsWith('US')) {
     echo "Working on a feature branch $branch"
}
elseif ($branch.StartsWith('DE')) {
     echo "Working on a feature branch $branch"
}
elseif ($branch.StartsWith('QA')) {
     echo "Working on a feature branch $branch"
}
Ben Richards
  • 3,437
  • 1
  • 14
  • 18