You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps Pipeline如何在PR合并时触发并获取PR编号?

实现方案

以下两种方式均无需依赖Webhook或Git提交消息,且触发逻辑完全包含在流水线YAML配置中:

方案1:CI触发器+REST API关联PR编号

通过监听main分支的推送事件(PR合并会产生推送)实现触发,同时通过Azure DevOps REST API查询当前提交对应的已合并PR编号。

YAML配置示例

# 触发规则:监听main分支推送 + 定时触发
trigger:
  branches:
    include:
      - main

schedules:
  - cron: "0 0 * * *" # 自定义定时规则,这里是每天凌晨触发
    displayName: 每日定时构建
    branches:
      include:
        - main
    always: true # 即使无代码变更也执行定时任务

jobs:
- job: ExecuteScript
  steps:
  # 第一步:获取关联的已合并PR编号
  - task: PowerShell@2
    name: FetchPRNumber
    inputs:
      targetType: 'inline'
      script: |
        $commitId = "$(Build.SourceVersion)"
        $orgUri = "$(System.TeamFoundationCollectionUri)"
        $project = "$(System.TeamProject)"
        $repoId = "$(Build.Repository.Id)"
        $authToken = "$(System.AccessToken)"

        # 调用REST API查询提交关联的PR
        $headers = @{Authorization = "Bearer $authToken"}
        $apiUrl = "$orgUri$project/_apis/git/repositories/$repoId/commits/$commitId/pullrequests?api-version=7.1-preview.1"
        $prResponse = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Get

        # 过滤出已合并的PR
        $mergedPR = $prResponse.value | Where-Object { $_.status -eq "completed" -and $_.isMerged -eq $true }
        if ($mergedPR) {
          $prNum = $mergedPR.pullRequestId
          Write-Host "##vso[task.setvariable variable=PRNumber;isOutput=true]$prNum"
          Write-Host "已找到关联的合并PR:#$prNum"
        } else {
          Write-Host "当前提交无关联的合并PR(可能是直接推送或定时构建)"
          Write-Host "##vso[task.setvariable variable=PRNumber;isOutput=true]0"
        }

  # 第二步:执行业务脚本,传入PR编号
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        echo "开始执行脚本,PR编号:$(FetchPRNumber.PRNumber)"
        # 替换为你的业务脚本逻辑,使用$(FetchPRNumber.PRNumber)作为PR编号参数

注意事项

  • 流水线服务账户默认拥有读取PR的权限,若权限受限需在项目设置中调整
  • 该方案会响应所有main分支的推送(包括直接push和PR合并),若无需处理直接push,可在任务中增加条件判断

方案2:PR完成事件触发器

直接利用Azure DevOps的PR关闭事件触发流水线,仅在PR合并时执行逻辑,且可直接获取PR内置变量。

YAML配置示例

# 触发规则:PR合并到main时触发 + 定时触发
pr:
  branches:
    include:
      - main
  events:
    - closed # PR关闭时触发(包含合并和未合并关闭)

trigger:
  branches:
    include:
      - main # 可选,若需要响应直接push到main的情况

schedules:
  - cron: "0 0 * * *"
    displayName: 每日定时构建
    branches:
      include:
        - main
    always: true

jobs:
- job: ExecuteScript
  # 仅在定时构建或PR合并时执行
  condition: or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['System.PullRequest.Merged'], 'True'))
  steps:
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        if [ "$(Build.Reason)" = "Schedule" ]; then
          echo "执行定时构建任务"
          # 定时任务逻辑
        else
          echo "执行PR合并后的任务,PR编号:$(System.PullRequest.PullRequestId)"
          # PR合并后的业务逻辑,使用$(System.PullRequest.PullRequestId)作为参数
        fi

优势

  • 无需额外API调用,直接使用Azure DevOps内置的PR变量
  • 触发逻辑精准,仅在PR合并或定时任务时执行,避免无效触发

内容的提问来源于stack exchange,提问作者cafe123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 07:16:18