Reputation: 319
I need PR number on which my CI kickoff.I am using System.PullRequest.PullRequestNumber but it is showing empty string when my CI is running.
Write-Host "PR Number is:-" $env:System.PullRequest.PullRequestNumber
I am not running this CI through save and queue. Following the complete process of PR.
Upvotes: 3
Views: 11251
Reputation: 4556
This worked for me only when running a build triggered by a PR on GitHub. If your build is being triggered outside of the PR context, for example after merging the PR, then this variable will not be available.
That happens because on Azure DevOps there is no way to trigger a build on PR merge: PR triggers on Azure DevOps only work when creating and updating a PR.
Therefore, on the example above, when you merge to master, what actually triggers the build is a CI trigger.
If your code is hosted on GitHub like in my case, you could create a workflow on GitHub to trigger on PR merge only, and do the logic you want there.
on:
pull_request:
types:
- closed
branches:
- master
jobs:
merged-pr:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- run: |
echo This is the PR ${{ github.event.number }}
It is important to understand that, this pipeline will trigger on PR merge to master, whereas the CI triggers on Azure Pipelines will also trigger when you push to master - if configured that way. Therefore, when merging to master, the Azure DevOps pipeline and GitHub workflow will trigger at the same time.
If this is not ok, you could prevent that from happening by turning off the CI trigger on the pipeline, and triggering the build from the GitHub workflow. This can be achieved with this command:
az pipelines build queue --definition-name $azure_devops_pipeline_name --organization $azure_devops_organisation_url --project $project_name --branch master
In my case, I needed the PR number on the Azure DevOps pipeline, so I used a variable group for that. The logic I am running on the GitHub workflow, before triggering the pipeline, is precisely updating that number in a group variable. The command below should do precisely that.
az pipelines variable-group variable update --organization $azure_devops_organisation_url --project $project_name --group-id $azure_devops_variable_group_id --name $azure_devops_variable_name --value ${{ github.event.number }}
Upvotes: 0
Reputation: 399
You can get PR number via System.PullRequest.PullRequestNumber
.
For example I have a pull request on GitHub with this number:
For this PR Write-Host $(System.PullRequest.PullRequestId)
returned 1030194803
And Write-Host $(System.PullRequest.PullRequestNumber)
returned 63.
Upvotes: 0
Reputation: 21
$(System.PullRequest.PullRequestId)
is initialized only if the build ran because of a Git PR affected by a branch policy. The variable is also available only in Classic pipelines (not YAML), as per the documentation.
You can extract the PullRequestId
from:
"$(RELEASE.ARTIFACTS.*YOURARTIFACTNAME*.SOURCEBRANCH)"
Upvotes: 0
Reputation: 76760
How to get the pull request number from predefined variable in azure build pipeline(CI)
First, just like the document System variables state:
The variable System.PullRequest.PullRequestNumber
is populated from GitHub. We should use the System.PullRequest.PullRequestId
.
So, we could use the syntax $(System.PullRequest.PullRequestId)
to get the value:
Write-Host "PR ID is:-" $(System.PullRequest.PullRequestId)
Besides, based on the Understand variable syntax document
When variables are turned into environment variables, variable names become uppercase, and periods turn into underscores. For example, the variable any.variable becomes $ANY_VARIABLE
So, if you get the environment variables $env:System_PullRequest_PullRequestId
instead of $(System.PullRequest.PullRequestId)
So, the scripts should be:
Write-Host "PR ID is:-" $env:System_PullRequest_PullRequestId
That is the reason why System.PullRequest.PullRequestId
not work for you.
Hope this helps.
Upvotes: 5
Reputation: 586
Try use this:
Write-Host "PR Number is:-" $(System.PullRequest.PullRequestId)
EDIT
as per documentation, System.PullRequest.PullRequestNumber is populated from GitHub, are you sure you need this?
and System.PullRequest.PullRequestId:
The ID of the pull request that caused this build. For example: 17. (This variable is initialized only if the build ran because of a Git PR affected by a branch policy.)
Upvotes: 0