Gaui
Gaui

Reputation: 8959

Running tests on feature branches

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:

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.

Upvotes: 9

Views: 6175

Answers (2)

Jack Ukleja
Jack Ukleja

Reputation: 13521

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.

Upvotes: 3

Sam Holder
Sam Holder

Reputation: 32946

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.

Upvotes: 11

Related Questions