Azure DevOps Parameters Best Practices for Successful Pipelines

Author

Reads 718

Woman in focus working on software development remotely on laptop indoors.
Credit: pexels.com, Woman in focus working on software development remotely on laptop indoors.

To create successful pipelines in Azure DevOps, it's essential to follow best practices for parameters. Parameters are used to store and retrieve values in a pipeline, and they can be defined at the pipeline, stage, or job level.

Use a consistent naming convention for your parameters to avoid confusion and make them easier to understand. For example, you can use a prefix like "env_" to indicate that a parameter is an environment variable.

In Azure DevOps, you can define parameters at the pipeline level, which allows you to reuse them across multiple jobs and stages. This can simplify your pipeline configuration and reduce duplication of effort.

Readers also liked: Azure Pipelines Parameters

Defining and Using Parameters

Defining Parameters requires specifying a type, such as boolean, number, object, or string, and many types that deal with various Pipelines objects. You can provide a default value in the YAML by using the 'default' key.

Each Parameter must have a value, which can be provided in multiple ways: by using a default value, running the Pipeline and entering a value manually, or by using the first value listed in the 'values' key if neither of the above options are used. You can also use a combination of these methods.

Credit: youtube.com, Adding Pipeline Parameters - Azure DevOps

To specify the parameter name, you can use one of two syntaxes, both of which produce the same result. The syntaxes are shown in the code below, and both are acceptable.

Here are the common types of Parameters:

Defining and Using Parameters

Parameters are a powerful tool in Azure Pipelines, allowing you to make your pipeline more flexible and reusable. Each Parameter must specify a type, such as boolean, number, object, or string, and many other types that deal with various Pipeline objects.

You can provide a value for your Parameter in multiple ways. You can provide a default value in the YAML by using the 'default' key, or you can run the pipeline and provide a value manually at runtime. If neither of these options are used, the first value listed in the 'values' key will be used.

You can specify the parameter name using one of two syntaxes, both of which produce the same result. This allows for flexibility in how you define your parameters.

Credit: youtube.com, Defining Procedures and Using Parameters

Parameters defined at the pipeline level are known as Runtime Parameters, because you can interactively change their values when you run the pipeline. This is useful when you want to customize the pipeline's behavior without modifying the YAML code.

Here are the common types of parameters you can use:

  • boolean
  • number
  • object
  • string
  • environment
  • filePath
  • pool
  • secureFile
  • serviceConnection
  • container
  • containerList
  • deployment
  • deploymentList
  • job
  • jobList
  • stage
  • stageList
  • step
  • stepList

Return Values

Return Values are essential in defining and using parameters. They allow a function to send data back to the caller.

In the example of the `get_greeting` function, the return value is a string that says "Hello, world!". This is a simple but effective way to send a greeting back to the caller.

The return value can be assigned to a variable, which can then be used in the code. For instance, you can assign the return value of `get_greeting` to a variable called `greeting`.

The return value can also be used in conditional statements, such as if-else statements. In the example of the `get_greeting` function, the return value is used to determine whether to print a morning or afternoon greeting.

Take a look at this: Azure Devops Extension Example

Parameter Best Practices

Credit: youtube.com, What is a parameter in Azure pipeline? | Advanced parameter usage in Azure DevOps | CI/CD Tutorial

When working with Azure DevOps parameters, it's essential to follow best practices to ensure smooth and efficient deployment processes.

Use a consistent naming convention for your parameters to avoid confusion and make it easier to understand their purpose.

For example, using a prefix like "db" for database-related parameters is a good practice.

Avoid using sensitive information like passwords and API keys directly in your parameter files.

Instead, store them securely in Azure Key Vault or another secrets management system.

Required Parameters

You can add a validation step at the beginning of your template to check for the parameters you require. This helps prevent errors and ensures your template runs smoothly.

To show that the template fails if it's missing the required parameter, you can pass parameters to templates. Templates are expanded just before the pipeline runs, replacing values surrounded by ${{ }} with the parameters it receives from the enclosing pipeline.

The parameters section defines what parameters are available in the template and their default values. This is useful for setting up default values that can be used if a parameter is not provided.

You can use predefined variables in parameters, but not variables that are created on the fly. This is because templates are expanded before the pipeline runs, and only predefined variables are available at that time.

Template Expressions

Credit: youtube.com, ARM Templates Parametrization | Expressions, Parameters and Variables

Template Expressions are evaluated once at the very beginning of a Pipeline, at compile time. This means they're not dynamic, so their value won't change throughout the pipeline's execution.

You can use Template Expressions for Parameters, Variables, or Azure DevOps Expressions. They're a powerful tool for customizing your pipeline's behavior.

If you're using a Template Expression for a Parameter or a Variable that doesn't exist, it will evaluate to an empty string. This can be a useful default value in certain situations.

Template Expressions can be used anywhere in a YAML statement, both in the key (left side) or the value (right side). This flexibility makes them a versatile feature to have in your pipeline.

Pipeline Configuration

