将变量组作为字典传递给 Python 脚本

Pass Variable Group as Dictionary To Python Script

我有一个从 python 脚本使用的变量组。像这样:

- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      print('variableInVariableGroup: $(variableInVariableGroup)')

我想编写我的脚本,这样我就可以在不显式引用单个变量的情况下迭代变量组。有没有办法将整个变量组作为字典或类似的东西提供给脚本?

您不能直接这样做,因为解决方法是通过 azure cli 获取变量并设置任务变量,然后在 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)\n\n       "
...