我不确定因为你没有展示你的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
displayName: succeed
- bash: echo "Test"
displayName: succeed
start.yaml
parameters:
- name: repo
default: ''
- name: buildSteps
type: stepList
default: []
stages:
- stage: secure_buildstage
pool: Hosted VS2017
jobs:
- template: process.yaml
parameters:
pool:
vmImage: ubuntu-latest
- 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
default: ''
steps:
- pwsh: Write-Host 'Hello form another template'
请看这里:
![在此输入图像描述](https://istack.dev59.com/GVS9H.webp)
构建作业使用devops-template存储库中的模板,但我没有在此作业中检出存储库。
您可能会想知道为什么我们不能每次构建都检出一次。这是因为每个作业可以运行不同的代理程序。
![enter image description here](https://istack.dev59.com/D9hHa.webp)
这里有几个链接:
最后一点,当您从该代码库调用文件时,确实需要检查包含模板的代码库。例如:
steps:
- task: PowerShell@2
inputs:
filePath: /scripts/myscript.ps1