有没有办法无限期地保持 pullrequest 的管道运行?

时间:2021-02-24 12:18:22

标签: azure-devops

我们的项目是在一个受监管的区域,我们需要证明 PR 测试结果已经成功运行,以备 1,2 年后的审计。因此,我们需要保留 PR 管道构建而不被删除。

我已设法使用受保护的保留策略存储与主分支相关的流水线构建。 我已经成功地存储了与发布相关的流水线构建。

我找不到如何将 PR 管道存储超过 30 天:

enter image description here

我唯一的想法是手动去并在构建中选择“保留”。

有什么线索吗?

1 个答案:

答案 0 :(得分:1)

您可以使用 rest api Update Build。保留一个构建定义的所有构建的 PowerShell 示例:

$user = ""
$token = "<pat>" #https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page

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

$org = "<org_name>"
$teamProject = "<team_project_name>"
$buildDefId = "<build_definition_id>"

$restApiGetBuilds = "https://dev.azure.com/$org/$teamProject/_apis/build/builds?definitions=$buildDefId&api-version=6.0"
$restApiPatchBuildTemplate = "https://dev.azure.com/$org/$teamProject/_apis/build/builds/{buildId}?api-version=6.0"

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

function InvokePatchRequest ($GetUrl, $body)
{   
    return Invoke-RestMethod -Uri $GetUrl -Method Patch -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
}


$builds = InvokeGetRequest $restApiGetBuilds

foreach($build in $builds.value)
{
    $restApiPatchBuild = $restApiPatchBuildTemplate -replace "{buildId}", $build.id

    InvokePatchRequest $restApiPatchBuild '{"keepForever":true}'
}
相关问题