使用变量在Azure DevOps中动态更改变量组名称

huangapple go评论77阅读模式
英文:

Change variable group name dynamically in azure devops using a variable

问题

我试图使用下面的代码在Azure DevOps管道中更改变量组名称。我尝试过不同的表达式${{ variables.env }}$(env)$[eq(variables['env']。似乎都不起作用。

build.yaml:

variables:
- ${{ if eq('$(env)', 'Production') }}:
  - group: Production
- ${{ if eq('$(env)', 'Staging') }}:
  - group: Staging
- ${{ if eq('$(env)', 'Test') }}:
  - group: Test
- ${{ if eq('$(env)', 'Development') }}:
  - group: Development

请您指导我正确的方向。

英文:

I'm trying to change the variable group name in azure devops pipeline using the code below. I have tried different expressions ${{ variables.env}}, $(env), $[eq(variables['env']. None seem to work.

使用变量在Azure DevOps中动态更改变量组名称

build.yaml:

variables:
- ${{ if eq('$(env)', 'Production') }}:
  - group: Production
- ${{ if eq('$(env)', 'Staging') }}:
  - group: Staging
- ${{ if eq('$(env)', 'Test') }}:
  - group: Test
- ${{ if eq('$(env)', 'Development') }}:
  - group: Development

Could you please point me in the right direction.

答案1

得分: 1

正如这个答案中所提到的,您不需要评估变量组的条件。以下是一个完整的工作 YAML,使用 variables 替代 parameters

trigger: none

variables:
- name: env
  value: 'Staging'
- group: ${{ variables.env }}

pool:
  vmImage: ubuntu-latest

jobs:
- job: Build
  steps:
  - script: echo Environment is $(env)

演示运行:

使用变量在Azure DevOps中动态更改变量组名称

英文:

As mentioned in this answer, you don't need the conditions to evaluate the variable group. Below is a complete working YAML that uses variables instead of parameters

trigger: none
 
variables:
- name: env
  value: 'Staging'
- group: ${{ variables.env }}

pool:
  vmImage: ubuntu-latest

jobs:
- job: Build
  steps:
  - script: echo Environment is $(env)

Demo Run:

使用变量在Azure DevOps中动态更改变量组名称

答案2

得分: 0

在我的情况下,我想根据特定管道定义的变量值来确定适当的变量组。目标是避免为每个环境单独创建YAML文件。首先,我尝试了以下语法:

variables:
- ${{ if eq(variables['EnvName'], 'prod') }}:
  - group: prod

但是 if 表达式似乎在编译时进行评估,那时变量的值尚未设置,所以它不起作用。

相反,我使用了其中一个 "构建变量",这些变量在处理 if 表达式时已经设置好。对我来说,DefinitionName 变量返回构建管道的名称,这是一个很好的选择,因为我在我的管道名称中指定了环境:

使用变量在Azure DevOps中动态更改变量组名称

这是我在YAML中使用的内容:

variables:
- ${{ if contains(variables['Build.DefinitionName'], 'testing') }}:
  - group: testing-env-group
- ${{ elseif contains(variables['Build.DefinitionName'], 'staging') }}:
  - group: staging-env-group
- ${{ elseif contains(variables['Build.DefinitionName'], 'production') }}:
  - group: prod-env-group
英文:

In my case I wanted to determine proper variable group based on a value of variable defined for specific pipeline. The goal was to avoid the need to have separate yml file for each environment. First, I tried following syntax:

variables:
- ${{ if eq(variables['EnvName'], 'prod') }}:
  - group: prod

but if expression seems to be evaluated compile time and at that moment values of the variables are not yet set so it didn't work.

Instead I used one of the "build variables" which luckily are already set when if expression is processed. For me DefinitionName variable which returns the name of the build pipeline was a good candidate as I have environment specified in my pipelines' names:

使用变量在Azure DevOps中动态更改变量组名称

This is what I used in the yml:

variables:
- ${{ if contains(variables['Build.DefinitionName'], 'testing') }}:
  - group: testing-env-group
- ${{ elseif contains(variables['Build.DefinitionName'], 'staging') }}:
  - group: staging-env-group
- ${{ elseif contains(variables['Build.DefinitionName'], 'production') }}:
  - group: prod-env-group

huangapple
  • 本文由 发表于 2023年3月7日 01:42:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定