azure-pipelinesazure-yaml-pipelines

Pass a variable to template in an Azure YAML pipeline


Consider the following working job from an azure yaml pipeline

  - job: create_slot
    dependsOn: setup
    displayName: 'Create slot'
    pool:
      vmImage: 'windows-latest'
    variables:
      slotName: $[ dependencies.setup.outputs['slot.name'] ]
    steps:
      - bash: |
          echo "Slot to be created: $(slotName)"
        displayName: 'Show slot name'
      - template: templates/create-slot.yml
        parameters:
          slot: $(slotName)

From the documentation I would expect that I can replace the marco $(slotName) directly with the runtime expression $[ dependencies.setup.outputs['slot.name'] ], which results in the following:

   - job: create_slot
    dependsOn: setup
    displayName: 'Create slot'
    pool:
      vmImage: 'windows-latest' 
    steps:
      - bash: |
          echo "Slot to be created: $(slotName)"
        displayName: 'Show slot name'
      - template: templates/create-slot.yml
        parameters:
          slot: $[ dependencies.setup.outputs['slot.name'] ]

But if you do this, the pipeline fails

enter image description here

From the error I get the impression that $[ dependencies.setup.outputs['slot.name'] ] is treaded as a string. Is it possible what I'm trying here, maybe I have incorrect syntax.


Solution

  • There's no syntax issue to call slot.name on your create_slot job, here the issue should cased by the script you used in setup job. Since you did not share the scripts of setup job, I post with mine below along with some key points of it.

    In your setup job, it should contain one script to generated output variable name. Also, the task which hold the variable generation process should configure the reference name slot.

    Simple sample(Updated):

    jobs:
    - job: setup
      steps:
      - checkout: none
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: 'echo "##vso[task.setvariable variable=name;isOutput=true]Staging"'
        name: slot
    - job: create_slot
      dependsOn: setup
      variables:
        slotName: $[ dependencies.setup.outputs['slot.name'] ]
      steps:
      - checkout: none
      - bash: |
              echo "Slot to be created: $(slotName)"
        displayName: 'Show slot name'
    

    Only this, the create_slot job which depends on the setup job can get the variable name by using the syntax $[ dependencies.Job1.outputs['slot.name'] ]:

    enter image description here