在 Azure DevOps 中发布完成后如何创建拉取请求?

How can I create a Pull Request when a release completes in Azure DevOps?

我正在做的一个项目有 2 个长期存在的功能分支以及 master 分支。

为了完全自动化部署,我想在部署从 Azure DevOps Release 退出时从 master 创建一个拉取请求到这两个功能分支。

Azure DevOps 中的哪种工具允许我将拉取请求创建为发布任务?

您可以在发布期间通过Pull Request REST API创建合并请求。

Invoke HTTP REST API task 但可能不符合您的要求。

简单的方法就是通过PowerShell任务:

  1. Select 阶段(例如 运行 在代理上)并检查 允许脚本访问 OAuth 令牌 选项。您也可以使用 Personal Access Token
  2. 添加 PowerShell 任务

简单示例:

param(
[string]$project,
[string]$repo,
[string]$sourceBranch,
[string]$targetBranch,
[string]$title,
[string]$des,
[string]$token
)
$bodyObj=@{
  "sourceRefName"="refs/heads/$sourceBranch";
  "targetRefName"= "refs/heads/$targetBranch";
  "title"= "$title";
  "description"="$des";
}
$bodyJson=$bodyObj| ConvertTo-Json
$uri="https://XXX.visualstudio.com/DefaultCollection/$project/_apis/git/repositories/$repo/pullRequests?api-version=3.0"
Write-Output $bodyJson
Write-Output $uri
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "test",$token)))
$result= Invoke-RestMethod -Method POST -Uri $Uri -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyJson

参数:-project "XXX" -repo "XXX" -sourceBranch "XX" -targetBranch "XX" -title "XX" -des "XX" -token [$(System.AccessToken) or personal access token]

您可以安装 Create Pull Request 扩展,它使您能够从具有多目标分支的发布管道自动创建拉取请求: