Get name of <default> branch for checkout rules
Hi,
I am having the same problem described in post https://teamcity-support.jetbrains.com/hc/en-us/community/posts/206900329-Have-actual-git-branch-instead-of-default-in-build-name. Unfortunately the latest comment was never answered there.
I would like to use the branch specification feature, and I need to retrieve the current branch name for my checkout rules. Searching for "branch" in the builtin parameters returns %teamcity.build.branch%, %teamcity.build.vcs.branch.<ProjectName>%, %vcsroot.branch%, and %vcsroot.<ProjectName>.branch%. None of these parameters returns the branch name when building the default branch (master).
Using %teamcity.build.branch% returns "<default>" instead of "master". %teamcity.build.vcs.branch.<ProjectName>% returns the very helpful string "???", and the others returns something like "refs/heads/master". Is it possible to only retrieve "master" somehow?
Please sign in to leave a comment.
Hi Dennis,
%teamcity.build.vcs.branch.xxx% will return the full name, but %teamcity.build.branch% returns the logical name. You can set your branch names to be converted into specific logical names by applying wildcards or parenthesis, for example:
+:refs/heads/(master) will yield master as the result of the parameter
+:refs/heads/* on a repo with "master" and "dev" branches, will yield "master" and "dev" (depending on which branch was triggered)
If you have "master" as the default branch, add +:refs/heads/(master) to your branch specification and that will modify the name. I've been running tests and it seems to work just fine on my end using 2019.1.1.
Hi Denis,
Did you perform your tests using VCS checkout rules? %teamcity.build.branch% does not work there (returns "<default>"). However using %teamcity.build.branch% seems to work fine when using it in a Build Step (returns "master"). I have tested with the following configuration:
VCSRoot 1 (this VCSRoot only has one branch, "master")
Default branch: master
Branch specification: +:refs/heads/(master)
VCSRoot 2
Default branch: master
Branch specification: +:refs/heads/*
Trying to checkout VCSRoot 2 with the following checkout rule results in a path with "<default>", throwing an error:
+:. => XXX\%teamcity.build.branch%
However, following your advice with the above VCSRoot configurations, I have been able to successfully use %vcsroot.<ProjectName>_<VCSRootName>.branch% in checkout rules. That variable seems to successfully resolve to the correct path name, and is a good workaround.
It seems I commented too early. %vcsroot.<ProjectName>_<VCSRootName>.branch% does NOT work in checkout rules. It seems to only work when building the default branch "master". However, no matter what other branch I try to start the build with, that variable always resolves to "master".
Hi Dennis,
There are some discordances between your tests and my results in my environment, so I'll try to address those:
-When using "%teamcity.build.branch%, it always resolves just fine, even in checkout rules, to whatever the requested branch for the build is. This is using 2019.1.1, if you are using an older release, this might have been fixed in the meantime.
-When using %teamcity.build.vcs.<vcs_root_id>.branch% it does *not* resolve when having multiple vcs roots. In my case it's trying to move the checkout files to "???" which obviuosly doesn't really work all that well. As far as I'm able to understand, this parameters are not filled up at the time of checkout.
-When using %vcsroot.*% it always returns the default branch for whatever VCS Root is chosen. This is the normal behavior of this parameter.
Also worth reminding that if you are using VCS Roots with the same branches, teamcity will assign a logical branch name to both VCS Roots and will be used in both, but if you are using different branches for different roots, the branch will be used in those that are available, and the default branch will be used in the rest.
If the setup you propose is the one you have in place, then you could set up VCS Root 1 to always move to "master", and VCS Root 2 to always move to %teamcity.build.branch%, which should work.
If it doesn't correspond as you are using multiple vcs roots or a more complex scenario, what I could recommend would be to set the checkout to not be performed automatically by settings in the VCS Settings of the build configuration to "Do not checkout files automatically", and then setting up a first build step that pulls the files manually into wherever you need. At that point, the %teamcity.build.vcs.*.branch% parameters are already filled up and you can use them fine to move the files into the appropriate folders.
Beyond that, I can only suggest sending an issue to our tracker about the parameters not being populated: https://youtrack.jetbrains.com/issues/TW . I'm not fully sure that this is a bug or expected behavior, as this is also a rather peculiar scenario. If you open an issue, please describe why you want to checkout into branch-separated folders, as running a build on the same agent in another branch would automatically replace the content.