我正在寻求实施自动化 SemVer 流程。
假设我有一个打开的 PR,其中包含专业的标签/标签,在将其合并到主标签后是否可以检索该标签?
当 PR 合并到 main 时,我想触发一个读取标签的管道,然后自动在存储库上更改标签版本,这在 Azure DevOps 中可能吗?我知道这可以在 GitHub 中完成。
我们可以使用两个 REAT API Pull Request Labels - Get 和 Annotated Tags - Create 来完成该过程。
以下是我的测试步骤:
PAT
的秘密变量。Build.SourceVersionMessage
包含“Merged PR”字样时,作业才会运行。然后我们在PowerShell脚本中使用REST API从PR中获取标签值并将标签添加到分支中。如果没有,该作业将被跳过。
trigger:
- main
pool:
vmImage: Windows-latest
jobs:
- job: A
condition: eq(contains(variables['Build.SourceVersionMessage'], 'Merged PR'), True)
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
# Set your Azure DevOps organization, project, repository
$organization = ""
$project = ""
$repositoryId = "" #repo name or repo id
$token="$(PAT)"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token)))
$string = "$(Build.SourceVersionMessage)"
$splitString = $string.Split(" ")
$pullRequestId = $splitString[2].Replace(":", "")
Write-Host "pullRequestId is $pullRequestId"
# Invoke the REST API to get the label for the pull request
$uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId/labels?api-version=7.1-preview.1"
try {
$labelResponse = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$labelName = $labelResponse.value[0].name
Write-Host "Label for Pull Request $pullRequestId : $labelName"
} catch {
Write-Host "Error retrieving label for Pull Request $pullRequestId : $_"
exit 1
}
$tagName = $labelName
$tagMessage = "Release $tagName"
$commitId = "$(Build.SourceVersion)"
# Invoke the REST API to create the annotated tag
$uri = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/annotatedtags?api-version=7.1-preview.1"
$tagBody = @{
name = $tagName
taggedObject = @{
objectId = $commitId
}
message = $tagMessage
}
try {
$tagResponse = Invoke-RestMethod -Uri $uri -Method Post -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body ($tagBody | ConvertTo-Json) -ContentType 'application/json'
# Extract the tag URL
$tagUrl = $tagResponse.url
Write-Host "Annotated tag created: $tagUrl"
} catch {
Write-Host "Error creating annotated tag: $_"
exit 1
}
测试结果: