[英]Triggering an Azure Devops pipeline from another pipeline
我在從 Azure DevOps 中的另一個管道觸發管道時遇到問題。 我有一個 CI 管道,我想在 CI 通過主分支時觸發一個部署管道。 這在技術上似乎是可行的,但文檔尚不清楚。
我看到以下內容:
# this is being defined in app-ci pipeline
resources:
pipelines:
- pipeline: securitylib
source: security-lib-ci
trigger:
branches:
- releases/*
- master
但尚不清楚 a) 這是否進入觸發管道(在我的情況下是 CI 管道)或觸發管道(在我的情況下是部署管道)。
還不清楚pipeline
和source
指的是什么,以及我如何找出這些變量? 它們都是管道的名稱嗎? 我嘗試了各種不同的排列,但似乎沒有任何效果。
編輯 2
最后,微軟改進了他們關於 YAML 中管道觸發器的文檔! 這是鏈接。
編輯
在寫下我的答案后,微軟提出了另一種解決方案來解決這個問題,通過經典管道使用構建完成觸發器。 他們的解決方案可以在這里找到。
如果您沒有從觸發管道發布工件,則不會觸發觸發的管道。
此外,使用這些類型的觸發器有很大的限制。 有必要將depends
管道defaultBranch for manual and scheduled builds
工作分支。 否則它不會在source
管道執行結束時啟動。 因此,假設您正在處理feature
分支,並且defaultBranch
設置為feature
。 你提交你的代碼,一切都會按預期運行: source
管道啟動,最后,將觸發depends
管道。 都好! 但是當你合並到master
時,如果你不改變defaultBranch
,則不會在source
管道的末尾觸發depends
管道。 我在答案末尾解釋了如何更改defaultBranch
。
我設法在一個簡約的項目上啟動並運行它。 您可以在此處獲取代碼,並在此處獲取有關 Azure DevOps 的項目。 我將嘗試指導您完成我是如何做到的,並回答您在帖子中提出的問題。
我將觸發管道稱為depends
管道,將觸發管道稱為source
管道。
在source
管道上,除了發布工件外,無需執行任何操作。 如果您不從source
管道發布工件,它將無法工作。 您可以在下面找到我用於虛擬source
管道的代碼。 我希望它為master
分支觸發,最后我想確保發布一個工件。
trigger:
branches:
include: # branch names which will trigger a build
- master
pr: none
steps:
# required to cause pipeline triggering downstream
- task: CopyFiles@2
inputs:
contents: $(System.DefaultWorkingDirectory)/**/*.yml
targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: $(Build.ArtifactStagingDirectory)
artifactName: dummy-$(Build.BuildId)
在depends
管道(代碼如下所示)上,我必須禁用CI
和PR
觸發器,否則當我提交到此 repo 時,此管道將由CI
觸發器觸發,然后在source
管道執行結束時觸發。 這是由我的代碼的前兩行完成的。 然后我希望名為source
的管道(這是下面 YAML 中的source
屬性),在名為Pipelining
的項目中(YAML 中的project
屬性)將在更新master
分支時觸發當前( depends
)管道。
trigger: none
pr: none
resources:
pipelines:
- pipeline: source
project: Pipelining
source: source
trigger:
branches:
include:
- master
steps:
- checkout: none
- script: echo 'triggered depends'
是否有意義? Azure DevOps 上的項目名稱與 YAML depends
管道代碼中的property
匹配很重要。對我來說,它是Pipelining
除了source
屬性,在 YAML 中同樣depends
於管道代碼。
default
分支為了更改defaultBranch
,由於上述問題,您應該編輯管道(在本例中為depends
管道),然后在右上角的三個點上選擇Triggers
。 然后選擇YAML
選項卡,您將進入下圖所示的屏幕,您可以在其中設置工作分支。
上面的yaml管道觸發器應該在觸發管道(部署管道)中定義。
- pipeline: string
這里的字符串是你給這個管道資源的標識符。 它可以是任何字符串。
source: string
這里的字符串是觸發管道的定義名稱(你的CI管道的名稱)。
下面的 yaml 來自文檔管道資源。
resources:
pipelines:
- pipeline: string # identifier for the pipeline resource
project: string # project for the build pipeline; optional input for current project
source: string # source pipeline definition name
branch: string # branch to pick the artifact, optional; defaults to all branches
version: string # pipeline run number to pick artifact, optional; defaults to last successfully completed run
trigger: # optional; triggers are not enabled by default.
branches:
include: [string] # branches to consider the trigger events, optional; defaults to all branches.
exclude: [string] # branches to discard the trigger events, optional; defaults to none.
選項:您還可以從 Ui 頁面設置管道觸發器。 進入觸發的 yaml 管道(部署管道)的編輯頁面,點擊3dots並選擇Triggers
轉到觸發器-> 構建完成並單擊添加-> 選擇您的觸發管道(CI 管道)
更新:
我看到 azure-deploy.yml 中的管道資源定義如下。
resources:
pipelines:
- pipeline: 'Deploy to Development'
source: 'DFE-Digital.dfe-teachers-payment-service'
trigger:
branches:
include:
- "master"
- "release-stuff"
請嘗試將觸發元素的縮進更改為與源元素相同。 檢查以下示例:
resources:
pipelines:
- pipeline: 'Deploy to Development'
source: 'DFE-Digital.dfe-teachers-payment-service'
trigger:
branches:
include:
- "master"
- "release-stuff"
我發現了以下內容:
在源管道中,我不需要創建工件
在依賴管道中,如果我想在對源分支的任何提交之后進行構建,我可以讓它與這個一起工作:
trigger: none
pr: none
resources:
pipelines:
- pipeline: 'depends'
source: 'common-gulp-trigger'
trigger: true
我可能會假設您沒有在 master 分支上工作,對嗎? 我以前有同樣的問題。 但是在我閱讀了MS's doc觸發器的默認分支部分之后。 我明白為什么。 默認情況下,觸發器僅檢查 master 分支的 yaml 文件。 這意味着管道將僅由 master 分支的 yaml 文件中的觸發器定義觸發。
因此,無論您在其他分支(非主分支)的 yaml 文件的觸發部分中添加什么分支,tirgger 都不會處於活動狀態。 您需要更改管道以查看當前分支中的 yaml 文件,而不是 master。 只需按照文檔的說明,更改默認觸發器分支。 你會得到它的工作。
將工作合並到 master 后,您可能需要將默認觸發器分支更改回 master。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.