Azure 容器注册表 - 删除除 2 之外的所有映像

exi*_*sta 5 azure azure-container-registry

我想删除 Azure 容器注册表中除最后两个之外的所有映像。我正在寻找一个脚本来执行此操作,但我发现只能删除 X 天之前的图像。这对于我的情况来说是不可能的,因为有时会创建很多图像,而有时则只创建一张图像。

有人有什么想法吗?

小智 11

将 $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
        }
 
    }
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ndl 1

我现在无法测试它,但这个小 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:$_"
        }
}
Run Code Online (Sandbox Code Playgroud)

它检索每个存储库的所有标签,跳过最后 2 个标签,然后迭代每个标签并将其删除。

请先在某种测试环境中进行测试。