Writing Azure DevOps Pipelines YAML, have you thought about including some conditional expressions? In this blog post, I am going to show how you can use If, elseif or else expressions to assist in your pipeline creation
Probably the most common expression you may be using is determining if a stage or job can run. There is lots of expressions available in Azure DevOps to assist you.
Consider the scenario where you have multiple environments, such as dev, test, and prod, and you want to deploy to a specific environment based on the selected option. You can use the following code snippet to achieve this:
In this example, the pipeline is using the parameters.environment parameter to determine which environment variable should be used. If the dev option is selected, the dev Environment variable is used. If the test option is selected, the test Environment variable is used, and so on.
if, elseif or else expressions to determine which task to run
In conclusion, the if, elseif, and else expressions are powerful tools in Azure DevOps pipelines. They can be used to add conditional logic to your pipeline and make it more dynamic and flexible. With these expressions, you can determine which variable, task, or stage should be used based on certain conditions. This can be particularly useful when deploying to different environments or when you only want to run certain tasks based on specific conditions. By using these expressions, you can make your pipeline more efficient, scalable, and easier to maintain.
Mohammad Al Rousan is a Microsoft MVP (Azure), Microsoft Certified Solution Expert (MCSE) in Cloud Platform & Azure DevOps & Infrastructure, An active community blogger and speaker.
Al Rousan has over 8 years of professional experience in IT Infrastructure and very passionate about Microsoft technologies and products.
Top 10 Microsoft Azure Blogs