1
votes

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)

enter image description here

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

3
Not sure it will work (because not all the commits pushed will have it) but you could try to update the merge commit message by adding [skip ci] (See developercommunity.visualstudio.com/comments/503497/view.html )Philippe
@Philippe I already tried it...somehow when the push comes from another pipeline (inside azure devops) it just ignores those rules...Danilo Ruziska
@DaniloRuziska Yes, it will ignore if your merge without conflicts.Shamrai Aleksander

3 Answers

1
votes

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(-)
0
votes

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