Search code examples
azure-devopstfsyamlazure-pipelinesazure-pipelines-yaml

Can't use Predefined variables under environment tags in YAML azure devops pipelines


Below is my YAML code, where I'm trying to use the yaml predefined variable $(Build.BuildNumber) under the tags section for environments, but it doesn't work at all. It looks more like a limitation in the Environments' tags block which I think needs the value to be present during compile time.

stages:
- stage: Deploy
  displayName: 'Test Deploy'
  jobs:
  - deployment: Test_job
    environment:
      name: myEnvironment
      resourceType: VirtualMachine
      tags: $(Build.BuildNumber) 
    strategy:
      runOnce:
        deploy:
            steps:
            - task: PowerShell@2
              inputs:
                targetType: 'inline'
                script: Deploy-Steps...

The error I get is:

##[error]No resource were found in the environment with ID 17 matching the specified criteria: ResourceId , ResourceName , ResourceType VirtualMachine, Tags $(Build.BuildNumber).


Solution

  • We should use the syntax ${{}} for compile time in the tag of the environment.

    Example:

    tags: ${{variables['Build.DefinitionName']}}

    tags: ${{variables['Build.SourceVersion']}}
    

    However, we cannot use $(Build.BuildId), $(Build.BuildURL), and $(Build.BuildNumber), because their values aren't set at the time expressions are evaluated.