Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally execute a TeamCity build step

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.

like image 816
Stuart Lange Avatar asked Jul 08 '13 15:07

Stuart Lange


People also ask

How do you trigger a build in TeamCity?

To run a custom build with specific changes, open the build results page, go to the Changes tab, expand the required change, click the Run build with this change, and proceed with the options in the Run Custom Build dialog. Use HTTP request or REST API request to TeamCity to trigger a build.

How do you set building steps in TeamCity?

In Build Steps, click Auto-detect build steps, and then select the proposed steps you want to add to the current build configuration. You can change their settings afterwards. When scanning the repository, TeamCity progressively searches for project files on the two highest levels of the source tree.


2 Answers

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.

like image 94
fwise Avatar answered Sep 19 '22 05:09

fwise


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.

like image 27
iwo Avatar answered Sep 20 '22 05:09

iwo