Reputation: 2173
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.
Upvotes: 2
Views: 1613
Reputation: 66
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
Upvotes: 3
Reputation: 4556
As explained in this answer, if your build is being triggered outside of the PR context, for example after merging the PR, 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.
As mentioned in the answer I quoted, you could use variable groups to store that value. But when I had to deal with this issue, I would update that variable each time a PR is created or updated. When multiple pull requests are created and updated, it is not possible to ensure consistency of the value contained in that variable.
To address that problem, and given that my code is hosted on GitHub, I created a workflow on GitHub to trigger on PR merge only.
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 trigger on the Azure pipeline will also trigger when pushing to master. Therefore, when merging to master, the Azure pipeline and GitHub workflow will trigger at the same time.
Given that I wanted to update a variable group value on the GitHub workflow and read that value on the Azure pipeline, having the two running concurrently would not work out. By turning off the CI trigger on the Azure pipeline, and triggering the build from the GitHub workflow, I was able to address that issue. The command below should do exactly that:
az pipelines build queue --definition-name $azure_devops_pipeline_name --organization $azure_devops_organisation_url --project $project_name --branch master
To update the value in the group variable I use the command below.
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