且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将变量传递到Azure YAML管道中的模板

更新时间:2023-10-25 17:44:46

在您的 create_slot 作业上调用 slot.name 时没有语法问题,此处应解决此问题通过您在 setup 作业中使用的脚本.由于您没有共享 setup 作业的脚本,因此我在下面与我一起发布了它的一些要点.

在您的 setup 作业中,它应包含一个脚本以生成输出变量 name .,负责变量生成过程的任务应配置引用名称 slot .

简单示例(已更新):

 职位:-工作:设置脚步:-结帐:无-任务:PowerShell @ 2输入:targetType:内联"脚本:"echo"## vso [task.setvariable variable = name; isOutput = true]登台"名称:插槽-工作:create_slot取决于:设置变量:slotName:$ [dependency.setup.outputs ['slot.name']]脚步:-结帐:无-重击:|echo要创建的插槽:$(slotName)"displayName:显示广告位名称" 

只有这样,依赖 setup 作业的 create_slot 作业才能使用语法 $ [encies.Job1.outputs ['slot.name']] :

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

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.

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'] ]: