25

This is a more generic version of this question: How to run a build step on a specific branch only?

For example, I can use a PowerShell script to run MSBuild if '%teamcity.build.branch.is_default%' -eq 'true' or if '%teamcity.build.branch%' -eq 'master' but then I will miss the collapsible log that comes with the TeamCity MSBuild build runner. Isn't there any easier way to conditionally run a build step?

orad
  • 12,746
  • 18
  • 69
  • 107
  • 1
    Possible duplicate of [Conditionally execute a TeamCity build step](http://stackoverflow.com/questions/17530688/conditionally-execute-a-teamcity-build-step) – sferencik Oct 16 '15 at 06:58

2 Answers2

28

It is not possible to execute build step based on condition. Vote for the related request: https://youtrack.jetbrains.com/issue/TW-17939.
The recommended approach is to create separate build configuration for each branch. You can use templates to simplify the setup. In this case it will be easier to interpret the results and the statistics of the builds will be informative.
Also see the related answer (hack is suggested).

Community
  • 1
  • 1
Alina Mishina
  • 3,140
  • 2
  • 19
  • 31
  • has anything changed since 2015? No conditional if steps, like octopus deploy? –  Jul 04 '18 at 17:39
  • 7
    Still no change in 2019 – IEatBagels Mar 20 '19 at 14:31
  • You can solve this either by prepending a build with a separate first-tier build, which only runs a custom script evaluating the condition, marks itself as "Cancelled" if no change, and then have the REAL build snapshot-depend with "On cancelled dependency: Cancel build" ... or ... you can set a parameter in your first buildstep using https://www.jetbrains.com/help/teamcity/build-script-interaction-with-teamcity.html and then use that %param% as a conditional around each of your remaining steps (assumig they are of Comman Line runner type) – conny Sep 27 '19 at 12:23
  • Long past due for this feature, especially when using Git, where you can configure additional branches and pull requests to trigger builds. Having to maintain a separate config for each of over a dozen of these is quite painful, even with templates. In our case the only variable build step in our pipeline is _where_ to deploy the binaries, and we have a very simple Powershell script that can make that switch based on the branch/PR. – lesscode Oct 29 '19 at 21:13
  • 2
    It looks like this feature will be released very soon: https://youtrack.jetbrains.com/issue/TW-17939#focus=streamItem-27-3892126.0-0 – MikhailSP Apr 04 '20 at 19:20
  • About time, but I'm glad they finally got around to it. I've been praying for such a feature right from the time we started using TC. – Arvindh Mani Apr 22 '20 at 23:31
  • This has been implemented in TeamCity 2020.1. See here for how to do it: https://blog.jetbrains.com/teamcity/2020/07/new-in-2020-1-conditional-build-steps/ – Observer Jul 06 '20 at 12:13
8

JetBrains introduced support for conditional build steps in TeamCity 2020.1.

Here is a snippet of their blog post announcing this new feature:

Have you ever wanted to execute different command line scripts on different platforms, or deploy changes in different branches to different staging servers? Now you’re free to do just about anything! TeamCity 2020.1 allows you to specify conditions for your build steps and execute them only if the criteria are met.

conditional build steps demo

0xced
  • 21,773
  • 10
  • 89
  • 238