1
votes

I have a YAML file which forms an Azure DevOps pipeline. The pipeline itself defines four variables which are needed in the variables section of the YAML...

variables:
  environmentIdentifier: "$(environmentIdentifier)"
  keyVaultSourceName: "$(keyVaultSourceName)"
  location: "$(location)"
  locationIdentifier: "$(locationIdentifier)"

The variables are definitely set for each run of the pipeline, but when it runs I encounter errors further down in my script which indicate that these variables were not populated correctly...

ERROR: (InvalidResourceGroup) The provided resource group name 'rg-main-$(locationIdentifier)' has these invalid characters: '$:'. The name can only be a letter, digit, '-', '.', '(', ')' or '_'.

I've also tried...

  • $env:location
  • ${{variables['location']}}

...but incurred the same error.

How should I correctly declare vars in the variables section of the pipeline definition, where their values are retrieved from the pipeline's variables?

1

1 Answers

0
votes

You need to define as:

variables:
- name: location
  value: 'Australia Southeast' 

If you want them at a later stage as a template expression use:

${{ variables.location }}

and if you want to use them inside a script:

steps: 
- bash: echo $(location)
- powershell: echo $(location)
- script: echo $(location)

Check this Link and the below extracted sample for more information.

variables:
- name: one
  value: initialValue 

steps:
  - script: |
      echo ${{ variables.one }} # outputs initialValue
      echo $(one)
    displayName: First variable pass
  - bash: echo '##vso[task.setvariable variable=one]secondValue'
    displayName: Set new variable value
  - script: |
      echo ${{ variables.one }} # outputs initialValue
      echo $(one) # outputs secondValue
    displayName: Second variable pass