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

标签 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"

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

__built-in-schema.yml (Line: xx, Col: yy): 'variables' is already defined

我觉得因为我们的模板也在使用变量,所以不能在根 yml 文件中使用。

谢谢

最佳答案

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

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

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}}

这是我的 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)"

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

enter image description here

关于azure-pipelines - 在可能从模板扩展的 azure pipelines yml 中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66509894/

相关文章:

azure-devops - 发布中的 VSTS 程序集版本

sonarqube - 将 Sonar MSBuild Runner 与/p :IsPackaging=True 一起使用

azure - 如何从 Azure DevOps YAML 管道访问 AzureResourceManagerTemplateDeployment (@3) 任务中的 "deploymentOutputs"?

powershell - 如何使用 Azure PowerShell 脚本将包发布到 VSO 中的 Web 应用虚拟应用程序

sql-server - 在 Visual Studio Team Services 中以 Windows 用户身份运行

azure - Azure Function 的 VSTS 持续部署失败 - ERROR_FILE_IN_USE

azure-devops - VS402864 : No artifact type found corresponding to id PipelineArtifact

azure - 如何获取 azure 管道的上次运行 ID?

azure-devops - 我是否必须在 Azure DevOps 管道中明确下载 Artifact ?

ios - 尽管使用工作区,Azure Pipeline 'No such module'