I am trying to write an Azure DevOps YAML pipeline where I set the deployment environment dynamical in a predecessor job. Here is a conceptual example of what I am trying to do:
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: SetVariable
jobs:
- job: SetVariableJob
steps:
- script: |
echo "##vso[task.setvariable variable=environmentName;isOutput=true]TestEnvironmentName"
name: setvarStep
- stage: Deploy
dependsOn: SetVariable
variables:
environmentName: $[ dependencies.SetVariable.outputs['SetVariableJob.setvarStep.environmentName'] ]
jobs:
- deployment: DeployWeb
environment: $(environmentName)
strategy:
runOnce:
deploy:
steps:
- script: echo Hello, $(environmentName)!
Something is wrong, as the echo Hello, $(environmentName)!
just outputs: Hello, !
2
Answers
You can’t use
environment: $(environmentName)
, as Macro syntax variables$(var)
is runtime variable.environment
only accept compile time variable or parameter.Compile time variable(template expression variables (${{ variables.var }}) ):
Parameter:
In addition, if you want to use cross stage variable, you could use
The correct format should be
azure-pipelines.yml
There are three things to pay attention to