Azure Pipeline:用户定义的变量在脚本中设置在稍后使用模板中使用时未扩展

发布于 2025-01-22 08:07:17 字数 879 浏览 0 评论 0 原文

我正在尝试将一个由PS1脚本设置的变量传递给Azure Pipeline yaml中的模板yaml文件。但是,无论我尝试什么,当变量达到模板时都不会扩展。

parameters:
- name: myparam
  type: boolean
  default: 'true'

stages:
- stage: stage1
  variables:
    override: 'true'
  jobs:
    - job: FilterJob
    - task: PowerShell@2
      name: ps1task
      inputs:
        targetType: inline
        script: |
          $override = "some value"
          Write-Host "##vso[task.setvariable variable=override;isOutput=true]$override"

- ${{ if eq(parameters.myparam, true) }}:
  - template: Mytemplate.yml
    parameters:
      varPassedToTemplate: $(variables.override) ### VARIABLE DOES NOT EXPAND

变量“ varpassedtotemplate”始终以“:”之后的任何内容的形式最终形成。 我相信 $ {{如果eq(parameters.myparam,true)}}}:是编译时间,并且在运行时没有扩展,但这是否适用于传递给模板的参数?有没有办法扩展变量“ varpassedtotemplate”,还是我的语法只是错误的? (顺便说一句,我尝试过多种语法)

这几天一直使我发疯,因此任何帮助。

I am trying to pass a variable, set by a ps1 script, as a parameter to a template yaml file in an azure pipeline yaml. However no matter what I try the variable is never expanded when it reaches the template.

parameters:
- name: myparam
  type: boolean
  default: 'true'

stages:
- stage: stage1
  variables:
    override: 'true'
  jobs:
    - job: FilterJob
    - task: PowerShell@2
      name: ps1task
      inputs:
        targetType: inline
        script: |
          $override = "some value"
          Write-Host "##vso[task.setvariable variable=override;isOutput=true]$override"

- ${{ if eq(parameters.myparam, true) }}:
  - template: Mytemplate.yml
    parameters:
      varPassedToTemplate: $(variables.override) ### VARIABLE DOES NOT EXPAND

The variable 'varPassedToTemplate' always ends up as a litteral of whatever is after the ':'
I believe ${{ if eq(parameters.myparam, true) }}: is compile time and isn't expanded during runtime but does that also apply to params passed to a template? Is there a way to expand the variable 'varPassedToTemplate' or is my syntax just wrong? (BTW I have tried multiple syntaxes)

This has been driving me crazy for days so any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2025-01-29 08:07:17

根据您的YAML样本,您正在定义模板中的阶段或作业。

您在阶段1中定义的变量不能直接传递到下一个阶段或作业。

要解决此问题,我建议您可以在模板中定义作业并使用阶段输出变量接受阶段1中的变量。

请参阅我的示例:

模板:

parameters:
- name: varPassedToTemplate 
  type: string 
  default: test

jobs:
  - job: FilterJob
    steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              echo ${{ parameters.varPassedToTemplate }}

主YAML:

parameters:
- name: myparam
  type: boolean
  default: 'true'

stages:
- stage: stage1
  variables:
    override: 'true'
  jobs:
    - job: FilterJob
      steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              $override = "some value"
              Write-Host "##vso[task.setvariable variable=override;isOutput=true]$override"
          name: test

- ${{ if eq(parameters.myparam, true) }}:
   - stage: stage2
     variables:
       override: $[ stageDependencies.stage1.FilterJob.outputs['test.override'] ] 
     jobs:
       - template: template.yml
         parameters:
          varPassedToTemplate: $(override)

结果:

Based on your YAML sample, you are defining the Stages or Jobs in the Template.

The variables you define in stage1 cannot be passed directly to the next stage or job.

To solve this issue, I suggest that you can define the jobs in the template and use Stage output variable to accept the variable in stage1.

Refer to my sample:

Template:

parameters:
- name: varPassedToTemplate 
  type: string 
  default: test

jobs:
  - job: FilterJob
    steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              echo ${{ parameters.varPassedToTemplate }}

main YAML:

parameters:
- name: myparam
  type: boolean
  default: 'true'

stages:
- stage: stage1
  variables:
    override: 'true'
  jobs:
    - job: FilterJob
      steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              $override = "some value"
              Write-Host "##vso[task.setvariable variable=override;isOutput=true]$override"
          name: test

- ${{ if eq(parameters.myparam, true) }}:
   - stage: stage2
     variables:
       override: $[ stageDependencies.stage1.FilterJob.outputs['test.override'] ] 
     jobs:
       - template: template.yml
         parameters:
          varPassedToTemplate: $(override)

Result:

enter image description here

剧终人散尽 2025-01-29 08:07:17

Another option is to use

$(ps1task.override)

因此,更改下面的stage2应该使其正常工作!

- ${{ if eq(parameters.myparam, true) }}:
  - template: Mytemplate.yml
    parameters:
      varPassedToTemplate: $(ps1task.override)

Another option is to use output variables within the same job. The name you used for your powershell task is ps1task, ad the name of the variable you defined within this task is override. You could expand your user-defined variable - override - within the same job as ps1task the following way:

$(ps1task.override)

So, changing the stage2 for the below should make it work!

- ${{ if eq(parameters.myparam, true) }}:
  - template: Mytemplate.yml
    parameters:
      varPassedToTemplate: $(ps1task.override)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文