Azure DevOps多阶段管道YAML:如何签出多个存储库? [英] Azure DevOps multistage pipeline YAML: how to checkout multiple repos?
问题描述
我的Azure DevOps管道使用来自两个不同存储库的yaml模板,其配置如下.
My Azure DevOps pipeline uses yaml templates from two different repos, which is configured as following.
- 有一个应用程序存储库,其中包含可部署的应用程序和一个yaml文件-管道的根"模板
- 模板存储库.根模板从模板存储库中调用其他模板和阶段.然后,来自此存储库的模板会调用其他模板和脚本(来自同一存储库)
模板存储库在根模板中被引用为资源.我没有找到一种方法来仅检出模板存储库一次,然后在所有管道阶段使用模板和脚本.现在,我必须在需要使用其他模板或脚本的每个阶段中手动克隆模板存储库.在每个阶段结束时,Azure Devops都会清除克隆的存储库.是否有一种简单的方法可以仅一次检查模板存储库,或者以其他方式从子阶段引用其资源?
Templates repository is referenced as a resource in the root template. I didn't find a way to checkout the templates repo just once and then use templates and scripts across all pipeline stages. For now I have to clone the templates repo manually in each stage where I need to use additional templates or scripts. At the end of each stage Azure Devops clears the cloned repo. Is there a simple way to checkout templates repo just once, or somehow else reference its resources from sub-stages?
推荐答案
我不确定,因为您没有显示YAML文件,但是您是否使用了结帐步骤:
I'm not sure since you didn't show your YAML file, but did you use checkout step:
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
存储库中)的模板.
Above script checout two repos. In devops-templates
I have template which is used in main build yaml file (located in self
repo).
也请看看 编辑
我对此进行了一些尝试,并尝试了一些方法.让我描述一下文件之间的第一个关系:
I work a bit with this and tried few things. Let me describe first relation between files:
- build.yaml(主存储库)
- templates/start.yml(模板存储库-具有阶段的模板)
- 职位一-templates/process.yaml(模板存储库)
- 步骤-templates/another-template.yaml(模板存储库)
- build.yaml (main repo)
- templates/start.yml (template repo - template with stages)
- job one - templates/process.yaml (template repo)
- steps - templates/another-template.yaml (template repo)
您不必实际检出模板存储库,因为在运行时,所有模板都已完成,并且创建了构建计划.如果要运行某些脚本(例如powershell脚本),则仅需要签出模板存储库.这里有我的yaml文件:
And you don't have to actually checkout template repo because at running all templates are fecthed and build plan is created. You only need to checkout template repo if you are going to run some scripts (for instance powershell scripts). Here you have my yaml files:
build.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
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
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
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 repo中的模板,但我不在此作业中签出repo.
Build job uses template from devops-template repo but I don't checkout repo in this job.
您可能想知道为什么我们每次构建都无法进行一次结帐.这是因为每个作业都可以运行不同的代理.
You may wonder why we can't have one checkout per build. And this is because each job can run a different agent.
您在这里有几个链接:
- build log
- repo with main file
- repo with templates
最后一句话,当您通过模板从该仓库中调用文件时,确实需要使用模板签出仓库.例如:
Last remark, you really need to checkout repo with templates when you template calling a file from that repo. For instance:
steps: - task: PowerShell@2 inputs: filePath: /scripts/myscript.ps1
这篇关于Azure DevOps多阶段管道YAML:如何签出多个存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- job one - templates/process.yaml (template repo)
- templates/start.yml (template repo - template with stages)
- 职位一-templates/process.yaml(模板存储库)
- templates/start.yml(模板存储库-具有阶段的模板)