How to run a build step conditionally in TeamCity
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.
Related videos on Youtube
Comments
-
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?-
sferencik over 8 yearsPossible duplicate of Conditionally execute a TeamCity build step
-
-
Admin almost 6 yearshas anything changed since 2015? No conditional if steps, like octopus deploy?
-
IEatBagels about 5 yearsStill no change in 2019
-
conny over 4 yearsYou 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 over 4 yearsLong 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 about 4 yearsIt looks like this feature will be released very soon: youtrack.jetbrains.com/issue/…
-
Arvindh Mani about 4 yearsAbout 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 almost 4 yearsThis has been implemented in TeamCity 2020.1. See here for how to do it: blog.jetbrains.com/teamcity/2020/07/…
-
Kirill Metrik about 3 yearsWhat if I need to limit the step to several branches?