当从另一个管道使用“ git push”时,避免触发Azure管道

时间:2020-05-25 17:39:00

标签: git azure-devops azure-pipelines

我有一个master分支,并且在其管道中有一个powershell脚本可以在过程结束时更新另一个分支(出于自动同步目的):

# User and email must be set, otherwise an error occurs
Write-Host "1: Set git configs"
git config --global user.email "${env:BUILD_REQUESTEDFOREMAIL}" 
git config --global user.name "${env:BUILD_REQUESTEDFOR}"

git checkout stage
git merge master
git push

如果我手动推送到阶段分支,通常会触发该分支分支的另一个管道。但是在这种情况下(当另一个管道推送时),我不想触发,因为更改仅涉及更改文档文件,并且不必浪费时间和资源来触发新的构建。

我的第一个方法是设置路径过滤器,以排除修改后的文件为CHANGELOG.md(文档文件)

enter image description here

当我从计算机上推送时它可以工作,但是当推送来自构建代理机器时它仍然不起作用(仍在触发)

如何避免触发?也欢迎其他方式。

预先感谢

3 个答案:

答案 0 :(得分:1)

Git合并应用不会创建新的提交。您可以在命令行结果中找到(no commit created; -m option ignored)

git merge -m "[skip ci] Merge from build agent" branch
Updating ed7d8f5..11d4c44
Fast-forward (no commit created; -m option ignored)
 README.md | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

您可以避免快速前进,并使用--no-ff选项通过[skip ci]消息创建提交:

git merge --no-ff -m "[skip ci] Merge from build agent" branch
Merge made by the 'recursive' strategy.
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

答案 1 :(得分:0)

答案 2 :(得分:0)

另一种方法(但可能更复杂)是使用Pull Request创建具有[skip ci]消息的提交。这是在构建代理上运行的Power Shell示例:

$user = ""
$token = "$(System.AccessToken)"
$branchTarget = "refs/heads/stage"
$branchSource = "refs/heads/master"
$teamProject = "$(System.TeamProject)"
$repoName = "$(Build.Repository.Name)"
$orgUrl = "$(System.CollectionUri)"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uriCreatePR = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/pullrequests?api-version=5.1"
$uriUpdatePR = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/pullrequests/{pullRequestId}?api-version=5.1"

$bodyCreatePR = "{sourceRefName:'$branchSource',targetRefName:'$branchTarget',title:'Sync changes from $branchSource [skip ci]'}"
$bodyUpdatePR = "{status:'completed',lastMergeSourceCommit:{commitId:'{commitId}',url:'{url}'}}"

$resultNewPR = Invoke-RestMethod -Uri $uriCreatePR -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyCreatePR

Write-Host "Created PR" $resultNewPR.pullRequestId

$uriUpdatePR = $uriUpdatePR -replace "{pullRequestId}", $resultNewPR.pullRequestId
$bodyUpdatePR = $bodyUpdatePR -replace "{commitId}", $resultNewPR.lastMergeSourceCommit.commitId
$bodyUpdatePR = $bodyUpdatePR -replace "{url}", $resultNewPR.lastMergeSourceCommit.url

$resultUpdatedPR = Invoke-RestMethod -Uri $uriUpdatePR -Method Patch -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyUpdatePR

Write-Host "Completed PR" $resultUpdatedPR.pullRequestId