Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get TeamCity to automatically merge a Git feature branch with master and fail the build in case of merge conflicts?

Our development/release cycle works like this:

  1. Developer creates a feature branch, implements a feature
  2. Developer indicates feature is ready for acceptance testing (UAT)
  3. Tester deploys feature branch and accepts (or rejects) feature

Accepted features are then merged into the master branch by the tester, and will therefore be released during the next release cycle (we deploy the trunk/master code weekly).

We're having frustrations with merge conflicts, because by the time the tester has UAT'ed the feature and discovered that it won't merge cleanly, the developer who worked in it has normally moved on to something else.

We're considering a solution whereby every feature branch is automatically merged, by TeamCity, against the current master branch, and any build resulting in a merge conflict is considered a failed build - this will give us early visibility on problematic merges so we can fix them sooner.

TeamCity doesn't appear to have built-in support for this workflow (i.e. when a push happens to branch X, checkout master, merge branch X onto it, build, unit-test, create package). Has anyone created a similar workflow using TeamCity and Github - using custom msbuild targets, perhaps?

EDIT: I should clarify that we're using Github but we're not currently using pull requests - sounds like this is something I should investigate. :)

like image 328
Dylan Beattie Avatar asked Jun 16 '13 12:06

Dylan Beattie


People also ask

How do I merge two branches automatically?

Enable automatic branch merging for a single repository Go to Repository settings > Branches. Under Automatic merging, select the On status and then select Save.

How do I merge a feature branch with a master?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.

How do I merge feature branches?

Merge branches Select the branch that you want to merge into the current branch, click Modify options and choose from the following: --no-ff : a merge commit will be created in all cases, even if the merge could be resolved as a fast-forward.


1 Answers

If you're using Github and Pull Requests then check out Hadi Hariri's blog post about how to get the pull request after it has been merged with master:

http://hadihariri.com/2013/02/06/automatically-building-pull-requests-from-github-with-teamcity/

Github does an automatic merge for every pull request and the resulting merge is available (though pretty much undocumented) as

git fetch origin +refs/pull/298/merge

where pull request id is 298. And so all merged pull requests can be fetched with a wildcard in place of the id in Teamcity and be built automatically. The branch specification would look like this:

+refs/pull/*/merge

EDIT: You say you're not using pull requests so I guess you could do this with some git commands. I haven't tried this out myself so these are only a few tips to get you started.

  1. Use the branch specification feature in Teamcity to get the branch. Set the checkout mode to check out the code on the agent
  2. In a build step merge the master branch into the target branch. If master is not available on the agent you might have to fetch it first. If you get any errors, then the build fails.

To check for merge conflicts, one of these strategies should work.

like image 195
Daniel Lee Avatar answered Oct 15 '22 13:10

Daniel Lee