如何使用 Power Shell 删除 Azure DevOps 旧构建管道和租约

use*_*131 9 powershell azure devops azure-pipelines

您好 Azure DevOps 专家

我在 Azure DevOps 中确实有一些旧的构建管道,我想使用 Power Shell 删除它们。我编写了一个小的 power shell 脚本来首先获取构建定义,然后获取构建 ID、名称等。在删除定义之前,必须删除保留的版本。程序代码已准备就绪,但API需要ownerId或userId或Minimal Retention Leases。这就是我的挑战!

我阅读了文档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/470f9ca7bdfccd87e1c1fdea8023b8c3d2b1047a/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
}
Run Code Online (Sandbox Code Playgroud)

你能帮助我吗?提前谢谢你 蒂诺

Ari*_*Ari 23

Azure DevOps Portal 中的内容经常更改。2022 年 2 月,我根据几个 Stackoverflow 帖子编写了自己的脚本,以删除旧的构建管道、它们的构建和租赁。

#Azure DevOps Personal Access Token
# https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=Windows

$personalAccessToken = "<Enter your personal access token here>"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
$header = @{authorization = "Basic $token"}

$organization = "<Enter your Azure DevOps Organization here>"
$project = "<Enter your Project Name here>"

$pipelineName = "<Enter the Build Pipeline Definition Name to be deleted here>"

#Get all build definitions
# API: GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=6.0
$url = "https://dev.azure.com/$organization/$project/_apis/build/definitions?api-version=6.0"
$allBuildDefinitions = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header

$allBuildDefinitions.value | Where-Object {$_.name -eq $pipelineName} | ForEach-Object {
    Write-Host $_.id $_.name $_.queueStatus
 
    # For debugging reasons, just to be sure that we don't delete the wrong build pipeline
    if ( $_.name -ne $pipelineName ) {
        return;
    }

    #Get all Builds for a Definition
    # API: GET https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitions={definitions}&queues={queues}&buildNumber={buildNumber}&minTime={minTime}&maxTime={maxTime}&requestedFor={requestedFor}&reasonFilter={reasonFilter}&statusFilter={statusFilter}&resultFilter={resultFilter}&tagFilters={tagFilters}&properties={properties}&$top={$top}&continuationToken={continuationToken}&maxBuildsPerDefinition={maxBuildsPerDefinition}&deletedFilter={deletedFilter}&queryOrder={queryOrder}&branchName={branchName}&buildIds={buildIds}&repositoryId={repositoryId}&repositoryType={repositoryType}&api-version=6.0
    $url = "https://dev.azure.com/$organization/$project/_apis/build/builds?definitions=" + $_.id + "&api-version=6.0"
    $allBuildsOfDefinition = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
 
    #Process each Build of Definition
    $allBuildsOfDefinition.value | Where-Object {$_.retainedByRelease -eq "True"} | Sort-Object id | ForEach-Object {
        #Report on retain status
        Write-Host "Build Id:" $_.id " retainedByRelease:" $_.retainedByRelease

        #Get all Retention Leases for this Build
        # API: GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/leases?api-version=7.1-preview.1
        $url = "https://dev.azure.com/$organization/$project/_apis/build/builds/" + $_.id + "/leases?api-version=7.1-preview.1"
        $allLeasesOfBuild = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header

        #Delete each Lease of Build
        $allLeasesOfBuild.value | ForEach-Object {
            #Delete Lease
            # API: DELETE https://dev.azure.com/{organization}/{project}/_apis/build/retention/leases?ids={ids}&api-version=7.1-preview.2
            $url = "https://dev.azure.com/$organization/$project/_apis/build/retention/leases?ids=" + $_.leaseId + "&api-version=7.1-preview.2"
            Invoke-RestMethod -Uri $url -Method Delete -ContentType "application/json" -Headers $header

            #Report on Lease deleted
            Write-Host "Lease Id:" $_.leaseId " deleted"
        }

        #Delete Build
        # API: DELETE https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=7.1-preview.7
        $url = "https://dev.azure.com/$organization/$project/_apis/build/builds/" + $_.id + "?api-version=7.1-preview.7"
        Invoke-RestMethod -Uri $url -Method Delete -ContentType "application/json" -Headers $header

        #Report on Build deleted
        Write-Host "Build Id:" $_.id " deleted"
    }

    #Delete the Build Definition
    # API: DELETE https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=6.0
    $url = "https://dev.azure.com/$organization/$project/_apis/build/definitions/" + $_.id + "?api-version=6.0"
    Invoke-RestMethod -Uri $url -Method Delete -ContentType "application/json" -Headers $header
    
    Write-Host "Build Definition:" $pipelineName " (" $_.id ") deleted"
}
    
Write-Host "Habe fertig!"
Run Code Online (Sandbox Code Playgroud)

  • 优秀作品。Azure DevOps 似乎有这样的习惯:弃用那些你无法返回并轻松更改的东西。在我们的例子中,它是不再存在且无法删除的策略,但阻止我删除旧的构建定义。这节省了我无数个小时手动删除管道运行的时间。非常感谢! (5认同)

use*_*131 2

与此同时,我在https://tenbulls.co.uk/2020/03/25/delete-old-build-definitions-in-azure-devops/上找到了来自未知人士的操作方法。

诀窍是使用retainedByRelease=false来修补retainedByRelease 值。

$url = "https://dev.azure.com/$organization/$project/_apis/build/builds/" + $_.id + "?api-version=6.0-preview.5"
 
Invoke-RestMethod -Uri $url -Method Patch -Body (ConvertTo-Json @{"retainedByRelease"="false"}) -ContentType "application/json" -Headers $header
Run Code Online (Sandbox Code Playgroud)

$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 | where {$_.retainedByRelease -eq "True"} | Sort-Object id|ForEach-Object {
        #report on retain status
        Write-Host " BuildId:" $_.id " retainedByRelease:" $_.retainedByRelease

        #api call for a build
        $url = "https://dev.azure.com/$organization/$project/_apis/build/builds/" + $_.id + "?api-version=6.0-preview.5"
 
        Invoke-RestMethod -Uri $url -Method Patch -Body (ConvertTo-Json @{"retainedByRelease"="false"}) -ContentType "application/json" -Headers $header
    }

    $url = "https://dev.azure.com/$organization/$project/_apis/build/definitions/" + $_.id + "?api-version=6.0-preview.1"
    Write-Host $url
    Invoke-RestMethod -Uri $url -Method Delete -ContentType "application/json" -Headers $header
    
    Write-Host " Pipeline:" $pipelineName " (" $_.id ") deleted"
}
    
Write-Host "done"
Run Code Online (Sandbox Code Playgroud)