1
votes

Updated 26-Feb-2020

In our Project, I have a pipeline "MyPipeline" which restores the NuGet packages, builds the solutions and runs the tests.

On the master branch, I have a policy which does things like add code reviewers, and it has a "build validation" step which executes "MyPipeline". All well and good.

However, I created another branch from Master called NewBranch and synced (pushed) this up to Azure. After doing some minor changes in Visual Studio, I did a merge from master, committed and sync'd to Azure.

I was a bit surprised then to see "MyPipeline" executing. It seems to have been triggered when I pushed my changes to NewBranch to Azure. I don't have a branch policy on "NewBranch". The trigger in the YAML file is:

trigger:
- master

What kicked this off? I'll soon burn through my free agent minutes if this continues...

Update 26-Feb-2020

As per the comments below:

The history of commits on the master branch are:

  1. Tue 9:10 PM
  2. Tue 7:47 PM
  3. Mon 2:46 PM

The history of the pipeline execution show:

  1. Wed 9:14 AM "PR Automated"

So, nothing new has been committed to the Master branch. However, I think I know what's causing this. Just that I'm not convinced on the timing....

So, we have two branches, master and NewBranch.

Master has a policy that requires two code reviewers to Approve, and it requires the build to succeed. Because of this policy, a developer can't therefore merge directly into master - they have to generate a Pull Request.

So, Developer A creates a Pull Request to merge NewBranch to Master. There is then the rather lengthy code review process which may take multiple additional commits to the "NewBranch"'s Pull Request before it's deemed acceptable.

What seems to be happening is that everytime one of these new Commits is sync'd to the Pull Request, a build is triggered. Is this a good thing? Maybe, Maybe not. If the build is going to trigger just once, then the compilation should occur when all the code in the Pull Request has been approved, not before hand. Why trigger a build at such an early stage; the master branch may be updated by multiple other Pull Requests before this is ready to be merged in. However, with unlimited resources, then I guess there's no harm in building as often as possible, but a) this can delay other builds (representing an impediment to other developers) and b) this uses up the free limit on agent time.

3
did you try to reproduce it? and did you check master history?Yegor Androsov
It could not be reproduced on my side. BUT, this is wired. What did you see from the build history? Does it indicate it is the individual CI from master? Also, agree with what mentioned in above comment, what about your master history.Merlin Liang - MSFT
@MerlinLiang-MSFT - I updated the original post with additional details. I think I can see why it's happening, just not sure i 100% agree with it. Be interested to hear your take on this. ThanksDrGriff

3 Answers

1
votes

The detailed info you share in question is much help for me to understand the whole workflow of yours.

Though you did not express too clear, but, yeah, what you are guessing is correct. The action you faced is expected and by designed. The root cause is you are using branch policy and the Build validation also included into this policy.

The nutshell is you are using Pull request trigger.


Explanation:

Let's pay attention to its definition:

Pull request (PR) triggers cause a build to run whenever a pull request is opened with one of the specified target branches, or when changes are pushed to such a pull request.

Based on your added contents, your developers are pushing changes (new commit) into the opening Pull request (Note: The key point is Pull request is still be opening.) This belongs to the work scope of the PR trigger because of above definition. That is why the build triggered every y new changes pushed into NewBranch branch.


Work around:

I agree with the logic of @devpro's answer. But its script does not available for your scenario. Because the pr in YAML only work for GitHub and Bitbucket Cloud repos.

Here you are using VSTS repo, and configured the policy for it. So, you can only via branch policy configuration to avoid such burning trouble.

In your Build validation panel, you are setting build policy with Automatic, right?

enter image description here

Please change the Automatic to Manual, also keep the Policy requirement value as Required.

Now, the corresponding build pipeline will not be ran automatically once new commit pushed, it can only built when someone run it manually.


For the delay timing you noticed which make you unsure, I guess it would because of conflict.

For sample, the Pull request P1 which merge from NewBranch to Master is opening. I commit a new change C1 into NewBranch. BUT, it causes the conflict. At this time, the build will not be ran because the changes does not actually pushed into PR.

Note: The commit is true to NewBranch. BUT changes does not accepted by PR yet, because PR detect out there has conflict here and you must tell PR which changes you want to keep. Only the changes pushed into PR can work with PR trigger.

Only the conflict solved, the changes, maybe I can say commit, can truly be accepted and pushed into PR. Then the triggered build ran.

This would be the time period delay you noticed.

0
votes

I've seen the same behaviour today.

Can you try rewriting the trigger part in your pipeline like this:

trigger:
  branches:
    include:
    - master

Does that help ? I can't verify it myself yet, since I've done that change but the PullRequest isn't approved yet :)

I've also another repository where I do not see that behaviour, but there, my pipeline trigger looks like this:

trigger:
  - master

(Notice the 2 spaces before - master)

0
votes

I think you need to add pr: none to your pipeline definition to disable the automated run.

If pr is not set I think the default is to run on every PR.

That would give something like this:

trigger:
  batch: true
  branches:
    include:
    - master
  paths:
    exclude:
    - README.md

pr: none