Azure DevOps 多级管道 YAML:如何检出多个存储库?

lek*_*kso 0 azure-devops azure-pipelines azure-yaml-pipelines

我的 Azure DevOps 管道使用来自两个不同存储库的 yaml 模板,其配置如下。

  1. 有一个应用程序存储库,其中包含可部署的应用程序和一个 yaml 文件 - 管道的“根”模板
  2. 模板库。根模板从模板存储库调用其他模板和阶段。然后来自这个存储库的模板调用其他模板和脚本(来自同一个存储库)

模板存储库作为根模板中的资源被引用。我没有找到只检出模板存储库一次然后在所有管道阶段使用模板和脚本的方法。现在,我必须在需要使用其他模板或脚本的每个阶段手动克隆模板存储库。在每个阶段结束时,Azure Devops 会清除克隆的存储库。是否有一种简单的方法可以只检出模板 repo 一次,或者以其他方式从子阶段引用其资源?

Krz*_*tof 6

我不确定,因为您没有显示 YAML 文件,但是您是否使用了结帐步骤:

resources:
  repositories:
    - repository: devops
      type: github
      name: kmadof/devops-templates
      endpoint: kmadof

steps:
- checkout: self
- checkout: devops
- script: |
    echo $(Build.SourcesDirectory)
    ls $(Build.SourcesDirectory) *
- template: templates/template.yaml@devops
  parameters:
    repo: devops-templates
Run Code Online (Sandbox Code Playgroud)

上面的脚本检查两个 repos。在devops-templates我有在主构建 yaml 文件(位于selfrepo 中)中使用的模板。

请看这里

编辑

我在这方面做了一些工作,并尝试了一些东西。让我描述文件之间的第一个关系:

  • build.yaml(主存储库)
    • 模板/start.yml(模板存储库 - 带阶段的模板)
      • 工作一 - 模板/process.yaml(模板存储库)
        • 步骤 - 模板/another-template.yaml(模板存储库)
      • 作业二 - 在 start.yaml 中直接定义的步骤

而且您不必实际签出模板存储库,因为在运行时所有模板都已完成并创建了构建计划。如果您要运行一些脚本(例如 powershell 脚本),您只需要签出模板存储库。这是我的 yaml 文件:

构建.yaml

resources:
  repositories:
    - repository: devops
      type: github
      name: kmadof/devops-templates
      endpoint: kmadof

stages:
- template: templates/start.yaml@devops
  parameters:
    repo: devops-templates
    buildSteps:
      - checkout: self
      - checkout: devops
      - bash: echo Test #Passes
        displayName: succeed
      - bash: echo "Test"
        displayName: succeed
Run Code Online (Sandbox Code Playgroud)

开始.yaml

# File: start.yml
parameters:
- name: repo  # defaults for any parameters that aren't specified
  default: ''
- name: buildSteps # the name of the parameter is buildSteps
  type: stepList # data type is StepList
  default: [] # default value of buildSteps
stages:
- stage: secure_buildstage
  pool: Hosted VS2017
  jobs:
  - template: process.yaml
    parameters:
      pool:   # this parameter is called `pool`
        vmImage: ubuntu-latest  # and it's a mapping rather than a string
  - job: secure_buildjob
    steps:
    - script: echo This happens before code 
      displayName: 'Base: Pre-build'
    - script: echo Building
      displayName: 'Base: Build'

    - ${{ each step in parameters.buildSteps }}:
      - ${{ each pair in step }}:
          ${{ pair.key }}: ${{ pair.value }}     

    - script: echo This happens after code
      displayName: 'Base: Signing'
Run Code Online (Sandbox Code Playgroud)

进程.yaml

# File: start.yml
parameters:
- name: repo  # defaults for any parameters that aren't specified
  default: ''
- name: buildSteps # the name of the parameter is buildSteps
  type: stepList # data type is StepList
  default: [] # default value of buildSteps
stages:
- stage: secure_buildstage
  pool: Hosted VS2017
  jobs:
  - template: process.yaml
    parameters:
      pool:   # this parameter is called `pool`
        vmImage: ubuntu-latest  # and it's a mapping rather than a string
  - job: secure_buildjob
    steps:
    - script: echo This happens before code 
      displayName: 'Base: Pre-build'
    - script: echo Building
      displayName: 'Base: Build'

    - ${{ each step in parameters.buildSteps }}:
      - ${{ each pair in step }}:
          ${{ pair.key }}: ${{ pair.value }}     

    - script: echo This happens after code
      displayName: 'Base: Signing'
Run Code Online (Sandbox Code Playgroud)

另一个模板.yaml

parameters:
- name: 'pool'
  type: object
  default: {}

jobs:
- job: build
  pool: ${{ parameters.pool }}
  steps:
  - template: another-template.yaml
    parameters:
      repo: devops-templates
Run Code Online (Sandbox Code Playgroud)

请看这里: 在此处输入图片说明

构建作业使用来自 devops-template repo 的模板,但我不在此作业中检出 repo。

您可能想知道为什么我们不能对每个构建进行一次结帐。这是因为每个作业可以运行不同的代理。

在此处输入图片说明

这里有几个链接:

最后一点,当您从该存储库模板调用文件时,您确实需要使用模板检出存储库。例如:

parameters:
- name: repo  # defaults for any parameters that aren't specified
  default: ''

steps:
  - pwsh: Write-Host 'Hello form another template'
Run Code Online (Sandbox Code Playgroud)