如何在Azure devops中将管道模板用于多个管道(在多个项目中)

时间:2019-04-23 14:18:33

标签: azure azure-devops azure-pipelines

我是刚接触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核心项目,并充当微服务项目。如您所见,每个项目都有自己的azure-pipelines.yml管道文件,并且每个项目都位于Github中自己的存储库中。然后是驻留在公共项目中的模板管道文件(build-project.ymlinstall-net-core)。

所有项目都具有相同的构建步骤,因此我想对所有三个项目使用build-project.yml模板(而不是对每个文件中的每个步骤进行硬编码)。

我的问题是,由于它们驻留在不同的项目中,因此无法像从project3那样简单地访问模板文件,只需按以下方式对其进行处理即可:

.
.
.
- template: ../proj0-common/pipeline-templates/build-project.yml
.
.
.

[我相信]原因是每个项目都有自己的隔离构建池(如果我错了,请对此进行纠正)。

我在考虑Azure DevOps是否具有与变量组类似的功能,但对于管道模板,这可以解决我的问题,但是,我找不到这种功能。有人可以建议解决这个问题的方法吗?

2 个答案:

答案 0 :(得分:0)

您可以复制此用例吗?在检查了一些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..

答案 1 :(得分:0)

我发现只有一种解决方案可以真正做到这一点。您可以使用绝对路径引用父目录。关键是使用系统变量填充根路径。您的示例的解决方案:

- template: ${{variables['System.DefaultWorkingDirectory']}}/proj0-common/pipeline-templates/build-project.yml
相关问题