I am trying to pass a variable in an Azure deployment between two jobs, (deployments).
According to the doc, as I use runOnce
and resourceType
I need to pass the resource-name in the variable:
tag.
The resource name is an environment variable, $(Environment.ResourceName)
stages:
- stage: Stage
jobs:
- deployment: A
...
steps:
- bash: echo "##vso[task.setvariable variable=OutputVar;isOutput=true]$myVar"
env:
myVar: 'Hello World'
name: printvar
- deployment: B
...
variables:
Variable1: $[ dependencies.A.Outputs.['Deploy_$(Environment.ResourceName).printVar.OutputVar'] ]
Variable2: $[ dependencies.A.Outputs.['Deploy_MyResourceName.printVar.OutputVar'] ]
...
But when I run the code above the Variable1
is not set but Variable2
is properly set.
So, the reason Variable1
is not set is because $(Environment.ResourceName)
is not expended.
I have also tried to output the value of $(Environment.ResourceName)
and that value is properly set to MyResourceName
, (in my example above).
How can I pass $(Environment.ResourceName) without hardcoding the value in the script?
From your description, you need to use the nested variable: $[ dependencies.A.Outputs.['Deploy_$(Environment.ResourceName).printVar.OutputVar'] ]
to get the output value in previous job.
I am afraid that the nested variable is not supported in Azure DevOps. So the variable: $(Environment.ResourceName) is not able to expand as expected.
How can I pass $(Environment.ResourceName) without hardcoding the value in the script?
I suggest that you can change to use Parameters to pass the value to the variable.
Then you can use the format: $[ dependencies.A.outputs['${{parameters.name}}.setvarStep.myOutputVar'] ]
For example:
parameters:
- name: MyResourceName
type: string
default: MyResourceName
pool:
vmImage: windows-latest
stages:
- stage: Stage
jobs:
- deployment: A
...
steps:
- bash: echo "##vso[task.setvariable variable=OutputVar;isOutput=true]$myVar"
env:
myVar: 'Hello World'
name: printvar
- deployment: B
...
variables:
Variable1: $[ dependencies.A.Outputs.['Deploy_${{parameters.MyResourceName}}.printVar.OutputVar'] ]
Variable2: $[ dependencies.A.Outputs.['Deploy_MyResourceName.printVar.OutputVar'] ]