Azure Devops 批量删除CICD的Pipeline&Release
使用Azure Devops进行项目的发布简单高效,完全解放了手工运维的麻烦。在使用Azure Devops Pipeline的时候,每次运行Build 和 Release 都会产生一条新的记录,在历经长时间的使用后,历史的运行记录已经没有了实质意义,手工删除比较麻烦,费时费力。所以写了个脚本批量删除这些运行记录。
Azure Devops 接口文档可以在这里找到。 https://docs.microsoft.com/zh-cn/rest/api/azure/devops
直接上脚本,如果不会用,请看后面的介绍。
-
删除 Pipeline 中的 Build 记录
由于每次 Build 都会产生 retention lease, 需要清理掉所有Build的 retention lease,才能删除Build,否则会报错。所以删除 Build 记录 需要一次运行下面两个脚本。
$organization = "{组织名称}" $project = "{项目名称}" $personalAccessToken = "{授权Token}" $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)")) $header = @{authorization = "Basic $token"} write-host $token $definitionId = {DefinitionId, 一个数值} function DeleteLease($definitionId) { $url = "https://dev.azure.com/$organization/$project/_apis/build/retention/leases?api-version=6.0-preview.1&definitionId=$definitionId" $leases = (Invoke-RestMethod -Method GET -Uri $url -ContentType "application/json" -Headers $header ) write-host $url foreach ($lease in $leases.value) { $url = "https://dev.azure.com/$organization/$project/_apis/build/retention/leases?ids=$($lease.leaseId)&api-version=6.0-preview.1" write-host $url $ignore = Invoke-RestMethod -Method DELETE -Uri $url -ContentType "application/json" -Headers $header } } DeleteLease $definitionId cmd /c "pause"
$organization = "{组织名称}" $project = "{项目名称}" $personalAccessToken = "{授权Token}" $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)")) $header = @{authorization = "Basic $token"} write-host $token $definitionId = {DefinitionId, 一个数值} function DeleteLease($definitionId) { $url = "https://dev.azure.com/$organization/$project/_apis/build/builds?api-version=6.0&definitions=$definitionId" $builds = (Invoke-RestMethod -Method GET -Uri $url -ContentType "application/json" -Headers $header ) write-host $url foreach ($build in $builds.value) { $url = "https://dev.azure.com/$organization/$project/_apis/build/builds/$($build.id)?api-version=6.0" write-host $url $ignore = Invoke-RestMethod -Method DELETE -Uri $url -ContentType "application/json" -Headers $header } } DeleteLease $definitionId cmd /c "pause"
-
删除 Pipeline 中的 Release 记录
$organization = "{组织名称}" $project = "{项目名称}" $personalAccessToken = "{授权Token}" $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)")) $header = @{authorization = "Basic $token"} write-host $token $definitionId = {DefinitionId, 一个数值} function DeleteLease($definitionId) { $url = "https://vsrm.dev.azure.com/$organization/$project/_apis/release/releases/?api-version=6.0&definitionId=$definitionId" $releases = (Invoke-RestMethod -Method GET -Uri $url -ContentType "application/json" -Headers $header ) write-host $url foreach ($release in $releases.value) { $url = "https://vsrm.dev.azure.com/$organization/$project/_apis/release/releases/$($release.id)?api-version=6.0" write-host $url $ignore = Invoke-RestMethod -Method DELETE -Uri $url -ContentType "application/json" -Headers $header } } DeleteLease $definitionId cmd /c "pause"
一些提示
-
organization {组织名称}、project {项目名称)、definitionId {DefinitionId}在哪里找到?
随意打开一个属于你的Build记录, 在Url中可以找到如下结构,https://dev.azure.com/{组织名称}/{项目名称}/_build?definitionId=DefinitionId
-
personalAccessToken(授权Token)如何获取?
在 Azure Devops Protal 上右上角找到 User Settings > Personal Access Tokens 生成新的Token。
评论