可以在从模板扩展的azure pipelines yml中使用变量吗?

Mar*_*rko 9 azure-pipelines

我们正在使用扩展功能以安全的方式重用管道中的模板。为了更轻松地定义模板的参数,我想使用变量,但我觉得这是不可能的。

但由于我在官方文档中找不到答案,所以我在这一轮中询问。

我的 yml 文件如下所示:

name: '[$(Date:yyyyMMdd).$(Rev:r)][$(Build.SourceBranchName)]'

# ========================================================================
#                          Pipeline Triggers
# ========================================================================
schedules:
- cron: "0 22 * * *" # time in UTC timezone
  displayName: Daily midnight build for mainline branches
  branches:
    include:
    - develop
    - master
  always: true
- cron: "0 22 * * *" # time in UTC timezone
  displayName: Daily midnight build for release branches
  branches:
    include:
    - release/*
  always: false 

# ========================================================================
#                          Template resource
# ========================================================================
resources:
  repositories:
  - repository: templates # id for reuse in below script code
    type: git
    name: Tools/pipeline-templates
    ref: develop

variables:
  Test: TestGroup

# ========================================================================
#                          Template reference
# ========================================================================
extends:
  template: template.yml@templates # Template reference
  parameters:
    Param1:
      - "-T test"

Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,出现以下错误:

__built-in-schema.yml (Line: xx, Col: yy): 'variables' is already defined
Run Code Online (Sandbox Code Playgroud)

我觉得由于我们的模板也使用变量,因此不能在根 yml 文件中使用。

谢谢

Fel*_*lix 4

您的意思是您想使用变量来帮助您定义模板参数吗?如果是,我们建议您可以使用 ${{variables.VariableName}}

这是我将阶段更改为关键字(extends)的演示脚本:

resources:
  repositories:
    - repository: templates
      type: git
      name: Tech-Talk/template

trigger: none

variables:
  - name: Test
    value: TestGroup
    
pool:
  vmImage: ubuntu-latest


extends:
  template: temp.yaml@templates
  parameters:
    agent_pool_name: ''
    db_resource_path: $(System.DefaultWorkingDirectory)      
    variable_group: ${{variables.Test}}
Run Code Online (Sandbox Code Playgroud)

这是我的 temp.yaml:

parameters:
- name: 'variable_group'    
  type: string    
  default: 'default_variable_group'
- name: agent_pool_name
  default: ""
- name: db_resource_path
  default: ""       

  
stages:
  - stage:
    displayName: ${{parameters.db_resource_path}}
    
    variables:
    - group: ${{parameters.variable_group}}

    jobs:
    - job: READ
      displayName: Reading Parameters
      steps:
      - script: |
          echo variable_group: ${{parameters.variable_group}}
      - powershell: echo "$(ServiceConnection)"
Run Code Online (Sandbox Code Playgroud)

附上新的测试结果:您可以发现变量 TestGroup 已传递到临时 yaml:

在此输入图像描述

  • 我非常确定,如果您在 temp.yaml 中添加全局变量(不是在“stages”下方,而是在“parameters”块正下方),那么它也会失败。 (2认同)
  • Marko,如果我们在 temp.yaml 中添加全局变量而不是在阶段中。这当然会出现错误:“变量”已定义。因为,我们已经在主 yaml 文件中定义了该变量。我们可以在主 yaml 文件中定义全局变量。 (2认同)