Working with Feature Branches
Feature Branches in distributed version control systems (DVCS) allow you to work on a feature independently from the main development and commit all the changes for the feature onto the branch, merging the changes into the main branch when your feature is complete. This approach brings a number of advantages to software development teams; however, in continuous integration servers that do not have dedicated support for it, it also causes a number of problems, like constant build configurations duplication, poor visibility, and, in the end, loss of control over the process.
TeamCity support for feature branches is continuously increasing, starting from partial support in version 7.0 by Branch Remote Run Trigger, that automatically starts a new personal build each time TeamCity detects changes in particular branches of the VCS roots of the build configuration; moving on to TeamCity 7.1, allowing you to automate the process and ensuring visibility of branches all over the interface. In TeamCity 8.0, the feature branches support is taken to the next level with a number of improvements. TeamCity 8.1 introduced Automatic Merge functionality to merge a branch into another after a successful build.
Supported version control systems
Feature branches are supported for Gitand Mercurial; since TeamCity 10 Perforce branch streams support is added (experimental).
Configuring branches
To start working with DVCS branches, you need to configure which branches need to be watched for changes. This is done in the General Settings section of a Gitor MercurialVCS root via the Branch Specification field which accepts a list of branch names or patterns. TeamCity monitors the branches matched by the branch specification plus the default branch.
Once you've configured the branch specification, TeamCity will start to monitor these branches for changes. If your build configuration has a VCS trigger and a change is found in some branch, TeamCity will trigger a build in this branch. From the build configuration home page you'll also be able to filter the history, change log, pending changes and issue log by the branch name. Branch names will also appear in the custom build dialog, so you'll be able to manually trigger a custom build on a branch too.
The syntax of the branch specification field is newline-delimited list of "+|-:branch_name
" rules. "+:" rules include the matching branches into the list, the "-:" rules exclude the branches from the list. Each rule can have one optional "*" placeholder which matches one or more characters. e.g. "+:refs/heads/teamcity*
" matches all branches starting with refs/heads/teamcity
and at least one additional character. The branch with refs/heads/teamcity
will not be matched. The "branch_name" is VCS-specific, i.e. refs/heads/master in Git:
The part of the branch name matched by the asterisk (*) wildcard becomes the short branch name to be displayed in the TeamCity user-level interface (also known as the logical branch name). The line can also contain optional parentheses which, when present, denote the part of the pattern to be used as the logical name instead of just *-matched symbols.
You can use parameters in the branch specification.
When a single VCS branch is matched by several lines of the branch specification, the most specific (least characters matched by pattern) last rule applies.
That is: If the specification contains an exact pattern matching the branch (i.e. a pattern without the *
wildcard), then the last such pattern is used. So if you have a specification like this:
then the last pattern will win and the branch will be excluded. If a branch specification has several patterns with the *
wildcard, then TeamCity selects the pattern producing the shortest logical name. The branch specification
will include the refs/heads/v1/hotfix
branch (because v1
is shorter than hotfix
). If 2 patterns with *
wildcard produce logical names of the same length, then the last pattern wins.
The branch specification supports comments as lines beginning with "#"
.
There is also a special escaping syntax defined via "#! escape: CHARACTER" syntax: e.g. to use round brackets in a branch name, you need to escape them. Let's say you want to track the release-(7.1)
branch: to do that, specify an escaping symbol as the first line in the specification. For Mercurial, the following branch specification does that:
Default branch
When configuring a VCS root for DVCS, you need to specify the branch name to be used as the default one in case a branch name was not specified. For example, if someone clicks on a Run button, TeamCity will create a build in the default branch.
The default branch is always implicitly included into the branch specification. In the TeamCity UI the default branch is marked with darker background of the branch marker.
If there are several VCS roots or a snapshot dependency and they have default branch set to different values, the build in the default branch will be marked with the predefined "<default>" name.
Since TeamCity 2017.1, it is possible to disable building in the default branch if you want to build pull requests only.
Logical branch name
A logical branch name is a branch name shown in the user interface for the builds and on build configuration level. A logical branch name is regularly a part of the full VCS-specific branch name. It is calculated by applying a branch specification to the branch name from the version control.
For example, if the branch specification is defined like this:
then the part matched by * (e.g. master
) is a logical branch name.
If the branch specification pattern uses parentheses, the logical name then is made up of the part of the name within the parentheses; to see thev8.1/feature1
logical name displayed in the UI for the VCS branch refs/heads/v8.1/feature1
, use this:
You do not need to include the default branch into the branch specification as it is already included there implicitly. But, if you want to have some short logical branch name for the default branch in the UI, e.g. master,
you can include it in the branch specification and use the parentheses:
Builds
Builds from branches are easily recognizable in the TeamCity UI, because they are marked with a special label:
You can also filter history by a branch name if you're interested in a particular branch. TeamCity assigns a branch label to the builds from the default branch too.
Changes
For each build TeamCity shows changes included in it. For builds from branches the changes calculation process takes the branch into account and presents you with the changes relevant to the build branch. The change log with its graph of commits will help you understand what is going on in the monitored branches. If the Show builds and Show graph options are enabled in the change log, TeamCity will display build markers on the graph.
Active branches
In a build configuration with configured branches, the Overview page shows active branches.
A number of parameters define whether a branch is active. The parameters can be changed either in a build configuration (this will affect one build configuration only), project, or in the internal properties (this defines defaults for the entire server). A parameter in the configuration overrides a parameter in the internal properties.
A branch is considered active if:
it is present in the VCS repository and has recent commits (i.e. commits with the age less than the value of
teamcity.activeVcsBranch.age.days
parameter, 7 days by default).or it has recent builds (i.e. builds with the age less than the value of
teamcity.activeBuildBranch.age.hours
parameter, 24 hours by default). A closed VCS branch with builds will still be displayed as active during 24 hours after last build. To remove closed branches from display, setteamcity.activeBuildBranch.age.hours=0
.
Tests
TeamCity tries to detect new failing tests in a build, and for those tests which are not new, you can see in which build the test started to fail. This functionality is aware of branches too, i.e. when the first build is calculated, TeamCity traverses builds from the same branch.
Additionally, a branch filter is available on the test details page and you can see a history of test passes or failures in a single branch.
Failure Conditions
If build fail condition is configured as follows: build metric has changed comparing to a last successful/finished/pinned build, then the build from the same branch will be used. If there is no suitable build on the same branch, then build from default branch is used and the corresponding message is added to the build log.
Triggers
The VCS trigger is fully aware of branches and will trigger a build once a check-in is detected in a branch. All VCS trigger options like per-checkin triggering, quiet period, and triggering rules are directly available for builds from branches. By default, the Schedule and Finish build trigger will watch for builds in the default branch only.
Additionally, a branch filter can be specified for the VCS, Schedule and Finish build triggers.
Dependencies
If a build configuration with branches has snapshot dependencies on other build configurations with branches, then when a build in a branch is triggered, the other builds in the chain will also get the branch associated, if the branches in the VCS roots of the builds have the same logical name and this branch is not excluded by the branch specification. The VCS roots of the builds can point to different repositories, but the logical branch name must be the same.
If this condition is met, the branches with this name will be checked out and all the builds down the chain (which the build triggered depends on) and all the builds up the chain (depending on the triggered build) will be marked with the same branch. Otherwise the default branch will be checked out.
It is possible to configure artifact dependencies to retrieve artifacts from a build from a specific branch: artifact dependencies will use builds from the branch specified. The same applies to the Schedule and Finish Build triggers.
Notifications
All notification rules except "My changes" will only notify you on builds from the default branch. At the same time, the "My changes" rule will work for builds from all available branches.
Build configuration status
The Build Configuration status is calculated based on the builds from the default branch only. Consequently, per-configuration investigation works for builds from the default branch. For example, a successful build from a non-default branch will not remove a per-configuration investigation, but a successful build from the default branch will.
Multiple VCS roots
If your build configuration uses more than one VCS root and you specified branches to monitor in both VCS roots, the way the builds are triggered is more complicated.
The VCS trigger groups branches from several VCS roots by logical branch names. When some root does not have a branch from the other root, its default branch is used. For example, you have 2 VCS roots, both have the default branch refs/heads/master
, the first root has the branch specification refs/heads/7.1/*
and changes in branches refs/heads/7.1/feature1
and refs/heads/7.1/feature2
, the second root has the specification refs/heads/devel/*
and changes in branch refs/heads/devel/feature1
. In this case VCS trigger runs 3 builds with revisions from following branches combinations:
root1 | root2 |
---|---|
refs/heads/master | refs/heads/master |
refs/heads/7.1/feature1 | refs/heads/devel/feature1 |
refs/heads/7.1/feature2 | refs/heads/master |
Build parameters
If you need to get the branch name in the build script or use it in other build configuration settings as a parameter, please refer to Predefined Build Parameters.
Clean-up
Clean-up rules are applied independently to each active branch.
Manual branch merging
You can merge branches in TeamCity manually, e.g. if you want to merge branches only after a code review / approval, or if you want to perform the merge despite the tests failure in a branch.
To merge sources manually: Open the build results page, click the Actions menu in the top-right corner and select "Merge this build sources...". The dialog that appears enables you to select the destination branch and add a commit message (required).
It is also possible to merge branches automatically.