如何区分拉取请求(PR)创建与更新触发的Azure DevOps流水线构建
I get it—using $(Build.Reason) alone doesn’t cut it because both initial PR creation and updates trigger a PullRequest value. Here’s a reliable way to distinguish between the two scenarios and run your task only once when the PR is first created:
Approach: Use Azure DevOps REST API to Count Completed Builds for the PR
The core idea is to check how many completed builds have been run for the current PR. If the count is 0, this means it’s the first time the pipeline is running for that PR.
Step 1: Add a Script Task to Check Build Count
Add a PowerShell (or Bash) task early in your pipeline to query the Azure DevOps API and set a custom variable indicating if this is the first PR run.
PowerShell Script Example:
# Get pipeline context variables $orgUri = "$(System.TeamFoundationCollectionUri)" $projectName = "$(System.TeamProject)" $prId = "$(System.PullRequest.PullRequestId)" $buildDefId = "$(System.DefinitionId)" $accessToken = "$(System.AccessToken)" # Set up authorization header $headers = @{ Authorization = "Bearer $accessToken" } # API endpoint to get completed builds for the PR and build definition $apiUrl = "$orgUri$projectName/_apis/build/builds?definitions=$buildDefId&pullRequestId=$prId&statusFilter=completed&api-version=7.1-preview.7" # Invoke API request $buildsResponse = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Get # Count completed builds $completedBuildsCount = $buildsResponse.count # Set pipeline variable: IsFirstPRRun = true if no completed builds yet if ($completedBuildsCount -eq 0) { Write-Host "##vso[task.setvariable variable=IsFirstPRRun]true" Write-Host "This is the first run for PR #$prId" } else { Write-Host "##vso[task.setvariable variable=IsFirstPRRun]false" Write-Host "$completedBuildsCount completed builds already exist for PR #$prId" }
Step 2: Add Condition to Your Target Task
Modify your task to run only when:
- The build reason is
PullRequest - The custom
IsFirstPRRunvariable istrue - The previous steps succeeded (optional, adjust as needed)
YAML Example for the Task:
- task: YourTaskName@1 displayName: "Run only on initial PR creation" condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'), eq(variables['IsFirstPRRun'], 'true')) inputs: # Your task inputs here
Key Notes:
- Permissions: Ensure the service account running your pipeline (usually
Project Collection Build Service ({Org Name})) has the Read permission for builds in your project. This is enabled by default in most cases. - Edge Cases: If the first build fails and a new commit triggers another run, the task won’t run again (since there’s now 1 completed build). This aligns with your requirement of running once on initial PR creation, regardless of build success.
- API Version: The example uses API version
7.1-preview.7—you can update this to a stable version if preferred, but preview versions usually have the latest features.
Alternative Approach (Using PR Tags)
If you don’t mind modifying the PR, you could add a tag to the PR during the first run, then check for that tag in subsequent runs. However, this requires additional API calls to modify the PR and may not be ideal if you want to keep PR tags clean.
The build count method is more straightforward and doesn’t alter the PR itself, making it the recommended solution.
内容的提问来源于stack exchange,提问作者Ajit

