Azure DevOps多阶段流水线YAML:如何检出多个仓库?

3
我的Azure DevOps管道使用来自两个不同存储库的yaml模板,配置如下。
  1. 有一个应用程序存储库,其中包含可部署的应用程序和一个yaml文件 - 管道的“根”模板
  2. 模板存储库。根模板调用来自模板存储库的其他模板和阶段。然后,该存储库中的模板调用其他模板和脚本(来自同一存储库)
根模板中将模板存储库作为资源引用。我没有找到一种方法可以仅检出模板存储库一次,然后在所有管道阶段中使用模板和脚本。目前,我必须在每个需要使用额外模板或脚本的阶段手动克隆模板存储库。在每个阶段结束时,Azure DevOps会清除克隆的存储库。是否有一种简单的方法可以仅检出模板存储库一次,或以其他方式从子阶段引用其资源?
1个回答

14

我不确定因为你没有展示你的YAML文件,但是你是否使用了checkout步骤:

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

上面的脚本检查了两个仓库。在devops-templates中,我有一个模板,它在主构建yaml文件中使用(位于self仓库中)。
请也看一下这里编辑 我稍微研究了一下并尝试了一些东西。让我先描述一下文件之间的关系:
- build.yaml(主要仓库)
- templates/start.yml(模板仓库 - 带有阶段的模板)
- job one - templates/process.yaml(模板仓库)
- steps - templates/another-template.yaml(模板仓库)
- job two - 在start.yaml中直接定义步骤
而且你不需要实际检出模板仓库,因为在运行时会获取所有模板并创建构建计划。只有当你打算运行某些脚本(例如powershell脚本)时,才需要检出模板仓库。这里是我的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

start.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'

process.yaml

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

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

another-template.yaml

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

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

请看这里: 在此输入图像描述 构建作业使用devops-template存储库中的模板,但我没有在此作业中检出存储库。
您可能会想知道为什么我们不能每次构建都检出一次。这是因为每个作业可以运行不同的代理程序。

enter image description here

这里有几个链接:

最后一点,当您从该代码库调用文件时,确实需要检查包含模板的代码库。例如:

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

嗨Krzysztof,谢谢你的回复。用这种方法我遇到了一个错误 在解析管道YAML时遇到错误:-----意外的值'stages'。我的YAML大致如下: `parameters: param1: ''stages:
  • stage: stage1`
- undefined
你可能复制粘贴了这个,并且把步骤和阶段混淆了。请检查我的修改后的答案。 - undefined
1
谢谢,我忽略了将步骤传递到模板的可能性。现在它可以正常工作了。 - undefined
1
很酷。听到这个我很高兴。如果我的回答对你有帮助的话,你可以考虑给我点赞吗? - undefined
嘿 @KrzysztofMadej,我有类似的问题,这里的终点是什么?应该在哪个存储库中创建?是自己还是我尝试检出的另一个存储库? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接