【问题标题】:Passing value from PowerShell script task (variable) into subsequent task within the same agent job将值从 PowerShell 脚本任务(变量)传递到同一代理作业中的后续任务
【发布时间】:2019-10-29 07:29:11
【问题描述】:

我正在关注this 链接以在任务中设置一个变量,以便可以在同一代理作业的另一个任务中使用它。

在 PowerShell 脚本任务中,我使用了以下方法。设置变量的行:

Write-Host "##vso[task.setvariable variable=sauce]crushed tomatoes"
  1. 链接的下一部分显示如何读取变量 - 它有参数和脚本。我必须在哪里输入参数?是否进入另一个PowerShell任务?下一部分建议当变量需要被另一个任务使用时我还需要设置isOutput=true - https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cbatch#using-variables-as-task-inputs

  2. 我对访问上述步骤中设置的变量感到困惑。如果我想在另一个 PowerShell 任务中获取值,或者如果我想将值传递给另一个非 powershell 任务,那么我应该使用 $(sauce)$env:SAUCE 来获取/发送值吗?

    李>

【问题讨论】:

    标签: azure-devops azure-pipelines


    【解决方案1】:

    是的,你可以用另一个 PowerShell 任务读取变量,简单的方法是:

    • 对于内联脚本:

      Write-Host $(sauce)

    • 对于文件脚本:

      Write-Host $env:sauce

    您无需添加isOutput=true

    我的 YAML:

    steps:
    - task: PowerShell@2
      displayName: 'Set var'
      inputs:
        targetType: filePath
        filePath: ./test.ps1
    
    - powershell: |
       Write-Host $(sauce)
     displayName: 'Read inline'
    
    - task: PowerShell@2
      displayName: 'Read from file'
      inputs:
        targetType: filePath
        filePath: ./read.ps1
    

    文件test.ps1是:

    文件read.pd1是:

    构建输出是:

    【讨论】:

    • 但是链接说:“这不会更新环境变量,但它确实使新变量可用于同一作业中的下游步骤。”。那么你是如何使用 $env:sauce 的呢?
    • 如果设置变量的 powershell 在文件中而不是内联脚本中,那么我必须设置 isOutput = true 并为任务命名(比如“生产者”)。然后在后续任务中使用该变量作为 producer.sauce。否则它不起作用。
    • 是的,这是正确的,但您不能将 $(sauce) 用作 azure 任务参数的一部分,除非您使用 isOutput=true。你试过了吗?
    • 是的,我试过了,即使不使用isOutput,我也可以传递参数。
    • 对我来说,$(sauce) 不能作为非 powershell 脚本的一部分工作(例如 - 如果我尝试将它用作参数的一部分)。如果我使用 isOutput=true 然后 $(producer.sauce)
    猜你喜欢
    • 1970-01-01
    • 2022-11-23
    • 2016-08-28
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多