Environment name binding is compile-time only in Azure DevOps YAML
0 reputation · 25 Oct 2021, 08:03 UTC
0 reputation · 25 Oct 2021, 08:03 UTC
A repeatable development environment is needed where each branch maps to a pre-created Azure DevOps Environment with approval gates and per-environment variable groups. The goal is to keep a single pipeline definition that can target different environments without manual edits.
Azure DevOps Environments must exist before a deployment job runs and are referenced by name in the environment keyword. The name is project-global and the deployment history is tied to that environment. Variable scoping is available via environment-scoped variable groups, and approvals can be configured on the environment.
The documented templating behavior for the environment reference is unclear for dynamic mapping. It is known that runtime variables are not accepted for the environment name, while compile-time template expressions are allowed.
Can an environment name be resolved from a runtime variable or queue-time parameter? Is there a supported pattern to map dynamic branch names to pre-created environments without duplicating pipeline definitions? What are the limits on templating environment references for repeatable dev environments?
32775 reputation · 25 Oct 2021, 13:50 UTC
In Azure DevOps YAML the environment keyword must be known when the pipeline is parsed. It cannot be set from a runtime variable or a queue‑time parameter. The only supported syntax is a compile‑time template expression that references a variable or parameter that is defined before the YAML is compiled.
$(envName)) are rejected with a configuration error.$[variables['envName']] or $[parameters.envName] are accepted.Define a compile‑time parameter or variable that maps the current branch to the desired environment name. The mapping can live in a variable group, a parameter file, or directly in the pipeline definition.
parameters:
- name: envName
type: string
default: 'dev'
variables:
# Map branches to environments – can be overridden by a variable group
branchEnvMap: |
main: prod
feature/*: test
steps:
- script: |
echo "Branch: $(Build.SourceBranchName)"
displayName: 'Show branch'
- deployment: Deploy
environment: $[variables['envName']]
strategy:
runOnce:
deploy:
steps:
- script: echo Deploying to $(environment)
In this example the envName parameter is set to the value from the branchEnvMap variable group (or overridden by a pipeline variable). Because the value is known at compile time, Azure DevOps can resolve the environment name before any jobs start.
dev, test, prod).environment keyword using a compile‑time template expression: environment: $[parameters.envName].environment keyword is encountered.Do you currently maintain a variable group that maps branch names to environment names, or would you prefer to embed the mapping directly in the pipeline parameters?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.