【问题标题】:How to set a variable in a DevOps Pipeline PowerShell task and use that variable as a condition for another task如何在 DevOps Pipeline PowerShell 任务中设置变量并将该变量用作另一个任务的条件
【发布时间】:2020-11-16 18:15:55
【问题描述】:

我查看了在 DevOps 管道中为任务设置变量和条件的各种示例,但我似乎无法完全理解我的用例。

我希望 PowerShell 任务将变量设置为 true 或 false,然后将该变量用作另一个任务的条件,该任务将创建工件。

到目前为止,我在网上阅读的内容是:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      #Some logic here
      
      if ($logicEvaluatedToTrue) {
          Write-Host "Artifacts will be created"
          Write-Host "##vso[task.setvariable variable=requiresRelease;isSecret=false;isOutput=true;]$true"
      } 
      else {
          Write-Host "No artifacts will be created"
      }
    failOnStderr: true

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
       #Both of these do not contain any values
       Write-Host $env:requiresRelease
       Write-Host $requiresRelease
    failOnStderr: true

# this just evaluates to Expanded: and(True, eq(Null, 'true'))
- task: PublishPipelineArtifact@1
  condition: and(succeeded(), eq(variables.requiresRelease, 'true'))
  inputs:
    targetPath: 'snip1.zip'
    artifact: 'snip1'
    publishLocation: 'pipeline'

# this just evaluates to Expanded: and(True, eq(Null, 'true')) too
- task: PublishPipelineArtifact@1
  condition: and(succeeded(), eq(variables['requiresRelease'], 'true'))
  inputs:
    targetPath: 'snip2.txt'
    artifact: 'snip2'
    publishLocation: 'pipeline'

我尝试过像这样先设置一个变量:

variables:
  #existing vars first
  requiresRelease: false

但是当我这样做时,条件的评估变为Expanded: and(True, eq('false', 'true')) 。您的任何帮助将不胜感激。

【问题讨论】:

    标签: azure-devops azure-pipelines


    【解决方案1】:

    您需要在分配值时删除isOutput=true:

    Write-Host "##vso[task.setvariable variable=requiresRelease;isSecret=false;]$true"
    

    要打印值,请使用以下语法:

    Write-Host $(requiresRelease)
    

    【讨论】:

    猜你喜欢
    • 2020-12-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多