Reputation: 557
I have a master branch and in its pipeline there's a powershell script to update another branch (for automatic sync purposes) at the end of the process:
# 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
There's another pipeline for the stage branch that is normally triggered if I manually push to it. But in this case (when another pipeline pushes) I don't want to trigger, because the changes only involve changing documentation files, and it's not necessary to waste time and resources triggering a new build.
My first approach was to set path filters, to exclude when the file modified is CHANGELOG.md (documentation file)
It works when I push from my computer, but it doesn't work when the push comes from the build agent machine (It's still triggering)
How can I avoid the trigger? Another approches are also welcome.
Thanks in advance
Upvotes: 1
Views: 1604
Reputation: 16188
Git merge apply does not create new commit. You can find that (no commit created; -m option ignored)
in the command line result:
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(-)
You can avoid fast forward and create commit with [skip ci] message using --no-ff
option:
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(-)
Upvotes: 1
Reputation: 16188
Another way (but maybe more complex) is to use Pull Request to create commits with [skip ci] message. Here is the power shell example to run on build agent:
$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
Upvotes: 0
Reputation: 31075
Check documentation of "How do I avoid triggering a CI build when the script pushes?" here:
Upvotes: 0