Created
July 22, 2022 22:53
-
-
Save thepaulmacca/1abd09f38cc63f1dfccb95238e126b02 to your computer and use it in GitHub Desktop.
Azure Container Registry - Cleanup
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
# utils: | |
filter timestamp {"$(Get-Date -UFormat '%T') | $_"} | |
$acrName='' | |
$acrMaxImageTags=3 | |
if ($(az acr list --query "[?name=='$acrName']" | ConvertFrom-Json).Length -gt 0) { | |
echo "Pruning old image tags from: $acrName" | timestamp | |
$repositories = az acr repository list --name $acrName --output tsv | |
foreach ($repository in $repositories) { | |
echo "Pruning repository $repository" | timestamp | |
$tags = az acr repository show-tags --name $acrName --repository $repository --output tsv --orderby time_desc | where {$_ -ne 'latest'} | |
if ($tags.length -gt $acrMaxImageTags) { | |
echo "Repository tag count: $($tags.length); max tags: $acrMaxImageTags; pruning excess" | |
for ($i=$acrMaxImageTags; $i -lt $tags.length; $i++) { | |
$imageName = "$($repository):$($tags[$i])" | |
echo "Deleting: $imageName" | timestamp | |
az acr repository delete --name $acrName --image $imageName --yes --only-show-errors | |
} | |
} | |
} | |
echo "Done pruning ACR $acrName" | timestamp | |
} | |
# credits: | |
# https://github.com/andrew-kelleher/azurecontainerregistry-cleanup/blob/master/acr-cleanup.ps1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment