一个管道用于多个存储库,不同项目使用不同的模板

问题描述 投票:0回答:1

环境:带有 Windows 代理的 Azure DevOps Server 2022.0

我们在不同的 Git 存储库中有几个大型 Windows 项目。它们应该由单个 YAML 管道使用特定于项目的模板构建。我为任何管道创建了一个 YAML 存储库。

我希望触发存储库设置一个变量,我可以将其用作模板名称的一部分,并在构建 ID 中使用。不仅如此,我希望能够指定手动运行要使用哪个项目,并以相同的方式使用该信息。

Azure-pipelines.yml:

resources:
  repositories:
    - repository: AAA
      type: git
      name: AAA
      ref: main
      trigger:
        branches:
          include:
            - main

    - repository: BBB
      type: git
      name: BBB
      ref: main
      trigger:
        branches:
          include:
            - main

    - repository: YAML
      type: git
      name: YAML-Automation
      ref: main
      trigger:
        - none

parameters:
- name: projectToBuild
  displayName: "Project to build:"
  type: string
  default: AAA
  values:
  - AAA
  - BBB
  - CCC

variables:
  productName: $(Build.Repository.Name)
  productVersion: 5.0.1

name: $(Build.Repository.Name)_$(productVersion)_$(Date:yyMMdd)$(Rev:rr)

我猜因为来自存储库的触发器是在 YAML 代码编译后发生的,所以当设置“name”时,该信息不可用。但是当我输出变量时,它显示了正确的信息:

当回购BBB触发时,

echo Build.Repository.Name =  $(Build.Repository.Name) 

显示 BBB,但构建具有名称 #YAML-自动化_5.0.1_24070505

签出应该仅适用于 YAML 存储库和触发存储库,因此我再次需要触发存储库的名称,该名称为空。

接下来,我想使用模板 jobStuff-$(productname).yml,但 ProductName 为空,并且不起作用。

最后,对于手动运行,我想使用手动项目选择的值projectToBuild作为productName的输入,猜测只需要一个“if手动”,如$[eq(variables['Build.原因'],'手册')]。

我的一般方法应该有效吗? 我该怎么做才能将触发的名称放入可用变量中?

git azure-devops
1个回答
0
投票

您需要管理变量并确保有关触发存储库的信息在需要时可用。以下是实现这一目标的方法:

  • 确定触发存储库:使用

    Build.Repository.Name
    变量获取触发存储库的名称。当触发管道时,Azure DevOps 应自动设置此变量。

  • 根据触发库设置变量:使用条件根据触发库设置变量。这允许您根据存储库配置不同的模板和构建设置。

  • 手动运行处理:使用管道参数允许手动选择要构建的项目。您可以使用条件逻辑来检查构建是否是手动触发的。

以下是如何构建

azure-pipelines.yml
的示例:

resources:
  repositories:
    - repository: AAA
      type: git
      name: AAA
      ref: main
      trigger:
        branches:
          include:
            - main

    - repository: BBB
      type: git
      name: BBB
      ref: main
      trigger:
        branches:
          include:
            - main

    - repository: YAML
      type: git
      name: YAML-Automation
      ref: main
      trigger: none

parameters:
- name: projectToBuild
  displayName: "Project to build:"
  type: string
  default: ''
  values:
  - AAA
  - BBB
  - CCC

variables:
  productName: ${{ coalesce(parameters.projectToBuild, variables['Build.Repository.Name']) }}
  productVersion: 5.0.1

name: $(productName)_$(productVersion)_$(Date:yyMMdd)$(Rev:rr)

stages:
- stage: Build
  jobs:
    - job: BuildJob
      displayName: Build $(productName)
      steps:
        - checkout: self

        - ${{ if eq(variables['productName'], 'AAA') }}:
          - template: jobStuff-AAA.yml

        - ${{ if eq(variables['productName'], 'BBB') }}:
          - template: jobStuff-BBB.yml

        - ${{ if eq(variables['productName'], 'CCC') }}:
          - template: jobStuff-CCC.yml

        - script: |
            echo "Build.Repository.Name: $(Build.Repository.Name)"
            echo "Product Name: $(productName)"

注:

  • productName
    变量使用
    coalesce
    设置,它首先检查手动
    projectToBuild
    参数。如果为空,则回落至
    Build.Repository.Name
  • 条件步骤使用
    ${{ if }}
    语法来包含基于
    productName
    变量的适当作业模板。
  • 通过默认将
    projectToBuild
    设置为空字符串,它允许管道自动使用存储库名称,除非手动指定。
© www.soinside.com 2019 - 2024. All rights reserved.