首页 > 解决方案 > 是否可以从 Azure Devops Pipelines 上的控制选项的拉取请求中获取参数?

问题描述

我在 Azure 开发操作上有一个简单的管道,它有 4 个作业

这样可行。目前,每次对主分支进行 PR 时,版本碰撞只会“修补”。所以它只发布补丁版本......我有一个自定义条件: contains(variables['Build.SourceBranch'], 'refs/heads/master')。好吧,我想要在这里再添加一个条件,该条件决定动态地将版本作为补丁,主要或次要版本......所以我想从拉取请求标题或描述中获取该参数......:

contains(variables['Build.PR.Title'], 'patch') contains(variables['Build.PR.Title'], 'major') contains(variables['Build.PR.Title'], 'minor')

如果 pr 标题有“补丁”、“主要”或“次要”,那确实是 3 个不同的工作将被解雇。有没有可能做这样的事情,或者有没有更简单的方法?:)

提前谢谢!

标签: azure-devopsazure-pipelines

解决方案


Azure Devops 管道没有通过 prs 的标题。您可以在此处找到预定义变量:使用预定义变量。要获取活动 Pull Request 的详细信息,您可以使用 Rest Api Pull Requests - Get Pull Request By Id with System.PullRequest.PullRequestId。作为获取标题的示例:

  1. 添加自定义变量定义变量,如 Custom.PRTitle。
  2. 在您的工作中设置对 PAT 的访问权限。System.AccessToken
  3. 将第一步添加为脚本以读取和保存拉取请求的标题,例如使用 PowerShell:
$user = ""
$token = "$(System.AccessToken)"
$teamProject = "$(System.TeamProject)"
$PRId = "$(System.PullRequest.PullRequestId)"
$orgUrl = "$(System.CollectionUri)"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uriGetActivePr = "$orgUrl/$teamProject/_apis/git/pullrequests/$PRId"

$resultPR = Invoke-RestMethod -Uri $uriGetActivePr -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

Write-Host "##vso[task.setvariable variable=Custom.PRTitle]"$resultPR.title

然后你可以在你的条件下使用你的新变量,比如:

and(contains(variables['Build.SourceBranch'], 'refs/heads/master'), contains(variables['Custom.PRTitle'], 'patch'))

推荐阅读