Pipeline Configuration can be a bit tricky, but one thing to keep in mind is that Azure DevOps allows you to configure pipelines with parameters.

Using pre-defined parameters can help prevent errors that occur when users accidentally enter unsupported values, as Musfirah Fatima pointed out. This is a cleaner approach than relying on variables.

To make the most of pipeline configuration, you can experiment with combining parameters with pipeline templates for reusable test suites, as Musfirah Fatima suggested.

Debug Output Task for Pipeline Code

Credit: youtube.com, How To Debug Pipelines

The Debug Output Task is a game-changer for pipeline code. It allows you to output all variables and parameters in use at the moment the step is being executed, which can be extremely useful for troubleshooting.

You can use the Debug Output Task to confirm that expected parameters are coming through to your step correctly. This is especially helpful when trying to expose variables across jobs and steps.

The Debug Output Task can be added to the bottom of your pipeline and passes the parameters and variables that you want to log out. To do this, you'll need to add the output-debug-info task to your pipeline template, like in the example below.

Passes the variables to be logged out

You can also use the Debug Output Task to pass specific parameters and variables to be logged out, like in the example below. The output-debug-info task has been added to the bottom of the pipeline and it passes the parameters and variables that we want to log out (i.e. those available to this pipeline).

To use the Debug Output Task effectively, you'll need to make sure that the variables and parameters you want to log out are available in the pipeline. You can do this by adding the necessary tasks and variables to your pipeline template.

3 Responses to "Pipeline"

Diverse team engaging in collaborative work in a contemporary office environment.
Credit: pexels.com, Diverse team engaging in collaborative work in a contemporary office environment.

Musfirah Fatima appreciated a deep dive into using parameters in Azure DevOps pipelines, particularly the shift from variables to pre-defined parameters for safer runs.

This approach helps prevent users from entering unsupported values, which can lead to errors.

In fact, Musfirah Fatima mentioned struggling with pipelines where users accidentally enter unsupported values, and this approach seems much cleaner.

Combining parameters with pipeline templates can lead to reusable test suites.

Here's a list of benefits Musfirah Fatima mentioned:

  • Shift from variables to pre-defined parameters for safer runs
  • Prevents users from entering unsupported values
  • Can be combined with pipeline templates for reusable test suites

Task Group Management

Task Group Management is a crucial aspect of Azure DevOps parameter management. You can remove unwanted parameters by editing the JSON file, deleting unnecessary inputs, and importing the modified JSON back into Azure DevOps.

Removing parameters is a straightforward process. Open the exported JSON file with your preferred editor, scroll down to the inputs array, and delete the inputs you wish to remove. This will effectively get rid of the parameters.

However, removal isn't always an option, and concealing parameters might be more suitable. To hide unwanted parameters, you can add a visibility rule to the JSON file. This involves adding the line "visibleRule": "1 = 2" to the parameters you wish to hide.

Credit: youtube.com, Task Groups in Azure Pipelines with Parameters

This rule effectively keeps the parameter invisible by setting a condition that's never true. By applying this rule, you can keep the parameters hidden but operational as internal variables.

Here's a summary of the steps to remove and hide parameters:

  • Remove parameters: Edit the JSON file, delete unnecessary inputs, and import the modified JSON back into Azure DevOps.
  • Hide parameters: Add a visibility rule to the JSON file, using "visibleRule": "1 = 2" to keep the parameter invisible.

By following these steps, you can effectively manage your Task Groups and keep your parameters organized. Remember that this fix is stable until any further modifications are made to the Task Group, so be sure to keep an eye on it.

Expand your knowledge: Azure Devops Powershell Task

Experimenting with Parameters

You can use variables in Azure DevOps as parameters, and they can be defined under variables in the pipeline.

Azure DevOps provides its own variables that can be used at the time of pipeline run. To access them, navigate to the yaml file and click on variables.

You can add a variable you require, such as an environment variable, and select the option "Let users override this value when running this pipeline" to allow users to change its value at runtime.

Credit: youtube.com, Mastering Variables and Parameters in Azure DevOps

If you want to change the default value of a variable, you can click on the variable that needs changing.

The way of using the variable hasn't changed, even if you remove the variables section from the yaml file. You can still use the variable in your pipeline as before.

When you run the test, you can navigate to Variables and change the environment to what you require. The newly defined variables will be visible under Advanced options.

Variables and parameters can sometimes be frustrating to work with, especially when trying to expose them across jobs and steps.

To make it easier, you can use a quick and simple Bash step to output all the variables and parameters that are in use at the time the step is being executed.

This can be useful for confirming that the expected parameters are coming through to your step correctly.

Desiree Feest

Senior Assigning Editor

Desiree Feest is an accomplished Assigning Editor with a passion for uncovering the latest trends and innovations in technology. With a keen eye for detail and a knack for identifying emerging stories, Desiree has successfully curated content across various article categories. Her expertise spans the realm of Azure, where she has covered topics such as Azure Data Studio and Azure Tools and Software.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.