Conditions - Differentiate pull request and pull request merge - azure-devops

Let's say I have a pipeline my-pipeline which 1) Install dependencies 2) Test my application.
I set up a build validation policy which triggers my-pipeline upon creation of a pull request.
Once the pull request is approved and merged, my-pipeline is triggered again.
Questions
Is there a way to differentiate the creation of the PR and the merged of the PR within my yaml file, so I can use conditions to trigger some scripts on creation but not on merge. Something similar to condition: and(succeeded(), startsWith(variables['build.reason'], 'PullRequest'))
For instance, I would trigger test on PR creation but not after the merge

Related

PR Ignore failed step Azure Devops

i have a problem with pull request policies, in CI pull request i have opcional stage. i can aprove or reject opcional stage but when reject the build pipeline is failed.
is there a way is build PR is sucessed if one step is rejected?
As long as a stage is rejected or fails, even if you can continue to run later stages by adding conditions,
condition: always()
the final status of the build pipeline is still failed. This is an expected action.
And if you set Build Validation as a required branch policy, the validated build must succeed in order to complete the pull request.

Github running pull request build pipeline despite pull request validation being disabled from Azure pipeline

I have a duplicate pipeline let us call is A and B. I have disabled pull request validation for the pipeline B, however despite the change when a pull request is raised A and B still both run, and what is worrying is that because automation execution has been disabled for pipeline B, it holds up the pull request and becomes a blocker. The way around is to manually run pileine B.
I cannot seem to remove pipeline B from the validation checks for pull requests. How can this be done ?
I have two same pipelines in Azure DevOps. "EnablePR-A" and "DisablePR-B".
The trigger setting is as below:
When a PR is made in GitHub repo, A is triggered.
To check the trigger setting in your pipeline:
Also, you could check how your pipeline is being triggered:

GitHub action workflow executed without being called in on statement

I have made a pull request on GitHub for a workflow that should be invoked in the following event:
on:
pull_request_review:
types: [submitted,edited]
Despite the fact that no pull request review has been done yet, it seems the workflow did create a status check in the PR that adds the particular file.
Why is that?
Shouldn't the workflow be executed only when a PR review is submitted?

Azure Devops pipelines to trigger ONLY on Merge

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
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:
create a pipeline and set it as Build validation, but not set it as Required, should set it as Optional:
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.
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.

Azure pipelines variable for PR number of a merged pull request

Azure dev-ops pipelines have predefined varialbles related to github pull requests. I can use SYSTEM_PULLREQUEST_PULLREQUESTNUMBER for getting PR number that triggered my pipeline. However I get no value from SYSTEM_PULLREQUEST_PULLREQUESTNUMBER when my pipeline is triggered again as a result of merging this PR in the main repo.
My use case is to identify the list of files that were changed in the original PR.
I looked into Azure user predefined variable document but could not see if there is any variable available to get this information.
When the pipeline is ran the second time when it is merged, it is considered to have the trigger type CI not Pull Request. Therefore the PR number is unavailable in this context because there was no PR.
You could also try to save the pull request number to a variable group in the previous run triggered by a pull request.
https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/cli/pipeline-variable-group-secret-nonsecret-variables?view=azure-devops
You could try and steal the pull request number from the commit message. If your PR merge type is set to 'squash commit' you can write some regex to pull the PR number out of the commit message.
https://learn.microsoft.com/en-us/azure/devops/repos/git/merging-with-squash?view=azure-devops#squash-merge