Pass a variable to template in an Azure YAML pipeline
Asked Answered
L

1

0

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.

Lasseter answered 27/4, 2020 at 21:5 Comment(0)
M
1

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

Marcheshvan answered 28/4, 2020 at 4:2 Comment(3)
I get the impression that you can only use $[ dependencies.Job1.outputs['slot.name'] ] in a variable assignment, like I did in my first code example. Because in my second example it is simply treated as a string, not as a variable. Does that make any sense?Lasseter
@JeanlucaScaljeri. Yes. And my fault. I just give part of my YAML script originally. I just updated the complete yaml which contain setup and create_slot jobs.Marcheshvan
Thanks, it makes more sense now :)Lasseter

© 2022 - 2024 — McMap. All rights reserved.