Azure Devops pipelines to trigger ONLY on Merge
Asked Answered
Y

2

11

I'm looking on a way to trigger a Azure pipeline ONLY on successful (or attempted) pull request merge.

Now I have :

trigger:
 branches:
  include:
    - DEV

steps:
- script: FOO

But this runs EVERY time there is a change on the DEV branch and I would like to avoid that.

Besides, I want a programmatic response not going trough the UI each time.

EDIT: A weird thing is happnening

condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))

gets:

Expanded: and(True, eq('IndividualCI', 'PullRequest'))" 

When doing a PR, and thus doesn't work as intented

Yelena answered 20/7, 2022 at 14:43 Comment(1)
You could use git to see if the current commit is a merge, then set a variable that you can use in conditions for that pipeline.Faith
U
12

I'm looking on a way to trigger a Azure pipeline ONLY on successful (or attempted) pull request merge.

There is no such out of box way to achieve this at this moment.

We could only set the CI trigger on the target branch, but we could set the condotion for the pipeline to avoid build any task:

and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))

For example:

trigger:
 branches:
  include:
    - DEV

steps:
- script: FOO
  condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))

Or you could set the condition for the stage, job and so on.

Please check the document Specify conditions for some more details.

If there is a change on the DEV branch and it would be avoided by the condition.

Note: With above way, the pipeline will be triggered, but no task will be executed.

And if you even do not want the pipeline be triggered. You could add new pipeline with powershall task to invoke REST API to trigger above pipeline and set the condition to the powershell task.

In this way, the pipeline will only triggered when the commit comes from the PR.

Update:

Doing a PR on the DEV branch results in : "Expanded: and(True, eq('IndividualCI', 'PullRequest'))"

Yes,you are correct. That because azure devops does not have the feature to trigger the pipeline after the PR completed. Pull request trigger and Build Validation both trigger the pipeline when the PR starts.

To resolve this request, we could try create a service hook to monitor PR status. If the PR status changes, the pipeline is triggered through API or Application, you could check this document for some more details.

And another way to achieve is using the REST API.

The main idea is:

  1. create a pipeline and set it as Build validation, but not set it as Required, should set it as Optional:

enter image description here

  1. Add powershell task in above pipeline to invoke REST API to monitor the PR status until it complated, and add another task to invoke the REST API to trigger your current pipeline.

So, you could remove the:

trigger:
 branches:
  include:
    - DEV

in your current pipeline.

Unconventionality answered 25/7, 2022 at 8:46 Comment(7)
Can you give a full dummy example ?Ibbison
@DavidMuñozTord, An example of which case? The first one is still using REST API?Unconventionality
Using the PR condition test.Ibbison
@DavidMuñozTord, please check my update answer for the for example.Unconventionality
@LeoLiu-MST it doesn't seem to work .. Doing a PR on the DEV branch results in : "Expanded: and(True, eq('IndividualCI', 'PullRequest'))"Ibbison
@DavidMuñozTord, Sorry, it's off hours, I'll actually create a test case tomorrow to refine the answerUnconventionality
@DavidMuñozTord, Have updated the answer, please check if it help you.Unconventionality
K
2

The trigger you have set is a CI trigger, it will work whenever the target branch has a new commit.

Currently, there isn't a trigger that works when a pull request is completed.

The feature closest to your needs is PR triggers and build validation branch policy. They will work when a pull request is created or when it has been changed.

If you are using Azure Repos Git, please use branch policy for build validation. If you are using GitHub or Bitbucket Could, please use pr triggers. Click the documents for the detailed information.

Besides, you can use branch policy to prevent the direct commits. When you set the branch policy of any type, only users with "Bypass policies" permission can commit to the branch directly. The rest of the users must commit the branch through a pull request.

How to create branch policy: Branch policies and settings.

How to set "Bypass policies" permission: Set Git repository permissions.

Kiangsu answered 21/7, 2022 at 5:23 Comment(3)
Hello, thank for this answer but that I have seen all these suggestions already and none of them is satisfying.. Besides, I want a programmatic response not going trough the UI each time.Ibbison
Hello @DavidMuñozTord, could you please give more detailed about "programmatic response"? For branch policies, you need to set it through UI, but then the pipeline will be triggered "programmatic".Kiangsu
I would like to do it through a yml script, because I would have to do it for dozens of repos.Ibbison

© 2022 - 2024 — McMap. All rights reserved.