我知道这可以用dockerhub
. 我想知道 gitlab 注册表是否有类似的东西。
用例是,我编写了一个结构脚本来将部署恢复为用户提供的特定标签。在实际拉入图像之前,我想知道注册表中是否存在具有指定标签的图像并相应地警告用户。
我在他们的文档中搜索过,但找不到任何东西。
注意:这里的用户是部署代码的人。
我知道这可以用dockerhub
. 我想知道 gitlab 注册表是否有类似的东西。
用例是,我编写了一个结构脚本来将部署恢复为用户提供的特定标签。在实际拉入图像之前,我想知道注册表中是否存在具有指定标签的图像并相应地警告用户。
我在他们的文档中搜索过,但找不到任何东西。
注意:这里的用户是部署代码的人。
Update: I added a solution that works without access to the docker server (non-privileged mode) below.
Ok, here is a solution I came up with using the docker:stable image by enabling the experimental client features.
mkdir -p ~/.docker
"echo '{\"experimental\": \"enabled\"}' > ~/.docker/config.json"
docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
docker manifest inspect $IMGNAME:$IMGTAG > /dev/null && exit || true
The exit
terminates the build script in case that tag already exists. Also you should be aware that ~/.docker/config.json
is overwritten. That is also why the login must happen afterwards.
Update: Instead of writing to the config one can also set the DOCKER_CLI_EXPERIMENTAL
environment variable to enabled
. Thus the first two lines can be replaced with export DOCKER_CLI_EXPERIMENTAL=enabled
Update: If you don't have privileged mode turned on and thus no access to the docker-daemon, you can use the registry-api scripts provided by harbor ( Note that they are python2.). This comes handy if you are building a docker image using kaniko, where no access to the docker-daemon is needed.
可以使用 Gitlab API。
tag=tag_name
image=image_name
private_token=gitlab_private_token
project=project_number
repo_id=$(curl --header "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$project/registry/repositories" | jq -c --arg regex ".*\\$image$" '.[] | select(.path | test($regex))'.id)
if [ $( curl --header "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$project/registry/repositories/$repo_id/tags/$tag" | jq -r '.name' ) == "$tag" ] ; then
echo "$tag exists"
else
echo "$tag does not exist"
fi
除非GitLab Container Registry支持curl dockerhub 所做的那种(带有v1/repositories/$1/tags/$2
),否则我怀疑它是否提供了该功能。
例如,问题 26866“GitLab 注册表可用图像列表/搜索”在 10 个月后仍然打开。
GitLab 12.2 更新(2019 年 4 月,18 个月后)
在完成实现之后,创建两个端点是有意义的:
GET /groups/:id/registry/repositories
- 返回组内所有项目的所有 Docker 容器存储库的列表,类似于GET /projects/:id/registry/repositories
和
GET /groups/:id/registry/repositories/tags
- 返回组内所有项目的所有 Docker 容器存储库列表,包括每个容器存储库的所有标记。响应将如下所示:
所以这可以帮助检查是否image:tag
存在。
使用搜索快速查找和发现托管在 GitLab 容器注册表中的图像
当您或您团队中的某个人将映像发布到 GitLab 容器注册表时,您需要一种快速找到它并确保正确构建映像的方法。
如果您使用 GitLab CI/CD 发布每个构建的图像,则很难在当前用户界面中有效地找到图像。相反,您依赖的是命令行或 API。我们很高兴地宣布,在 13.0 中,我们为 GitLab Container Registry 添加了搜索功能。
只需导航到您的项目或组的注册表并输入图像名称即可查看所有图像的列表。
另请参阅GitLab 14.7(2022 年 1 月)
在 Container Registry 浏览器中对 Docker 标记进行排序
您现在可以按名称对 Container Registry 标记详细信息页面中的标记列表进行排序。
以前,没有排序功能。这有时需要您滚动浏览许多页面才能找到特定标签。
默认情况下,标签列表现在按名称升序排序。您也可以将排序顺序更改为降序。
请参阅此问题以跟踪有关标签排序的任何进一步工作。
因此,除了@fparaggio 答案之外,我还在寻找当前分支的图像是否存在,如果存在则使用分支图像,否则使用最新标签作为基础图像
package:
stage: package
image:
name: registry.gitlab.com/org/hak:kaniko-debug
entrypoint: [""]
retry:
max: 2
tags:
- kubernetes
interruptible: true
script:
- if [[ $( curl --insecure --header "PRIVATE-TOKEN:$GITLAB_TOKEN" https://gitlab.com/api/v4/projects/xxx/registry/repositories/xxx/tags/$CI_COMMIT_REF_SLUG | jq -r '.name' ) == "$CI_COMMIT_REF_SLUG" ]] ; then
echo "$CI_COMMIT_REF_SLUG exists";
export CODE_VERSION=$CI_COMMIT_REF_SLUG;
else
echo "tag for the branch $CI_COMMIT_REF_SLUG => $CI_COMMIT_REF_NAME does not exist, using latest";
export CODE_VERSION="latest";
fi
然后CODE_VERSION
作为 docker build args传递
- /kaniko/executor
--build-arg CACHE_IMAGE=$CI_REGISTRY_IMAGE/install
--build-arg CODE_VERSION=$CODE_VERSION
--dockerfile $CI_PROJECT_DIR/Dockerfile-release
--destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
您可以在 Gitlab UI 中找到注册表 ID,无需进行两次 API 调用。
https://gitlab.com/org/xyz/repository/container_registry/xxx