gpt4 book ai didi

azure - 如何在 Azure 上获取构建名称?

转载 作者:行者123 更新时间:2023-12-03 06:54:19 27 4
gpt4 key购买 nike

我的目标是从 Azure YAML 管道代码获取构建名称。例如,这里是#20220803.9 JBS-2413 尚未批准,这是一个自动化交易的实验...

enter image description here

我看了这里,但没有运气

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

我尝试了 Build.DefinitionName 它返回 PR

我尝试了 Build.BuildNumber 它仅返回 20220803.9

我尝试了Build.SourceVersionMessage它返回Merge

最佳答案

没有内置的预定义变量或其他东西来获取您想要的“构建名称”。如果你拦截网络流量分析,你还会发现你想要的“构建名称”并不是一个整体,它是两个数据的组合。

您需要设计自己的代码才能获得它。例如,如果您基于 Azure Git Repo,下面的管道 YAML 可以帮助您在创建拉取请求以触发管道时获取所需的“构建名称”:

trigger:
- none


# 1
stages:
- stage: s1
displayName: Get the lastest comment
jobs:
- job: testJob
steps:
- task: PowerShell@2
name: setvar
inputs:
targetType: 'inline'
script: |

$PAT = "<Your Personal Access Token>"

$org_name = "<Organization Name>"
$project_name = "<Project Name>"
$branch_name = "<Current Branch Name>"
$def_id = "<Pipeline definition ID>"
$run_id = "$(Build.BuildId)"
Write-Host $run_id

if("$(Build.Reason)" -eq "PullRequest") {
$headers_run = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers_run.Add("Authorization", "Basic "+$PAT)
$run_url = "https://dev.azure.com/"+$org_name+"/"+$project_name+"/_apis/pipelines/"+$def_id+"/runs/"+$run_id+"?api-version=6.0-preview.1"
$response_run = Invoke-RestMethod $run_url -Method 'GET' -Headers $headers_run
$response_run | ConvertTo-Json
$pull_request_id = $response_run.variables."system.pullRequest.pullRequestId".value

$headers_pr = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers_pr.Add("Authorization", "Basic "+$PAT)
$pr_url = "https://dev.azure.com/"+$org_name+"/"+$project_name+"/_apis/git/repositories/ShowBuildName/pullrequests/"+$pull_request_id+"?api-version=6.0"
$response_pr = Invoke-RestMethod $pr_url -Method 'GET' -Headers $headers_pr
$response_pr | ConvertTo-Json
Write-Host $response_pr.title

$str = $response_pr.title
Write-Host "##vso[task.setvariable variable=outputvars;isOutput=true]$str"
}
else {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic "+$PAT)

$url = "https://dev.azure.com/"+$org_name+"/"+$project_name+"/_apis/git/repositories/ShowBuildName/commits?searchCriteria.itemVersion.version="+$branch_name+"&api-version=6.0"

$response = Invoke-RestMethod $url -Method 'GET' -Headers $headers
$response | ConvertTo-Json

$str = $response.value[0].comment
Write-Host "##vso[task.setvariable variable=outputvars;isOutput=true]$str"
}


# 2
- stage: s2
displayName: Get the "build name"
dependsOn: s1
variables:
vars: $[ stageDependencies.s1.testJob.outputs['setvar.outputvars'] ]
jobs:
- job:
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Write-Host "$(Build.BuildNumber) $(vars)"

管道运行:

enter image description here

成功获取“构建名称”:

enter image description here

关于azure - 如何在 Azure 上获取构建名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73224059/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com