首页 > 解决方案 > Azure DevOps 条件无法评估 Powershell 设置变量

问题描述

我有一个 powershell 脚本,它不在工作、任务或阶段,它是独立的。像这样在我的 DevOps 构建 yaml 中运行:

- powershell: |
   if (//something that's irrelevant) {
    Write-Host "##vso[task.setvariable variable=myCustomVar;isOutput=true]true"
   } else {
    Write-Host "##vso[task.setvariable variable=myCustomVar;isOutput=true]false"
   }

在这个 powershell 脚本之后,我有另一个脚本来回显 myCustomVar 以查看值是什么。像这样:

- script: |
    echo "What is my custom variable?"
    echo $(myCustomVar)

当构建运行时,在 devops 日志中,它会逐字地回显“$(myCustomVar)”,而不是 True/False

myCustomVar之后,我有一个发送电子邮件的任务,但如果是真的,我们只想发送一封电子邮件。所以我使用条件。

- task: SendEmail@1
  displayName: "Send email"
  condition: and(succeeded(), eq(variables.myCustomVar, true))

然而,这是打破。我已经尝试了其他几种方法。myCustomVar,在任务条件下,始终返回 NULL。语法上有什么帮助吗?

在此处输入图像描述

标签: azurepowershellazure-devops

解决方案


您可以使用##vso[task.setvariable]value从管道中的脚本设置变量。第一个任务可以设置一个变量,后面的任务可以使用这个变量。该变量作为环境变量公开给以下任务。

检查以下有用的链接:


推荐阅读