是否有VSTS构建任务来从注册表中删除未使用的Docker容器? [英] Is there a VSTS build task to remove unused Docker containers from the registry?
问题描述
我在Azure上有一个Docker容器注册表,其中一个容器包含多个标记的版本.我想删除未使用的标签,因为它们会占用很多空间,并且可以随时进行重新制作.
I have a Docker Container registry on Azure with one container holding multiple tagged versions. I'd like to remove the unused tags as they're taking up a lot of space and can be remade at any time.
该Docker容器注册表存在的唯一原因是因为VSTS如何处理Docker容器的构建和发布.运行VSTS生成时,它使用新标签将容器存储在Azure上的注册表中.并非所有版本都会发布,因此可以安全地删除这些标签.
The only reason this Docker container registry exists is because of how VSTS handles builds and releases of Docker containers. When running a VSTS build, it stores the container in the registry on Azure with a new tag. Not all builds will be released so those tags can be safely removed.
VSTS非常干净地管理构建,如果30天后不使用它们,则将其删除(默认情况下);但是,一旦推送到Docker容器注册表,即使没有匹配的VSTS版本,它们也会无限期地停留在那里.
VSTS manages builds very cleanly and removes them if they're not in use after 30 days (by default); although, once pushed to the Docker container registry, they will stay there indefinitely even without a matching VSTS build.
我将如何创建一个任务,以从注册表中删除不再与VSTS中的构建相关联的Docker容器?
How would I create a task to remove Docker containers from the registry that are no longer associated with a build in VSTS?
注意::容器标签与VSTS的buildId
s
NOTE: The container tags match VSTS's buildId
s
推荐答案
由于没有VSTS任务可以完成此任务,因此您需要执行一些步骤来生成数据的JSON文件,然后处理该数据并运行一些cmd通过Azure CLI执行命令.
As there's not a VSTS task to accomplish this task, you need to do a few steps to generate some JSON files of data then process that data and run some cmd commands through the Azure CLI.
您需要从VSTS获取构建定义ID.链接看起来像这样:
You'll need to get the build definition id from VSTS. The link looks something like this:
https://example.visualstudio.com/CP/Example%20Team/_build/index?context=mine&path=%5C&definitionId=11&_a=completed
在这种情况下,定义ID为11
:definitionId=11
.
In this case, the defintion id is 11
: definitionId=11
.
一旦有了定义ID,就可以使用该URL查询API并获取仅活动版本的列表.此链接不包括已删除的内部版本:
Once you have the definition id, use this URL to query the API and get a list of only active builds. This link does not include deleted builds:
https://companionprotect.visualstudio.com/CP/_apis/build/builds?deletedFilter=exclude&definitions=BUILD_DEFINITION_ID
标签
az login
az acr login --name {REGISTRY_NAME}
az acr repository show-tags --repository {CONTAINER_NAME} --output json > tags.json
删除未使用的Docker容器标签
处理完数据后,您可以创建运行以下命令的批处理文件:
Remove Unused Docker Container Tags
After processing the data, you can create a batch file running these commands:
az acr repository delete --yes --repository companionconnectclient_web --tag {TAG_NAME}
如果要在cmd
文件中运行多个命令,请将& ^
放在每个命令之间,以使每个命令完成后继续执行,否则只会执行第一个命令.您也可以在换行符末尾使用& ^
.
If you want to run multiple commands in a cmd
file, put & ^
between each command to have it continue after each one completes otherwise it will only do the first one. You can use & ^
at the end of newlines as well.
这篇关于是否有VSTS构建任务来从注册表中删除未使用的Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!