Last active
September 23, 2020 06:48
-
-
Save MyKEms/94d7d85a1d1398654e54f1862d6cdd19 to your computer and use it in GitHub Desktop.
Artifactory docker cleanup script (max unique tags + exclude tags)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script will keep defined number of max unique tags, exclude specific tags and remove the rest. | |
# Compatible with PowerShell Core 6+ | |
# Author: https://github.com/MyKEms | |
#Settings | |
[string]$userName = "ArtifactoryUsername" | |
[string]$userPassword = "ArtifactoryUserPassword/Token" | |
$max_unique_tags = 17 | |
$exclude_tags = "latest|mater" | |
$repository = "docker-repository" | |
$base_url = "https://some-instance.jfrog.io/artifactory/" | |
$DryRun = $true #Set to false to remove images | |
#Make a list of docker images with extra props based on settings | |
$body = @" | |
items.find({ | |
"name":{ | |
"`$eq":"manifest.json" | |
}, | |
"repo":{ | |
"`$eq":"$repository" | |
} | |
}).include("modified_by","original_sha1","id","size","updated","original_md5","repo","property.value","actual_md5","created_by","actual_sha1","property.key","sha256","type","path","depth","modified","name","created") | |
"@ | |
[SecureString]$secureString = $userPassword | ConvertTo-SecureString -AsPlainText -Force | |
[PSCredential]$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $secureString | |
$resp = Invoke-RestMethod -Method Post -Uri "$($base_url)api/search/aql" -ContentType "text/plain" -Body $body -Authentication Basic -Credential $cred | |
$images_arr = @() | |
foreach ( $item in $resp.results ) { | |
$artifact_url = $base_url+$item.repo+'/'+$item.path | |
$item | Add-Member -Type NoteProperty -Name "artifact_url" -Value $artifact_url | |
$item | Add-Member -Type NoteProperty -Name "docker_image" -Value $($item.path | Select-String "((?!\/).*\/)").Matches.Value | |
$item | Add-Member -Type NoteProperty -Name "docker_tag" -Value $($item.path | Select-String "\/(?:.(?!\/))+$").Matches.Value | |
$images_arr += $item | |
} | |
#Skip defined max unique tags and list the rest | |
$docker_image_unique = $images_arr.docker_image | Sort-Object | Get-Unique | |
$to_remove = @() | |
foreach ($item in $docker_image_unique) { | |
$match = $images_arr | Where-Object {$item -Contains $_.docker_image} | |
$to_remove += $match | Sort-Object -Property updated -Descending | Select-Object -Skip $max_unique_tags | |
} | |
#Exclude tags form remove list | |
$to_remove = $to_remove | Where-Object { $_.docker_tag -notmatch $exclude_tags } | |
#Remove images from the Artifactory | |
foreach ( $item in $to_remove.artifact_url ) { | |
if ($DryRun) { | |
Write-Output "[DRYRUN CLEANUP] Removing image: $item" | |
} | |
else { | |
Write-Output "[CLEANUP] Removing image: $item" | |
Invoke-RestMethod -Method Delete -Uri $item -Credential $cred -Authentication Basic | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment