Azure pipelines variable for PR number of a merged pull request - azure-devops

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

Related

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:

Conditions - Differentiate pull request and pull request merge

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

How can I update a vsts pull request after a release finishes?

I am trying to find the configuration required for using VSTS external services to make a VSTS release required to be successful before the PR to master can be completed.
The workflow is:
Create PR -> Triggers Build -> On Success Triggers Release -> On Success will flag the PR as OK.
Is there a way to do this using external services to post a successful status?
For the release definition, you can triggerred by the PR validation build artifacts, so you can get the pull request id by the pre-definied variable Release.Artifacts.{alias}.SourceBranch.
Assume the artifact alias for the release definition is prvalid, then you can get the pull request id (as the variable $id below) with below script:
$id="$(Release.Artifacts.prvalid.SourceBranch)".Split('/')
echo $id
$id=$id[2]
And then you can get the source branch and target branch by the REST API:
GET https://acount.visualstudio.com/DefaultCollection/_apis/git/repositories/repoID/pullRequests/PRid?api-version=3.0
Then you can merge the source branch into tartget branch, and the PR status will be completed.

Jenkins Github Pull Request Merger does not respect statuses

I'm trying to set up an automated CI process GitHub and Jenkins. The goal is to have developers create feature branches and generate pull requests that are automatically merged (if they pass build, of course) using Jenkins Github Pull Request Merger.
It is a further goal to require that pull requests be against an open GitHub issue. For us, that means that either the pull request title or at least one of the pull request commit messages must contain a substring like "fixes #NN" where #NN must reference an open GitHub issue. This 'issue_opened' check is also automated - our 'issue_opened' GitHub App queries the GitHub issues and examines commit messages and the PR title, then it POSTs the pull request with a status (for testing purposes I'm always posting 'failure').
The process envisioned is as follows:
1. Feature branch pushes are automatically built by Jenkins.
2. When a feature branch is ready and passes Jenkins testing, a developer will generate a pull request; this automatically triggers steps 3 & 4, each running independently:
3. Our 'issue_opened' GitHub App very quickly POSTs a status to the pull request.
4. Jenkins performs the build - it takes much longer than step 3. If the build passes, Jenkins applies that status. If all statuses are 'success', the pull request is automatically merged.
What I observe:
Currently, my pull requests are merging feature branches to master. Master is protected (GitHub master branch: Settings>Branches>Protect this branch>Require status checks to pass before merging and the 'issue_opened' status check is set to Required.) Everything works as planned, except that the Github Pull Request Merger breaks GitHub convention and only respects its own status, not the other statuses.
So the PR merge depends only on Jenkins:
After step 3 POSTs a 'failure' status but before step 4 completes, GitHub reports that "Required statuses must pass before merging" and indicates that the 'issue_opened' status is 'failure'. But when the Jenkins build succeeds, the merge takes place anyway.
FWIW, the merge also happens if the feature branch already has a bad status at the time the pull request is created.
Any way I can get this to do what I want?
After more desperate fiddling I tried enabling the GH master branch protection setting Settings>Branches>Protected Branches>master>Protect this branch>Include administrators and 'voila': it pretty much works for me, more or less:
Jenkins Github Pull Request Merger still tries to do the merge, but GitHub returns this:
HTTP response code: 405, message: 'Method Not Allowed'.
As a result of the rc 405, Jenkins generates a java.io.IOException and regurgitates this json message from GH:
{"message":"2 of 2 required status checks have not succeeded: 1 failing and 1 pending.","documentation_url":"https://help.github.com/enterprise/2.10/user/articles/about-protected-branches"}
Jenkins then POSTS a 'failure' status (which one might quibble over because the build itself didn't fail).
This makes sense, since I am an admin for this repo, but I didn't anticipate that the Jenkins Github Pull Request Merger would not check the statuses. But I'm very pleased that this will get the job done for me, though from my point of view it would be cleaner if Jenkins first posted the build status, then POSTed the merge. Even better, if it checked the statuses it could simply skip the attempt to POST, and I wouldn't have had to enable the Include administrators protection. As it stands, I don't see a way to clear the Jenkins-posted failure status on the pull request. So we'll have to close such failed pull requests and create new ones.
Additional Info
Since posting initial answer I have discovered that one must not set/enable the GitHub Branch protections status check that comes from the Jenkins build. If it is not enabled, one can close the failed pull request, correct whatever problems caused other status checks or the Jenkins build to fail, and then start the pull request process over again by opening a new pull request.

How to name the custom workspace in Jenkins job with branch name triggered by Github webhook?

I have created a single job in Jenkins to be triggered by commit on any branches in the GitHub using Webhook.
I want to create a custom workspace for each job trigger with respective to the branch commit.
I Tried following options and could not achieve it. Appreciate for any help.
Job Name : Test_Clone
Used advance options in general section and tried below options to name the workspace dynamically.
Test_Clone_${GIT_BRANCH} - always gives the branch name from previous build.
Test_Clone_${ref} - tried to use ref as defined in GitHub webhook payload, with no result.
To achieve: Ex: Commit from GitHub branch release-2.13.0, Jenkins should create workspace with name Test_Clone_release-2.13.0.
You might try and setup
first a payload variable in your Jenkins build, as described in "How to process a github webhook payload in Jenkins?"
then a webhook url (on GitHub side) of:
http://<<yourserver>>/job/<<yourjob>>/buildWithParameters?token=<<yourtoken>>
Then your Jenkins job would have the xml webhook payload in it, which you can analyze (with jq) and extract the 'ref' which includes the branch name.
From there, I would call (chain) a second job with a "branch" parameter (unless you can write a pipeline DSL with Jenkins 2) with that branch name, in order for the second job to use a custom workspace name based on that parameter