我是 Azure DevOps 的新手,我正在尝试为多个项目设置构建管道并在它们之间共享 yml
模板。我将更清楚地展示我想要实现的目标,但首先让我向您展示我们项目的结构:
proj0-common/
|----src/
|----azure-pipelines.yml
|----pipeline-templates/
|----build-project.yml
|----install-net-core
proj1/
|----src/
|----azure-pipelines.yml
proj2/
|----src/
|----azure-pipelines.yml
proj3/
|----src/
|----azure-pipelines.yml
第一个文件夹是我们的 Common
项目,我们要在其中放置常用脚本和包并在项目中使用它们。其余文件夹(proj1-proj3)是.net core 项目,充当微服务项目。如您所见,每个项目都有自己的 azure-pipelines.yml
管道文件,并且每个项目都驻留在 Github 中自己的存储库中。然后是驻留在公共(public)项目中的模板管道文件(build-project.yml
和 install-net-core
)。
所有项目都具有相同的构建步骤,因此我想对所有三个项目使用 build-project.yml
模板(而不是对每个文件中的每个步骤进行硬编码)。
我的问题是,由于它们驻留在不同的项目中,我无法简单地访问模板文件,比如说从project3,通过像这样解决它:
.
.
.
- template: ../proj0-common/pipeline-templates/build-project.yml
.
.
.
并且[我相信]原因是每个项目都有自己独立的构建池(如果我错了,请纠正我)。
我在想 Azure DevOps 是否具有与变量组类似的功能,但对于管道模板,这可以解决我的问题,但是,我找不到这样的功能。有人可以建议解决这个问题吗?
最佳答案
您能复制这个用例吗?在查看了一些 docs 后,我进行了一些实验。 。不过,它也存在一些差距,就像 Microsoft 围绕 Azure DevOps 的大多数其他文档一样。
假设您有 azdevops-settings.yml,它指定服务分支之一中的管道。在下面的示例中,它有两个任务步骤,在另一个存储库中运行外部模板,但在其中一个步骤中,我提供了一个参数,该参数在模板中设置为某些默认值。
注意我必须使用端点标签,否则它会提示。可以在文档中进一步指定的内容。
# In ThisProject
# ./azdevops-settings.yml
resources:
repositories:
- repository: templates
type: bitbucket
name: mygitdomain/otherRepo
endpoint: MyNameOfTheGitServiceConnection
steps:
- template: sometemplate.yml@templates
parameters:
Param1: 'Changed Param1'
- template: sometemplate.yml@templates
在模板中,我首先拥有想要通过管道传输到模板的可用参数。我尝试了在不通过管道传输参数的情况下引用参数,例如构建 id 和其他预定义变量,并且它们工作得很好。
我还尝试使用内联脚本以及脚本路径引用。 “test.ps1”仅打印一个字符串,如下面的输出所示。
# otherRepo/sometemplate.yml
parameters:
Param1: 'hello there'
steps:
- powershell: |
Write-Host "Your parameter is now: $env:Param"
Write-Host "When outputting standard variable build id: $(Build.BuildId)"
Write-Host "When outputting standard variable build id via env: $env:BuildNumber"
Write-Host "The repo name is: $(Build.Repository.Name)"
Write-Host "The build definition name is: $(Build.DefinitionName)"
env:
Param: ${{parameters.Param1}}
BuildNumber: $(Build.BuildId)
- powershell: './test.ps1'
以及单独的 powershell 脚本:
# otherRepo/test.ps1
Write-Host "Running script from powershell specification"
输出:
========================== Starting Command Output ===========================
Your parameter is now: Changed Param1
When outputting standard variable build id: 23
When outputting standard variable build id via env: 23
The repo name is: mygitdomain/thisRepo
The build definition name is: ThisProject
Finishing: PowerShell
========================== Starting Command Output ===========================
Running script from powershell specification
Finishing: PowerShell
..and so on..
关于azure - 如何在 Azure DevOps 中将管道模板用于多个管道(在多个项目中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55813339/