[英]Azure Container Registry - delete all images except 2
我想刪除 Azure 容器注冊表中除最后兩個之外的所有圖像。 我一直在尋找這樣做的腳本,但我只發現刪除早於 X 天的圖像。 這對於我的情況是不可能的,因為有些日子創建了很多圖像,而其他日子只創建了一個。
有人有什么想法嗎?
將 $skipLastTags 和 $registryName 的值修改為您的選擇並在 powershell 上運行此腳本。
注意:請確認您在本地系統上安裝了 az cli。
$registryName = 'registryName'
$doNotDeleteTags = ''
$skipLastTags = 4
$repoArray = (az acr repository list --name $registryName --output json | ConvertFrom-Json)
foreach ($repo in $repoArray)
{
$tagsArray = (az acr repository show-tags --name $registryName --repository $repo --orderby time_asc --output json | ConvertFrom-Json ) | Select-Object -SkipLast $skipLastTags
foreach($tag in $tagsArray)
{
if ($donotdeletetags -contains $tag)
{
Write-Output ("This tag is not deleted $tag")
}
else
{
az acr repository delete --name $registryName --image $repo":"$tag --yes
}
}
}
我現在無法測試它,但是這個小的 PowerShell 腳本應該可以工作:
$acrName = 'YourACRName'
$repo = az acr repository list --name $acrName
$repo | Convertfrom-json | Foreach-Object {
$imageName = $_
(az acr repository show-tags -n $acrName --repository $_ |
convertfrom-json )| Select-Object -SkipLast 2 | Foreach-Object {
az acr repository delete --yes -n $acrName --image "$imageName:$_"
}
}
它檢索每個存儲庫的所有標簽,跳過最后 2 個標簽,然后遍歷每個標簽並將其刪除。
請先在某種測試環境中進行測試。
如果你在bash
需要這個。
變量delete_from
是基於1-index 的,即如果您指定值1,則所有圖像都將被刪除。 值為 3 保留 2 個最新圖像。
#!/bin/bash -e
acr='your_acr'
repos=('repo1' 'repo2' 'repoN')
delete_from=3
for repo in "${repos[@]}"; do
tags_to_delete=$(echo $(az acr repository show-tags -n ${acr} --repository ${repo} --orderby time_desc --output tsv) | cut -d ' ' -f${delete_from}-)
for tag_to_delete in ${tags_to_delete}; do
az acr repository delete --yes -n ${acr} --image ${repo}:${tag_to_delete}
done
done
如果“最后兩個”是指最新的兩個,那么這應該可以解決問題:
az acr repository show-manifests --name your_acr --repository your_repo --orderby time_desc -o tsv --query '[].digest' | sed -n '3,$ p' | xargs -I% az acr repository delete --name your_acr --image your_repo@% --yes
您可以使用 build in acr purge 命令:
Powershell
$subscription = "your-subscription-id"
$registry = "your-registry"
$PURGE_CMD = "acr purge --filter 'acs-weather-api:.*' --keep 2 --ago 0d --untagged"
az acr run --cmd $PURGE_CMD --registry $registry --subscription $subscription /dev/null
Bash
SUBSCRIPTION="your-subscription-id"
REGISTRY="your-registry"
PURGE_CMD="acr purge --filter 'acs-weather-api:.*' --keep 2 --ago 0d --untagged"
az acr run --cmd "$PURGE_CMD" --registry "$REGISTRY" --subscription "$SUBSCRIPTION" /dev/null
基於@Christian Holm Jørgensen 的回答
現在您可以保存列表(save_list)。
#!/bin/bash -e
acr='MY_ACR_NAME'
repos=('MY_REPO') # 'repo2' 'repoN')
save_list="0.2.0-alpha.1 latest 1.0.0"
string_remove_pattern() {
echo "${1//$2}"
}
for repo in "${repos[@]}"; do
tags_available=$(echo $(az acr repository show-tags -n "${acr}" --repository "${repo}" --orderby time_desc --output tsv))
for to_save in $save_list; do
tags_available=$(string_remove_pattern "$tags_available" "$to_save")
done
tags_to_delete=$tags_available
echo -e "The follow image, from ACR $acr and repos $repos, will be deleted:\n$tags_to_delete"
read -rp "Is the list of image to delete correct? (Y/N)" answer
if [ "$answer" == "Y" ]; then
for tag_to_delete in ${tags_to_delete}; do
az acr repository delete --yes -n "${acr}" --image "${repo}":"${tag_to_delete}"
done
fi
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.