Features How-To's Tips & Tricks

Conditional Build Steps in TeamCity

In TeamCity 2020.1, we have introduced a highly demanded feature – conditional build steps. With new execution conditions, you can control whether or not a given build step is executed in every build run, depending on the current environment and parameters.

In this video demonstration we:

  • explain how to add an execution condition to a step;
  • show how to elevate your building experience by creating custom conditions based on build parameters.

Here is a quick recap of the video:

Adding execution conditions

To add execution conditions to a build step:

  1. Open the step’s advanced settings.
  2. Opposite the Execute step field, click Add condition.
  3. Select any of the example conditions (e.g. run the step only in the default branch) or add a custom one (e.g. run the step only on the specific agent OS).
  4. Add as many conditions as needed. You can change and delete them anytime.
  5. Save the build step settings.

In every build run, this conditional step will only be executed if all its execution conditions are satisfied.

Adding parameter-based conditions

By combining execution conditions with other classic features of TeamCity, you can significantly improve your building experience. One great example is creating a condition that is based on a build parameter.

Let’s consider the following use case:

Your build configuration can be deployed to any of the three environments (QA, Staging, and Production). By default, it is deployed to QA, but you can run a custom build and select a different environment. One of the build steps contains a script that must be executed only when deployed to Production.

With conditional steps, it is easy to arrange:

  1. Add a build parameter with the following settings:
    • Name: Environment
    • Spec | Display: Prompt
    • Spec | Type: Select
    • Items: a newline-delimited list of environment names (QA, Staging, Production)
  2. In the production-only build step, click Add condition and select Other condition.
  3. Enter Environment as the parameter name. TeamCity will suggest all matching results. Choose the “equal” condition. Enter Production as the expected value of the Environment parameter.
  4. Save the build step settings.

Now, you can click Deploy to run a custom build. Since you selected the Prompt display type for the parameter, TeamCity will ask you about the target environment for this build.

If you select anything other than Production, the production-only build step will be skipped in this custom build run:

That’s it for the tutorial!

Refer to our documentation for more information and leave your feedback about the feature in the comments.

Happy building!

image description