繁体   English   中英

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

[英]Azure DevOps multistage pipeline YAML: how to checkout multiple repos?

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

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

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

我不确定,因为您没有显示 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

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

请看这里

编辑

我对此进行了一些工作,并尝试了一些事情。 让我描述文件之间的第一个关系:

  • build.yaml(主仓库)
    • templates/start.yml(模板仓库 - 带有阶段的模板)
      • 工作一 - templates/process.yaml(模板回购)
        • 步骤 - 模板/另一个模板。yaml(模板回购)
      • 作业二 - 直接在 start.yaml 中定义的步骤

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

build.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

开始.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'

过程.yaml

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

jobs:
- job: build
  pool: ${{ parameters.pool }}
  steps:
  - template: another-template.yaml
    parameters:
      repo: devops-templates

另一个模板.yaml

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

steps:
  - pwsh: Write-Host 'Hello form another template'

请看这里: 在此处输入图像描述

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

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

在此处输入图像描述

这里有几个链接:

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

steps:
  - task: PowerShell@2
    inputs:
      filePath: /scripts/myscript.ps1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM