使用 Jib 和 Gitlab-CI 构建 docker 镜像

L. *_*per 3 gitlab docker gitlab-ci jib

我正在尝试创建一个管道,其中使用 JIB(通过 Maven 插件)创建 docker 映像并将其推送到我的 Gitlab 注册表。

当我登录到我的 docker 注册表时,这在本地工作得很好。

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
        <allowInsecureRegistries>true</allowInsecureRegistries>
        <from>
            <image>dockerhost/projectgroup/alpine</image>
        </from>
        <to>
            <image>dockerhost/project/imagename:${project.version}</image>
        </to>
        <container>
            <useCurrentTimestamp>true</useCurrentTimestamp>
        </container>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

假设我有一个 .gitlab-ci.yml ,如下所示:

stages:
  - build_image

build_image:
  stage: build_image
tags:
  - dev
script: |
  mvn compile jib:build
Run Code Online (Sandbox Code Playgroud)

现在,当管道被触发时,我遇到了异常

Build image failed: Failed to authenticate with registry dockerhost/projectgroup/alpine because: peer not authenticated
Run Code Online (Sandbox Code Playgroud)

我假设我收到此错误是因为我尚未运行 docker login -u [用户名] -p [密码/令牌]

我如何需要一个使用 docker-in-docker 映像的 .gitlab-ci.yml 才能在我的脚本中运行 docker 登录?

除了使用 docker-in-docker 映像在我的 Gitlab CI 上构建此映像之外,还有其他选择吗?

Rap*_*ael 9

从 GitLab 9.0 开始,默认情况下所有必需的变量都可用

  • CI_REGISTRY-- 项目注册中心的 URL
  • CI_REGISTRY_IMAGE-- 项目注册表的“base”镜像名称
  • CI_REGISTRY_USER-- 可以访问项目注册表的技术用户
  • CI_REGISTRY_PASSWORD-- 该用户的密码/令牌

因此,您可以使用以下命令构建并发布图像:

mvn compile jib:build \
    -Djib.to.auth.username=${CI_REGISTRY_USER} \
    -Djib.to.auth.password=${CI_REGISTRY_PASSWORD} \
    -Djib.to.image=${CI_REGISTRY_IMAGE}:latest
Run Code Online (Sandbox Code Playgroud)

您还可以使用Maven 设置,就像您通常在 Maven 注册表上进行身份验证一样。