How to run a build step conditionally in TeamCity

21,676

Solution 1

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).

Solution 2

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

Share:
21,676

Related videos on Youtube

orad
Author by

orad

Developers rule!... or they should if they don't.

Updated on July 03, 2020

Comments

  • orad
    orad almost 4 years

    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?

  • Admin
    Admin almost 6 years
    has anything changed since 2015? No conditional if steps, like octopus deploy?
  • IEatBagels
    IEatBagels about 5 years
    Still no change in 2019
  • conny
    conny over 4 years
    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 jetbrains.com/help/teamcity/… and then use that %param% as a conditional around each of your remaining steps (assumig they are of Comman Line runner type)
  • Danny Petrunov
    Danny Petrunov over 4 years
    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.
  • MikhailSP
    MikhailSP about 4 years
    It looks like this feature will be released very soon: youtrack.jetbrains.com/issue/…
  • Arvindh Mani
    Arvindh Mani about 4 years
    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.
  • Observer
    Observer almost 4 years
    This has been implemented in TeamCity 2020.1. See here for how to do it: blog.jetbrains.com/teamcity/2020/07/…
  • Kirill Metrik
    Kirill Metrik about 3 years
    What if I need to limit the step to several branches?