Member-only story

Best Practices for Azure Pipelines YAML with Variables and Conditional Logic

Natheem Yousuf
2 min readApr 8, 2024

--

Photo by Sigmund on Unsplash

Azure Pipelines YAML offers a powerful and flexible way to define your CI/CD workflows. But with great power comes great responsibility (and the potential for messy code). This blog dives into best practices for crafting clean, maintainable, and effective Azure Pipelines YAML files, focusing on variables and conditional logic.

Variable Magic: Keep Your Secrets Safe

  • Leverage Variable Groups: Store sensitive information like passwords and connection strings in variable groups. This keeps your YAML files clean and reduces the risk of accidental exposure. Here’s an example:

YAML

variables:
# Reference a variable group named "Production-Secrets"
connectionString: $(Production-Secrets.connectionString)
  • Embrace Different Scopes: Define variables at the pipeline, stage, or job level depending on their purpose. This allows for granular control and reusability. For example:

YAML

# Pipeline-level variable
pool:
vmImage: ubuntu-latest
# Stage-level variable (used only in the "build" stage)
stages:
- stage: build
variables:
buildConfiguration: 'Release'
# Job-level variable (used only in the "test" job)
jobs:
- job: test
variables:
testEnvironment: 'staging'

--

--

Natheem Yousuf
Natheem Yousuf

Written by Natheem Yousuf

Bridging the gap between ‘It works’ and ‘It scales’

No responses yet