Azure DevOpsMaven
我在Azure DevOps中确实有一些旧的构建管道,我想使用Power Shell删除它们。我写了一个小的powershell脚本,首先获取构建定义,然后获取构建id,名称等。在删除定义之前,必须先删除保留版本。程序代码已准备好,但API需要ownerId或userId或最小保留租约。这就是我的挑战!
我读了https://learn.microsoft.com/en-us/rest/api/azure/devops/build/leases?view=azure-devops-rest-6.0文档和后面的代码(托管在GitHub https://github.com/microsoft/azure-devops-node-api/blob/470 f9 ca 7 bdfccd 87 e1 c1 fdea 8023 b8 c3 d2 b1047 a/api/interfaces/BuildInterfaces.ts#L2006),但我不知道ownerId或userId是什么,也不知道如何找到它。
$personalToken = "AzureDevOpsPersonalToken"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}
$organization = "myOrga"
$project = "myProj"
$pipelineName = Read-Host "Please enter pipeline to delete"
#all build definitions
$url = "https://dev.azure.com/$organization/$project/_apis/build/definitions?api-version=6.0-preview.7"
$builddefinitions = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$builddefinitions.value | Sort-Object id|ForEach-Object {
Write-Host $_.id $_.name $_.queueStatus
if ( $_.name -ne $pipelineName ) {
return;
}
#all builds for a definition
$url = "https://dev.azure.com/$organization/$project/_apis/build/builds?definitions=" + $_.id + "&api-version=6.0-preview.5"
$builds = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$builds.value | Sort-Object id|ForEach-Object {
#report on retain status
Write-Host " BuildId:" $_.id " retainedByRelease:" $_.retainedByRelease
# ownerId UNKNOWN - where is this come from?
$url = "https://dev.azure.com/$organization/$project/_apis/build/retention/leases?ownerId=&api-version=6.0-preview.1"
$leases = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
Write-Host $leases
#$url = "https://dev.azure.com/$organization/$project/_apis/build/retention/leases?ids=" + $_.id + "&api-version=6.0-preview.1"
#Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
#Write-Host " BuildId:" $_.id " removed retentions"
#
#$url = "https://dev.azure.com/$organization/$project/_apis/build/builds/" + $_.id + "?api-version=6.0-preview.1"
#Invoke-RestMethod -Uri $url -Method DELETE -ContentType "application/json" -Headers $header
#Write-Host " BuildId:" $_.id " deleted"
}
#$url = "https://dev.azure.com/$organization/$project/_apis/build/definitions/" + $_.id + "?api-version=6.0-preview.1"
#Invoke-RestMethod -Uri $url -Method Delete -ContentType "application/json" -Headers $header
#
#Write-Host " Pipeline:" $pipelineName " (" $_.id ") deleted"
Write-Host
}
你能帮帮我吗?提前感谢Tino
3条答案
按热度按时间n1bvdmb61#
Azure DevOps Portal中的内容经常更改。在2022年2月,我用几个Stackoverflow帖子编写了自己的脚本,以删除旧的构建管道,它们的构建和租赁。
k7fdbhmy2#
与此同时,我发现我如何从一个未知的人在https://tenbulls.co.uk/2020/03/25/delete-old-build-definitions-in-azure-devops/。
技巧是使用retainedByRelease=false对retainedByRelease值进行**修补。
fkaflof63#
仅供参考,我运行了上面的脚本,仍然有问题,它不允许我删除所有保留的构建。相反,我必须查询当前构建的租约,并分别删除它们,然后才能删除构建。我从另一个答案修改后的循环如下。