在 CI 中使用来自 gitlab Registry 的图像

Bar*_*cki 7 gitlab docker

你能告诉我我是否以正确的方式做:

  • 我已经创建了 Docker 映像,其中包含在 gitlab CI 中运行测试所需的所有内容
  • 我把它推送到 gitlab 注册表
  • 我可以在 gitlab 页面的 Registry my image - gitlablogin/projectname部分看到
  • 我想将此图像用于 CI,因此在 .gitlab-ci.yml 中添加图像:gitlablogin/projectname

之前我有 .gitlab-ci.yml

same_task:
  stage: deploy
  image: python:3
  script:
    - python -V
Run Code Online (Sandbox Code Playgroud)

我现在有:

pep8:
  stage: deploy
  image: gitlablogin/projectname
  script:
    - python -V
Run Code Online (Sandbox Code Playgroud)

在此更改作业失败后:

Running with gitlab-runner 11.4.2 (cf91d5e1)
  on docker-auto-scale 72989761
Using Docker executor with image gitlablogin/projectname ...
Pulling docker image gitlablogin/projectname ...
ERROR: Job failed: Error response from daemon: pull access denied for gitlablogin/projectname, repository does not exist or may require 'docker login' (executor_docker.go:168:0s)
Run Code Online (Sandbox Code Playgroud)

我在 gitlab CI 和 gitlab 注册表的上下文中使用 docker 是否正确?我还想将我的 docker 文件保留在同一个 repo 上,并在 Dockerfile 中的相同内容发生变化时构建新映像,最好的方法是什么?

phi*_*phi 7

要从同一 gitlab 实例上的另一个项目访问 docker 镜像,唯一需要做的是允许您的项目在具有该镜像的项目上进行令牌访问。

不需要其他配置,例如 docker 登录。只需使用image: $CI_REGISTRY/other/project:tag

job-in-project1:
  stage: test
  image: $CI_REGISTRY/group-b/otherproject:latest
  script:
    - command to run with docker image
Run Code Online (Sandbox Code Playgroud)

配置:

  • 转到容器注册表中包含该映像的项目。例如group-b/otherproject
  • 打开设置 > CI/CD
  • 展开“令牌访问”
  • 允许以下项目中的 CI 作业令牌访问此项目中单击“添加项目”

允许从您的项目访问令牌

文档:https ://git.aviloo.com/help/ci/jobs/ci_job_token#configure-cicd-job-token-access


Mak*_*sov 6

现在可以使用 gitlab 注册表中的图像而无需任何特殊步骤。只需构建映像并将其推送到您的 gitlab 项目容器注册表

docker build -t registry.gitlab.com/gitlabProject/projectName:build .
docker push registry.gitlab.com/gitlabProject/projectName:build 
Run Code Online (Sandbox Code Playgroud)

然后只需在您的管道设置中指定此图像:

image: registry.gitlab.com/gitlabProject/projectName:build
Run Code Online (Sandbox Code Playgroud)

Gitlab 能够使用它的凭据来拉取这个镜像:

Preparing the "docker+machine" executor
00:46
 Using Docker executor with image registry.gitlab.com/gitlabProject/projectName:build ...
 Authenticating with credentials from job payload (GitLab Registry)
 Pulling docker image registry.gitlab.com/gitlabProject/projectName:build ...
 Using docker image sha256:e7e0f4f5fa8cff8a93b1f37ffd7dd0505946648246aa921dd457c06a1607304b for registry.gitlab.com/gitlabProject/projectName:build ...
Run Code Online (Sandbox Code Playgroud)

更多:https : //docs.gitlab.com/runner/configuration/advanced-configuration.html#using-a-private-container-registry

  • 还需要转到 **设置** -> **CI/CD** -> **令牌访问** 并为想要使用图像的项目添加访问权限。 (5认同)

Ser*_*giu 2

您的方法与您想要实现的目标相距不远。我相信你所缺少的是:

根据: https: //docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-an-image

为了使用为 CI 构建的映像,您首先需要将其作为服务添加到运行器的config.toml文件中。

完成后,您就可以使用该指令:image: my_image

但是,您还可以选择另一个选项:您可以登录到 Docker 注册表,拉取并运行 CI Docker 映像,然后您可以在将运行管道的位置执行。