0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Azure DevOps Server 2020で、暫定処理として、古い自動ビルドを削除する

Posted at

概要

Azure DevOps Server 2020のあるバージョンには設定した保持ルールで自動ビルドが削除されないという不具合がある(2022年5月17日に改善されている

PR時などに自動CIを行っている場合、バックアップ容量などの増加を招くことがある。

何らかの理由で最新バージョン、またはパッチ適用できないが、保持ルールに適合していないものを削除したい場合に、次のPowerShellスクリプトを利用する。

事前準備

個人用アクセストークンを取得する

  • 「ビルド(読み取りおよび実行)」権限が必要

スクリプトを実行

概要

  • 指定プロジェクトの指定パイプラインで、保護されていない、かつ20日以前のものを削除

スクリプト内の設定

  • $personalAccessTokenに前項で取得したトークンを指定
  • <ProjectName>にAzure DevOps Serverのプロジェクト名を指定
  • <AzureDevOpsServerName>にAzure DevOps ServerのURLを指定(例:http://sampleserver:8080/tfs/
  • 必要に応じ$collection$pipelineNameを指定

スクリプト内の設定

#Azure DevOps Personal Access Token
$personalAccessToken = "<AccessToken>"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
$header = @{authorization = "Basic $token" }

$collection = "DefaultCollection"
$project = "<ProjectName>"
$urlbase = "<AzureDevOpsServerName>"

$pipelineName = "<ProjectName> CI"

#Get all build definitions
# API: GET https://{instance}/{collection}/{project}/_apis/build/builds?api-version=6.0
$url = "$urlbase/$collection/$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://{instance}/{collection}/{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

    $maxTime=(Get-Date).AddDays(-20).ToString("yyyy-MM-dd")
    Write-Host "maxTime:" $maxTime

    $url = "$urlbase/$collection/$project/_apis/build/builds?definitions=" + $_.id + "&maxTime="  + $maxTime + "&deletedFilter=excludeDeleted&api-version=6.0"
    $allBuilds = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
    #Process each Build of Definition
    $allBuilds.value | Where-Object { $_.keepForever -eq $false }  | Sort-Object id | ForEach-Object {
        #Report on retain status
        Write-Host "Build Id:" $_.id " keepForever: " $_.keepForever " reason: " $_.reason " sourceBranch: " $_.sourceBranch " finishTime: " $_.finishTime

        #Delete Build
        # API: DELETE https://{instance}/{collection}/{project}/_apis/build/builds/{buildId}?api-version=6.0
        $url = "$urlbase/$collection/$project/_apis/build/builds/" + $_.id + "?api-version=6.0"
        Invoke-RestMethod -Uri $url -Method Delete -ContentType "application/json" -Headers $header

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

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?