How to run a build step conditionally in TeamCity
Asked Answered
A

2

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?

Aragats answered 15/10, 2015 at 21:12 Comment(1)
Possible duplicate of Conditionally execute a TeamCity build stepPlotter
T
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).

Threemaster answered 19/10, 2015 at 13:37 Comment(7)
has anything changed since 2015? No conditional if steps, like octopus deploy?Steck
Still no change in 2019Grumble
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)Puckett
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.Ordeal
It looks like this feature will be released very soon: youtrack.jetbrains.com/issue/…Spencerspencerian
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.Quartas
This has been implemented in TeamCity 2020.1. See here for how to do it: blog.jetbrains.com/teamcity/2020/07/…Ghyll
M
17

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

Micaelamicah answered 2/7, 2020 at 12:18 Comment(2)
What if I need to limit the step to several branches?Hydrostatic
@KirillMetrik you can use "matches" condition and specify your branches in regex... e.g. teamcity.build.branch matches development|uatSpouse

© 2022 - 2024 — McMap. All rights reserved.