azure-devops - 是否可以从 Azure Devops Pipelines 上的控制选项的拉取请求中获取参数?
问题描述
我在 Azure 开发操作上有一个简单的管道,它有 4 个作业
- Npm 我
- Npm 运行测试
- Npm 版本 Bump
- npm 发布
这样可行。目前,每次对主分支进行 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 Devops 管道没有通过 prs 的标题。您可以在此处找到预定义变量:使用预定义变量。要获取活动 Pull Request 的详细信息,您可以使用 Rest Api Pull Requests - Get Pull Request By Id with System.PullRequest.PullRequestId
。作为获取标题的示例:
- 添加自定义变量定义变量,如 Custom.PRTitle。
- 在您的工作中设置对 PAT 的访问权限。System.AccessToken
- 将第一步添加为脚本以读取和保存拉取请求的标题,例如使用 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'))
推荐阅读
- java - AAPT:错误:找不到属性 android:clipToOutline
- flask - Flask WTForm:提交按钮不会产生错误
- javascript - 未正确假定 JS 获取请求的标头参数
- javascript - 错误:属性或方法“用户”未在实例上定义,但在渲染期间被引用
- javascript - 使用多个条件检查数组中的任何两个对象是否相等
- google-maps - 在 Firestore 文档/地理定位器包中保存 LatLng(geopoint) 值
- flutter - Firebase 应用分发不适用于 Codemagic
- laravel - 未能创建符号链接“公共/存储/公共”:文件存在
- python - 从不知道这个键的单项 Python 字典中获取密钥的惯用方法(但知道字典只有一项)?
- mysql - Go []byte 未存储在 MySQL 数据库中