简体   繁体   中英

Setting VCS branch via snapshot dependency in TeamCity

I have a pair of Git repos that are used in a TeamCity build configuration. If a build is triggered in the first repo, I need to run a command which determines which branch from the second repo should be used for the build (this is a non-trivial, and requires calling the GitHub API).

However, this can't be done as a build step (by then the VCS branches have already been determined), so instead, the main build configuration has a snapshot dependency on another build configuration (called Setup ) which runs the command and modifies a configuration parameter called RepoBranch by outputting the following to stdout (for example):

##teamcity[setParameter name='RepoBranch' value='refs/pull/12/merge']

Then, in the main build configuration I have:

  1. Created the RepoBranch parameter and set it to %dep.Setup.RepoBranch% .

  2. In the VCS Root configuration page for the second repo, set the Default branch to %RepoBranch%

If I trigger the build, the snapshot dependency works fine, and I see the RepoBranch configuration parameter has been updated in both the Setup build configuration and the main build configuration.

However, the master branch of the second repo is still being checked out and used instead of the branch name specified by the RepoBranch parameter.

This appears to be because both teamcity.build.vcs.branch.RepoName and teamcity.git.build.vcs.branch.RepoName configuration parameters actually used in the build are still refs/heads/master . Is this because I have the branch specification field for the second repo set to +:refs/heads/master perhaps? How to set this up correctly?

After a lot of experimentation, it turns out that TeamCity enforces that all build configurations with snapshot dependencies use exactly the same VCS versions. This is true even if the initial build configuration doesn't include the VCS root for which you want to change the branch on, so I guess TeamCity has already decided that the default branch is going to be used for all VCS roots other than the one which triggered the build.

In the end, the only solution seems to be to just go back to a single build configuration, and then add a build step at the start which switches branches where necessary (requires client-side checkout of sources). If SSH authentication is used, the TeamCity SSH agent Build Feature can be used to authenticate the necessary git (or svn ) commands.

To allow for the fact that other build steps may be using various TeamCity configuration parameters which contain the branch name and head revision, the initial build step should override these parameters by outputing the following values to stdout :

// Override all TeamCity parameters which contain the branch name printfn "##teamcity[setParameter name='teamcity.build.vcs.branch.%s' value='%s']" repoName branchName printfn "##teamcity[setParameter name='teamcity.git.build.vcs.branch.%s' value='%s']" repoName branchName // Override all TeamCity parameters which contain the SHA printfn "##teamcity[setParameter name='build.vcs.number.%s' value='%s']" repoName sha printfn "##teamcity[setParameter name='system.build.vcs.number.%s' value='%s']" repoName sha printfn "##teamcity[setParameter name='env.BUILD_VCS_NUMBER_%s' value='%s']" repoName sha

where branchName is the new branch that was switched to on the repo called repoName , and sha is the SHA of the head of that branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM