Conditionally execute a TeamCity build step
Asked Answered
E

4

49

I am working on defining a general-purpose build template for all our projects -- which I have placed at the "root project" level (thanks to this new feature of TeamCity 8). Some of our projects create an installer, which needs to be generated by a separate "build step" that runs a powershell script. However, some of our projects do not create this installer, and hence do not need this additional build step.

Is there a way to conditionally execute a build step, based on a build parameter? I thought that perhaps the "disable build step" feature could be leveraged here, but I don't see a way to define the enabled/disabled status of a step via a parameter.

Of course I could bake this conditional into the build step that performs the installer generation, but it would be cleaner if this could be handled from within teamcity itself.

Eppes answered 8/7, 2013 at 15:40 Comment(2)
I am very interested in this as well. Please post whatever solution you come up with.Earsplitting
My current solution is to always run the "create installer" build step, but to set things up such that the build doesn't fail if that step fails. Not ideal, but it handles my current use case (I don't always build an installer).Eppes
J
17

I've had need for conditional build steps on numerous occasions but alas, the feature does not exist at the moment. http://youtrack.jetbrains.com/issue/TW-17939

There's no reason you can't have a template used by many build configurations and then simply disable the build step in the build configs/projects that do not need to create the installer. Disabling the step in an individual build config does not affect the parent template it is based on.

Not as tidy as having a runtime/dynamic method built in to TC but I use it on occasion. I've done iwo has suggested as well.

Jennette answered 2/7, 2014 at 22:20 Comment(0)
Y
11

It seems TC does not support conditional runner step execution at their level except the execution policy (only if build status is successful, if all previous steps finished successfully, even if some prev steps failed, always) but that is not what you want.

So it seems you need to provide a custom system build param for your installer generator powershell build step like system.Generate which should be a bool (you can make it hidden, prompt, get its value based a on a config param, etc), and then in your powershell runner, add this:

param([int]$Generate)
if ($Generate) {
  Write-Host "generating installer..."
   # call your func()
} else {
   Write-Host "skip installer"
}

In your runner config, add -Generate %system.Generate% as a script argument (careful, not additional command argument!). %system.Generate% should expand to 1 or 0 based on its runtime value.

Yiyid answered 14/9, 2013 at 10:38 Comment(0)
L
7

As of 2020.1, it appears this feature is now supported.

See https://www.jetbrains.com/help/teamcity/what-s-new-in-teamcity-2020-1.html#WhatsNewinTeamCity2020.1-Conditionalbuildsteps

Now, you get granular control over build steps with the new execution conditions. When running a build, TeamCity will execute each step only if all its preconditions, configured by you, are satisfied in the current run.

In the advanced build step settings, click Add condition and specify a logical condition for any build parameter provided by the TeamCity server or agent. You can quickly select among the example conditions, such as:

  • Run the step only in the default branch
  • Run the step only in the release branch
  • Skip the step in personal builds

Or, create a custom condition, and TeamCity will automatically suggest supported parameters and values. A wide set of logical conditions and parameters is available.

Lour answered 24/6, 2020 at 10:51 Comment(0)
B
0

I've been researching this use case as I have a very similar problem to solve. The best solution I've found so far is the one stated in fwise's answer - to include all possible steps you might need within your template, but then on a case-by-case basis disable the unrequired steps for each project.

So in your case you would include your 'installer' build step at the root level, but disable it for any projects which inherit this template and don't have an installer.

For a single optional step this feels like a good approach but admittedly it wouldn't scale very well to large complex templates with many optional steps.

I picked this up from the TeamCity documentation here:

  • You can reorder build steps as needed. Note, that if you have a build configuration inherited from a template, you cannot reorder inherited build steps. However, you can insert custom build steps (not inherited) at any place and in any order, even before or between inherited build steps. Inherited build steps can be reordered in the original template only.
  • You can disable a build step temporarily or permanently, even if it is inherited from a build configuration template.
Bleier answered 26/7, 2016 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.