Conditionally execute a TeamCity build step

旧巷老猫 提交于 2019-11-29 02:47:58

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.

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.

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!