[英]How to use a pipeline template for multiple pipelines (in multiple projects) in Azure devops
我是使用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.yml
和install-net-core
)。
所有項目都具有相同的構建步驟,因此我想對所有三個項目使用build-project.yml
模板(而不是對每個文件中的每個步驟進行硬編碼)。
我的問題是,由於它們駐留在不同的項目中,因此無法像從project3那樣簡單地訪問模板文件,而只能像這樣處理它:
.
.
.
- template: ../proj0-common/pipeline-templates/build-project.yml
.
.
.
[我相信]原因是每個項目都會有自己的隔離構建池(如果我錯了,請對此進行糾正)。
我在考慮Azure DevOps是否具有與變量組類似的功能,但對於管道模板,這可以解決我的問題,但是,我找不到這種功能。 有人可以為這個問題提出解決方案嗎?
您能復制這個用例嗎? 在檢查了一些文檔后,我做了一些實驗。 但是,它與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..
我發現只有一種解決方案可以真正做到這一點。 您可以使用絕對路徑引用父目錄。 關鍵是使用系統變量填充根路徑。 您的示例的解決方案:
- template: ${{variables['System.DefaultWorkingDirectory']}}/proj0-common/pipeline-templates/build-project.yml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.