1

我有一个 ACR,它包含用于我的生产和开发环境的 docker 映像。由于每天都会推送新图像,因此我正在尝试设置保留策略。我的确切用例如下,

ACR 用法

根据图像,假设我在 ACR 中有 100 个图像,第 100 个图像被开发环境消耗。但是,生产运行第 40 张图像。话虽如此,我需要保留生产环境和开发环境的最新和最后两张图像。例如,我需要保留第 38、39 和 40 个图像以及第 98、99 和第 100 个图像。

我尝试使用acr purge. 不幸的是,我不能对我的用例使用保留策略或 acr purge(根据我的理解,也许我错了)。

谁能帮我解决这个问题?如果您需要更多信息或要求不明确,请告诉我!

4

1 回答 1

1

请检查以下使用 azure CLI 命令的脚本是否提供了工作思路:

它使用按标签删除的方法。为此,您需要在系统上安装 azure cli。

$registryName = 'registryName'
$doNotDeleteTags = ''
$skipLastTags = 3

$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
        }
 
    }
}

参考:Azure Container Registry - 删除除 2 之外的所有图像 - VoidCC

(或者)

az acr repository show-tags -n MyRegistry --repository MyRepository
 | ConvertFrom-String 
| %{$_.P2 -replace "[",]",""} 
| where {$_ -notin "skipthistag","andthistag" } 
| % {az acr repository delete --name MyRegistry --image MyRepository:$_ --yes}

参考:如何从 Azure Container Registry 中删除映像 - 代码日志


参考 - 跳过最后 x 个图像:

  1. 清理 (ACR) | 安德鲁·凯莱赫
  2. 用于删除旧 ACR 映像的 powershell 脚本 - Microsoft Q&A

其他参考:

  1. acr-cleanup:(github.com)
  2. 所以参考
  3. (ACR):标签、清单和清理 | 通过 Anant Vardhan | 中等的
于 2021-12-16T11:45:34.053 回答