【问题标题】:How to assign conditional value to variable in Azure DevOps?如何为 Azure DevOps 中的变量分配条件值?
【发布时间】:2021-05-12 04:55:11
【问题描述】:

我正在尝试将条件值分配给 Azure DevOps 中的环境变量,但它不起作用。 这是我的管道代码:

pr: none
trigger: none

stages:
  - stage: Mail
    variables:
    - group: API_Key
    jobs:
      - job: Sending_Email
        variables:
          ${{ if ne( variables['RecipientEmail'], '' ) }}:
            EmailId: $(RecipientEmail)
          ${{ if eq( variables['RecipientEmail'], '' ) }}:
            EmailId: $(DefaultEmailId)
        steps:
           - script: echo Mail id after condition is - $(EmailId)

在这里,我采用了 2 个环境变量“RecipientEmail”和“DefaultEmailId”,并在此基础上为新变量“EmailId”赋值。但它不起作用。在这两种情况下,它都会获取“DefaultEmailId”值。

例如: DefaultEmailId:abc@example.com 收件人邮箱:xyz@example.com

Ideal Output: Mail id after condition is - xyz@example.com

Current Output: Mail id after condition is - abc@example.com

此外,在此我收到警告:在定义“EmailId”变量条件时出现“重复键”。

【问题讨论】:

  • 您确定您的 RecipientEmail 变量是在 API_Key 变量组中定义的吗?如果在管道运行时未定义,则默认为空字符串。 docs.microsoft.com/en-us/azure/devops/pipelines/process/…
  • 嗨@Bruno 是的,我已经在变量组中定义了变量。我仍然遇到这个问题。而且我没有得到空字符串输出。我在这两种情况下都得到“DefaultEmailId”变量值作为输出。

标签: azure-devops azure-pipelines devops azure-pipelines-yaml azure-devops-pipelines


【解决方案1】:

如果DefaultEmailId: abc@example.comRecipientEmail: xyz@example.com 设置在以下变量组中:API_Key,我们可以看到同样的问题。

经过研究,我们发现有一张关于它的建议票:https://developercommunity.visualstudio.com/t/azure-pipelines-yaml-set-variable-based-on-conditi/690246,你可以投票关注这张票。您还可以创建新的建议票here。产品组会定期审核这些工单,并考虑将其作为路线图。

作为一种解决方法,您可以set variables in scripts

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      if ('$(RecipientEmail)' -eq '') {
        Write-Host "##vso[task.setvariable variable=EmailId]$(DefaultEmailId)"
      } else {
        Write-Host "##vso[task.setvariable variable=EmailId]$(RecipientEmail)"
      }
- script: |
    echo Mail id after condition is -  $(EmailId)

【讨论】:

猜你喜欢
  • 2021-02-10
  • 2021-02-15
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 2021-01-05
  • 2021-12-07
  • 1970-01-01
  • 2019-12-17
相关资源
最近更新 更多