无法将参数传递给 Azure Devops 模板

Can't pass parameter to an Azure Devops template

正在尝试将动态创建的变量从舞台传输到舞台模板。根本不起作用。

我在做什么

我运行 main.yml 文件:

# main.yml

stages:
  - stage: Stage1
    jobs:
      - job: Job1
        steps:
          - task: powershell@2
            inputs:
              filePath: Tests/LoadData.ps1

          - script: echo $(foo)
            displayName: read foo

          - bash: echo "##vso[task.setvariable variable=bar;isOutput=true]$(foo)"
            name: step1
            displayName: create public variable

  - stage: Stage2
    variables:
      foo2: $[ stageDependencies.Stage1.Job1.outputs['step1.bar'] ]
    jobs:
      - job:
        steps:
          - script: echo $(foo2)
            displayName: read foo2

变量来自 PowerShell:

write-host "Run LoadData"
write-host "##vso[task.setvariable variable=foo]'one;two'"

这项工作很好。显示预期字符串的日志。

然后我将这个添加到 main:

  - template: myTemplate.yml
    parameters:
      foo3: $[ stageDependencies.Stage1.Job1.outputs['step1.bar'] ]

有神庙档案:

# myTemplate.yml
parameters:
  foo3: ""

    stages:
      - stage: Stage3
        dependsOn: [Stage1]
        jobs:
          - job:
            steps:
              - script: echo ${{ parameters.foo3 }}

这次不行了。我收到此错误消息:

Script contents: echo $[stageDependencies.Stage1.Job1.outputs['step1.bar'] ]

==========================开始命令输出================ ===========

/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/5ecfbe2d-20cd-43bf-b6b4-1a4f088d5e17.sh

/home/vsts/work/_temp/5ecfbe2d-20cd-43bf-b6b4-1a4f088d5e17.sh: line 1: stageDependencies.Stage1.Job1.outputs['step1.bar'] : syntax error: invalid arithmetic operator (error token is ".Stage1.Job1.outputs['step1.bar'] ") ##[error]Bash exited with code '1'.

变量似乎没有在main中扩展。

我需要的

我该怎么做才能使这个脚本正常工作? 为什么传递给模板时变量没有展开?

谢谢

我想我在多次尝试后找到了解决方案,而且很简单。

我这样修改main:

stages:
  - stage: Stage1
    jobs:
      - job: Job1
        steps:
          - task: powershell@2
            inputs:
              filePath: Tests/LoadData.ps1

          - script: echo $(foo)
            displayName: read foo

          - bash: echo "##vso[task.setvariable variable=bar;isOutput=true]$(foo)"
            name: step1
            displayName: create public variable

  - template: myTemplate.yml

并像这样更改模板:

stages:
  - stage: Stage3
    variables:
      foo3: $[ stageDependencies.Stage1.Job1.outputs['step1.bar'] ]
    dependsOn: [Stage1]
    jobs:
      - job:
        steps:
          - script: echo $(foo3)

这次成功了!