Search code examples
azure-devopsyamlcicdazure-pipelines-yaml

Cannot Access Azure DevOps Variable for resourceName in Environment at Runtime


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.

My Pipeline YAML

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"

What I Did

  1. I added a pipeline variable in the Variables section of the Azure DevOps UI (top right):
    • Name: ResourceName
    • Value: MyResource (the name of the VM resource in the Production environment).
  2. Referenced this variable in the YAML using resourceName: ${{ ResourceName }}.

The Problem

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.

What I Tried

  • Tried using both $(ResourceName) and ${{ ResourceName }} in the resourceName field.
  • Verified that the Production environment exists and contains the VM resource MyResource.
  • Tested with hardcoding the value (resourceName: 'MyResource'), which works fine. enter image description here

My Questions

  1. Why can’t I use a variable for the resourceName field in the environment definition?
  2. Is there a way to dynamically reference a VM resource in an environment using a variable defined in the Azure DevOps Variables section?

Additional Information

  • I am using Azure DevOps YAML pipelines with a multi-stage setup.
  • The VM (MyResource) is correctly registered in the Production environment.

Any help or insights would be appreciated!


Solution

  • Using pipeline variables

    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)
    

    Using pipeline parameters

    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
      # ...