当您将构建设置为拉取请求中的构建验证时,构建将在完成拉取请求之前执行。所以它不会满足你的要求。
关于Branch Policy,你可以参考这个文档。
知道如何有条件地向合并提交添加标签吗?
由于变量System.PullRequest.TargetBranch和System.PullRequest.SourceBranch只存在于Pull Request Trigger Build中,您可以尝试以下设置来设置管道。创建两个管道:管道 1 和管道 2。
在 Pipeline 1 中,您可以设置条件并使用Rest API 来触发 pipeline 2。
在 Pipeline 2 中,您可以在 Pipeline 2 中添加一个 Environment,该 Environment 包含检查:Invoke Rest API to check the status of the Pull Request。如果 API 响应满足要求(Pull Reuqest Status 完成),就会运行 git 命令添加标签。
这是我的示例:
管道 1:
trigger: none
pool:
vmImage: 'windows-latest'
jobs:
- job: Tag_As_Hotfix
condition: and( eq(variables['System.PullRequest.TargetBranch'],'refs/heads/master'), startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/test'))
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$token = "PAT"
$url="https://dev.azure.com/{Org}/{Project}/_apis/distributedtask/variablegroups/{VariableGroup ID}?api-version=5.0-preview.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
"variables": {
"id": {
"value": "$(SYSTEM.PULLREQUEST.PULLREQUESTID)"
}
},
"type": "Vsts",
"name": "New variable group 16-Nov"
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PUT -Body $JSON -ContentType application/json
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$token = "PAT"
$url="https://dev.azure.com/{ORG}/{Project}/_apis/build/builds?api-version=5.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
"definition": {
"id": BuilddefinitionID
}
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
注意:您需要创建一个变量组来保存拉取请求 ID。此 id 可用于Invoke Rest API check。
管道2:
trigger:
- none
pool:
vmImage: 'windows-latest'
stages:
- stage: deploy
jobs:
- deployment: DeployWeb
displayName: deploy Web App
environment: 'API Test'
strategy:
runOnce:
deploy:
steps:
- script: |
git tag hotfix
git push origin master
环境: 管道 -> 环境 -> 批准和检查 -> 调用 Rest API
变量组:
工作流程
创建拉取请求->触发管道1->触发管道2并更新变量组中的ID->管道2检查拉取请求状态->运行命令添加标签。