如何在带有变量组的 azure DevOps yaml 管道的变量中使用 IF ELSE? [英] how can I use IF ELSE in variables of azure DevOps yaml pipeline with variable group?

查看:21
本文介绍了如何在带有变量组的 azure DevOps yaml 管道的变量中使用 IF ELSE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 2 个值之一分配给变量组之外的变量,但找不到如何使用 IF ELSE 的参考.

I'm trying to assign one of 2 values to a variable in addition to variable group and can't find the reference that how to use IF ELSE.

基本上我需要将这个 jerkins 逻辑转换为 azure DevOps.

Basically I need to convert this jerkins logic to azure DevOps.

詹金斯

if (branch = 'master') { 
   env = 'a'
} else if (branch = 'dev'){
    env ='b'
}

我从以下引用中找到了 1 个引用,但如果变量部分没有变量组,这个引用似乎有效.

I found 1 reference from the following one, but this one seems to work if the variables section doesn't have variable groups.

https://stackoverflow.com/a/57532526/5862540

但在我的管道中,我已经有一个用于秘密的变量组,所以我必须使用名称/值约定,并且该示例不适用于诸如 expected a mapping不需要映射意外的值'env'

But in my pipeline, I already have a variable group for secrets, so I have to use name/value convention and the example doesn't work with the errors like expected a mapping or A mapping was not expected or Unexpected value 'env'

variables:
- group: my-global
- name: env
  value:
    ${{ if eq(variables['Build.SourceBranchName'], 'master') }}: 
      env: a
    ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}: 
      env: b

variables:
- group: my-global
- name: env
  value:
    ${{ if eq(variables['Build.SourceBranchName'], 'master') }}: a
    ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}: b

推荐答案

我认为现在你需要使用一个任务来自定义 name/value 语法变量和条件变量值.正如您所指出的,name/value 语法的对象结构似乎破坏了表达式的解析.

I think for now you're going to need to use a task to customize with name/value syntax variables and conditional variable values. It looks like the object structure for name/value syntax breaks the parsing of expressions, as you have pointed out.

对我来说,以下是一个相当干净的实现,如果您想将它从管道中抽象出来,似乎一个供您使用的许多管道的简单模板应该满足对中心全局"位置的需求.

For me, the following is a reasonably clean implementation, and if you want to abstract it away from the pipeline, it seems that a simple template for your many pipelines to use should satisfy the desire for a central "global" location.

variables:
  - group: FakeVarGroup
  - name: env
    value: dev

steps:
  - powershell: |
      if ($env:Build_SourceBranchName -eq 'master') {
        Write-Host ##vso[task.setvariable variable=env;isOutput=true]a
        return
      } else {
        Write-Host ##vso[task.setvariable variable=env;isOutput=true]b
      }      
    displayName: "Set Env Value"

这篇关于如何在带有变量组的 azure DevOps yaml 管道的变量中使用 IF ELSE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