我在 azure 上有两个环境。它们之间的区别之一只是来自变量组的环境变量。是否可以为一个管道动态设置组名称,而不是设置两个可以映射自己的组变量的管道? 这是我的构建管道的示例
trigger:
- master
- develop
jobs:
- job: DefineVariableGroups
steps:
- script: |
if [ $(Build.SourceBranch) = 'refs/heads/master' ]; then
echo "##vso[task.setvariable variable=group_name_variable;isOutput=true]beta_group"
elif [ $(Build.SourceBranch) = 'refs/heads/develop' ]; then
echo "##vso[task.setvariable variable=group_name_variable;isOutput=true]alpha_group"
fi
name: 'DefineVariableGroupsTask'
- script: echo $(DefineVariableGroupsTask.group_name_variable)
name: echovar # that works.
- job: Test
dependsOn: DefineVariableGroups
pool:
vmImage: 'Ubuntu-16.04'
variables:
- group: $[ dependencies.DefineVariableGroups.outputs['DefineVariableGroupsTask.group_name_variable'] ]
# that doesn't work. Error here
steps:
- script: echo $(mode)
displayName: 'test'
您可以使用的一种方法或解决方法是使用模板。 我确信最好的选择是动态使用“组变量”的可能性,虽然我相信这是一种选择,但这是不可能的。
trigger:
tags:
include:
- develop
- qa
- production
variables:
- name: defaultVar
value: 'value define yml on project repo'
- group: variables-example-outside
resources:
repositories:
- repository: yml_reference
type: github
ref: refs/heads/yml_reference
name: enamba/azure_devops
endpoint: enamba
jobs:
- ${{ if eq(variables['Build.SourceBranch'], 'refs/tags/develop') }}:
- template: deploy.example.yml@yml_reference
parameters:
GroupVariablesName: variables-example-developer
- ${{ if eq(variables['Build.SourceBranch'], 'refs/tags/qa') }}:
- template: deploy.example.yml@yml_reference
parameters:
GroupVariablesName: variables-example-qa
- ${{ if eq(variables['Build.SourceBranch'], 'refs/tags/production') }}:
- template: deploy.example.yml@yml_reference
parameters:
GroupVariablesName: variables-example-production
模板内部:
parameters:
GroupVariablesName: ''
jobs:
- job: deploy_app
displayName: 'Deploy application'
variables:
- group: ${{ parameters.GroupVariablesName }}
- group: variables-example-inside
steps:
- script: |
echo outsidevar '$(outsidevar)'
echo defaultVar '$(defaultVar)'
echo var by tag '$(variable)'
组名称变量在天蓝色管道中可以是动态的吗?
如有不便,敬请谅解。
恐怕目前不支持此功能。因此,我们必须在 YAML 管道中声明您想要使用的变量组。
其他社区早前也提出了同样的需求,目前该需求已经转达给产品团队,具体可以从工单中查看:
票证:动态变量组?
注意:您可以对此反馈进行投票并添加评论。当有足够多的社区对此反馈进行投票并添加评论时,产品团队成员会认真对待此反馈。
希望这有帮助。
对于那些正在研究它的人(在这里找到https://www.eliostruyf.com/quick-tip-dynamic-variable-groups-azure-devops-yaml-pipelines/):
variables:
- ${{ if eq(variables['build.SourceBranchName'], 'refs/tags/develop') }}:
- group: variables_group_master
- ${{ if ne(variables['build.SourceBranchName'], 'refs/tags/master') }}:
- group: variables_group_dev
请注意语法。
您实际上可以在管道中使用运行时参数来动态引用组名称,而无需将它们硬编码在 yaml 本身中。
一个非常基本的例子
parameters:
- name: env
type: string
default: dev
variables:
- group: my-group-name-${{ parameters.env }}
所以在不传递任何参数的情况下,使用的变量组将是
my-group-name-dev
但也许您将参数传递给 yaml 文件,例如QA
my-group-name-qa
将是使用的变量组。
我遇到过类似的情况,我想分享一下我的结果。
虽然理论上您可以在Pipeline中利用组变量(假设权限设置正确),但我发现这不是Azure DevOps在Pipeline中以某种条件加载一个组到另一个组的预期工作流程.
所以我所做的是:
这样,Pipeline可以通过提交触发并为每个单独的分支运行,然后执行需要在创建工件时触发的Release部分中的组变量的任务。因此总而言之,我通过Release中的不同阶段进行条件过滤,并应用工作流程中该点所需的任何任务。
我希望这可以帮助人们理解 pipeline 和 release 之间的结构和区别,并节省第一次构建 DevOps 构建/发布工作流程的时间 :)
我遇到了类似的问题,这对我有用:
例如,您正在尝试检索名为
dev-01
的组变量
- name: currentStack
value: ${{ format('$({0}-{1})', variables['env'], variables['location']) }}
编译时有一些可用的构建变量。模板中可用的内容已在 Microsoft Docs 中列出。
您可以控制一些变量,例如
Build.SourceBranch
或 Build.DefinitionName
。
定义名称是管道的名称。它可用于存储变量组的名称,并可在模板编译时访问。做到这一点的一个好方法是使用分隔符将构建定义名称的人类可读部分与变量组的名称分开。
例如,对于名为“Update Contoso University #ContUni-w6edf”的管道,以下 yaml 将加载名为
ContUni-w6edf
的变量组。
variables:
- template: 'variables.yml' #some source controlled variables
- group: "${{ split(variables['Build.DefinitionName'], '#')[1] }}"