I am setting up a deployment pipeline in Azure DevOps that targets a specific virtual machine (VM) resource in an environment. I want to dynamically pass the VM resource name using a variable defined in the Azure DevOps Variables section (the UI at the top right of the pipeline editor). However, I am unable to access this variable at runtime when using it in the resourceName
field of the environment.
Here is the relevant section of my pipeline:
jobs:
- deployment: DeployToVM
displayName: 'Deploy to VM'
environment:
name: 'Production' # Static environment name
resourceName: ${{ ResourceName }} # Variable for the VM resource name
resourceType: VirtualMachine
strategy:
runOnce:
deploy:
steps:
- script: echo "Deploying to $(ResourceName)"
displayName: "Deployment Script"
ResourceName
MyResource
(the name of the VM resource in the Production
environment).resourceName: ${{ ResourceName }}
.When I run the pipeline, I get the following error:
Resource '' does not exist in environment 'Production'.
It seems like the resourceName
field cannot resolve the value of the variable ResourceName
. However, I can successfully echo the value of $(ResourceName)
in the steps section, so the variable itself is accessible in other contexts.
$(ResourceName)
and ${{ ResourceName }}
in the resourceName
field.Production
environment exists and contains the VM resource MyResource
.resourceName: 'MyResource'
), which works fine.
resourceName
field in the environment
definition?MyResource
) is correctly registered in the Production
environment.Any help or insights would be appreciated!
You're using the wrong syntax to reference variables.
Instead of:
${{ ResourceName }}
Use template expressions (compile time):
${{ variables.ResourceName }}
Or use macro syntax (runtime):
$(ResourceName)
If you need user input when queuing a new pipeline I'd recommend using a pipeline parameter instead of a variable.
Parameters are a better choice because they are strongly-typed, will be compiled and are immediately visible when you queue a new build. The only good reason IMO to use UI variables is if you need to set secret variables.
As an alternative to variables, use a pipeline parameter:
# my-pipeline.yaml
parameters:
- name: ResourceName
type: String
displayName: Name of the virtual machine
jobs:
- deployment: DeployToVM
displayName: 'Deploy to VM'
environment:
name: 'Production' # Static environment name
resourceName: ${{ parameters.ResourceName }} # <------ use pipeline parameter
resourceType: VirtualMachine
# ...