9

I have a build configuration with a test VCS root that connects to git branch dev, 3 build steps and 1 trigger. These are my build steps:

  • Build tests
  • Run tests
  • Build & Deploy

I would like to run all of these build steps for branch dev but only two of them (build and run tests) for branches matching feature/*. I want this to be displayed under my build configuration. So the build configuration has a default dev branch that runs tests and deploys, but the feature/* additional branches only run tests.

How can I achieve this?

If I add /refs/heads/(feature/*) to the branch specification (below default branch), this works perfectly, but it always deploys - which I don't want.

enter image description here

Edit 1: There seems to be a variable available named %teamcity.build.branch% that you can use. But how to do a conditional in the deployment step to check if the branch is the dev branch. I'm not sure.

Edit 2: There is also a variable name %vcsroot.branch% that is the name of the default branch in the VCS root. So we still need a condition that checks if the %teamcity.build.branch% variable equals %vcsroot.branch%, then run the deployment step.

Gaui
  • 8,723
  • 16
  • 64
  • 91

2 Answers2

11

The way to achieve what you want is to split your build into 2 builds and have dependencies between them. then you can have separate triggers between the builds.

So split the builds so you have build A which contains

  • Build tests
  • Run tests

and build B which contains

  • Build & Deploy

Give build B a snapshot dependency on build A.

Then add a trigger to build A when a VCS check-in is detected. This will ensure that the build and tests are run on any feature branch.

Also add a trigger on build B when a VCS check-in is detected, but edit the rules so that you exclude your feature branches. When a check-in on any other branch is detected build B will start, but it needs build A to finish first so it will queue that up first, and won't start if build A fails (assuming you set that in the options)

UPDATE If this is too much hassle then you might be able to play a small trick but creating a build step between Run tests and Build and Deploy which calls a command line or powershell script. Call the script passing in %teamcity.build.branch% and then the script can check if it was called with dev and Exit 0 if so and Exit -1 if not and this step should then fail the build and prevent the deploy. This will mean that the build will seem failed but will prevent the step you want to avoid from running. It may be possible to get teamcity not to report the build as failing if this step fails, I'm not certain

You other option is that you write a script which does the build and deploy manually and then call this script passing in the %teamcity.build.branch% and exit early if its not dev and only go on to do the actual build and deploy if it is dev. this won't result in a failed build but means you have to write scripts to do the things TeamCity is doing for you now.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • There must be another simpler way? We have 100+ build configurations (test/staging/live). This is way too much hassle. – Gaui Jan 29 '15 at 18:29
  • You didn't mention any constraint about number of build configurations. I believe this is the 'correct' way, but i'll update the answer with a hack that *might* work. – Sam Holder Jan 29 '15 at 18:38
  • ok updated the answer. i'll look to see if you can make the build seem passed even if a step fails. Which version of TC are you running? – Sam Holder Jan 29 '15 at 18:46
  • TeamCity Enterprise 9.0 (build 32060). Thank you for the update. I exactly thought of something similar (the script approach). :) – Gaui Jan 29 '15 at 18:48
3

You can do this by creating a "test" build step (e.g. powershell script) to test if your %teamcity.build.branch% matches your feature/* pattern. You only run the following steps (in this case Build & Deploy) if previous steps were successful. Obviously the "test" step should not fail the build.

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113