我有一个变量组,我从python脚本使用。像这样:
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
print('variableInVariableGroup: $(variableInVariableGroup)')
我想写我的脚本,这样我就可以遍历变量组而不显式引用单个变量。是否有一种方法可以将整个变量组作为字典或类似的东西提供给脚本?
您不能直接这样做,因为解决方法是通过azure cli获取vars并设置任务变量,然后在python脚本任务中获取它们。
如下所示:
# 'Allow scripts to access the OAuth token' was selected in pipeline. Add the following YAML to any steps requiring access:
# env:
# MY_ACCESS_TOKEN: $(System.AccessToken)
# Variable Group 'vargroup1' was defined in the Variables tab
resources:
repositories:
- repository: self
type: git
ref: refs/heads/testb2
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: ubuntu-20.04
steps:
- checkout: self
persistCredentials: True
- task: PowerShell@2
name: TestRef
displayName: PowerShell Script
inputs:
targetType: inline
script: >-
echo $(System.AccessToken) | az devops login
$a=az pipelines variable-group variable list --org 'https://dev.azure.com/orgname/' --project testpro1 --group-id 3 --only-show-errors --output json
echo "$a"
echo "##vso[task.setvariable variable=allvars;isOutput=true]$a"
- task: PythonScript@0
displayName: Run a Python script
inputs:
scriptSource: inline
script: "b=$(TestRef.allvars)nprint(b)nn "
...