1

The dynamic variables with the checkout step do not work with the template.

The same works without a template.

#- checkout: git://MY_PROJ/MY_REPO@refs/tags/${{parameters.tag_name}} ## DOES NOT WORK

Getting below error while running pipeline: "ERROR: An item with the same key has already been added"

Working: my_pipeline.yml without template works

parameters:
- name: RELEASE_TAG
  displayName: Enter The Master Release Tag Name Example 1.0.0-RELEASE
  default: 1.0.0-RELEASE
  type: string
      
trigger:
- none

variables:
  db_resource_path: '$(System.DefaultWorkingDirectory)/resources/db'
  pipeline_environment_name: 'PROD_ENV'
  db_env_name: 'prod'

stages:
  - stage: "PROD_DB_DEPLOYMENT"
    displayName: "PROD DB Deployment"
    pool:
      name: $(param.agent.pool.name)
    variables:
    - group: PROD_VG
    jobs:
      - job:
        steps:
          - script: |
              echo param release tag: ${{parameters.RELEASE_TAG}}
          
      - deployment: Deploy
        environment: PROD_ENV
        strategy:
          runOnce:
            deploy:
              steps:
              - checkout: git://MY_PROJ/MY_REPO@refs/tags/${{parameters.RELEASE_TAG}} ## WORKS
                #- checkout: git://MY_PROJ/MY_REPO@refs/tags/${{variables.tag_name}} ## WORKS WITH VAR ALSO

Not Working: Gives ERROR: An item with the same key has already been added. my azure-pipelines.yml is:

parameters:
- name: RELEASE_TAG
  displayName: Enter The Master Release Tag Name Example 1.0.0-RELEASE
  default: 1.0.0-RELEASE
  type: string

resources:
  repositories:
    - repository: templates
      type: git
      name: MY_PROJECT/MY_TEMPLATE_REPO
      
trigger:
- none

variables:
  tagName: '${{ parameters.RELEASE_TAG }}'

stages:
  - stage: "PROD_DB_DEPLOYMENT"
    displayName: "PROD DB Deployment"
    variables:
    - group: PROD_VG
    jobs:
      - template: my_template.yml@templates
        parameters:
          tag_name: $(tagName)
          db_env_name: 'prod' 
          agent_pool_name: $(param.agent.pool.name)
          db_resource_path: $(System.DefaultWorkingDirectory)/resources/db
          pipeline_environment_name: PROD_ENV
          is_release: 'true'

My Template is:

parameters:
- name: 'agent_pool_name'    
  type: string    

- name: 'db_resource_path'
  type: string
  
- name: 'pipeline_environment_name'
  type: string
  
- name: 'db_env_name'
  type: string

- name: 'is_release'
  type: string
  default: 'false'

- name: 'tag_name'
  type: string
  default: '1.0.0-RELEASE'

jobs:
  - job:
    pool:
      name: ${{parameters.agent_pool_name}}
    steps:
      - script: |
          echo param tag_name: ${{parameters.tag_name}}
          echo var tag_name: $(tagName)

  - deployment: Deploy
    pool:
      name: ${{parameters.agent_pool_name}}
    environment: ${{parameters.pipeline_environment_name}}
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self
          - ${{ if eq(parameters.is_release, true) }}:
            #- checkout: git://MY_PROJ/MY_REPO@refs/tags/1.0.0-RELEASE ## WORKS
            #- checkout: git://MY_PROJ/MY_REPO@refs/tags/$(tag_name) ## DOES NOT WORK
            #- checkout: git://MY_PROJ/MY_REPO@refs/tags/${{parameters.tag_name}} ## DOES NOT WORK

Tried below variable option also but get error:Unexpected value 'variables'

enter image description here

Any suggestion, please.

Sanjesh M
  • 341
  • 2
  • 6
  • 22

1 Answers1

2

This is due to that you passed $(var) as parameters of the template.

You can pass ${{variables.tagName}} instead. checked on my side screenshot

In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. Macro syntax variables ($(var)) get processed during runtime before a task runs.

Because templates are expanded before the pipeline execution gets planned, so you cannot pass $(var) as parameter to the template.

Please check official doc for the variable syntax. https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#understand-variable-syntax

Edit: for error "Unexpected value 'variables'": Move variables to stage scope: my yaml here

My template here

wade zhou - MSFT
  • 1,397
  • 1
  • 3
  • 6
  • i already tried this option of assigning parameter to variable but it gives error: Unexpected value 'variables'. Please try and validate – Sanjesh M Aug 13 '21 at 04:33
  • I tried your sample yaml, the "variables" works fine on my side. It appears you have other jobs in your real yaml, did you. please try to put "variables" to your stage scope. it works as well on my side, i update in the reply. can refer to similar link for the details: https://stackoverflow.com/a/63387238/14996050 – wade zhou - MSFT Aug 13 '21 at 05:59
  • Greetings @sanjesh! How is it going? does it work for you? – wade zhou - MSFT Aug 17 '21 at 01:27