【问题标题】:Pass Variable Group as Dictionary To Python Script将变量组作为字典传递给 Python 脚本
【发布时间】:2021-09-16 18:25:33
【问题描述】:

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

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

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

【问题讨论】:

    标签: azure-devops azure-pipelines devops


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 2015-06-25
      • 1970-01-01
      • 2013-08-03
      • 2011-12-03
      • 2013-10-24
      • 2016-11-13
      • 1970-01-01
      相关资源
      最近更新 更多