Gradle bootBuildImage 使用 docker:dind 服务在 GitLab CI/CD 中失败并显示“未找到摘要”

Posted

技术标签:

【中文标题】Gradle bootBuildImage 使用 docker:dind 服务在 GitLab CI/CD 中失败并显示“未找到摘要”【英文标题】:Gradle bootBuildImage fails with 'No digest found' in GitLab CI/CD using docker:dind service 【发布时间】:2021-05-23 09:28:35 【问题描述】:

我想使用bootBuildImage 任务构建 Spring Boot 2.4 服务的 docker 映像并将其推送到 GitLab 注册表。

我有以下.gitlab-ci.yml 文件:

stages:
  - build
  - deploy

build:
  stage: build
  rules:
    - if: $CI_MERGE_REQUEST_ID
  image: azul/zulu-openjdk:15.0.2
  script:
    - export GRADLE_OPTS="-Dorg.gradle.daemon=false -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false"
    - ./gradlew clean build

docker-build:
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: "/certs"
  stage: build
  image: azul/zulu-openjdk:15.0.2
  services:
    - docker:dind
  script:
    - apt-get update
    - apt-get install -y apt-transport-https ca-certificates curl software-properties-common
    - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add
    - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    - apt-get update -y
    - apt-get install -y docker-ce
    - service docker start
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - ./gradlew bootBuildImage --imageName="$CI_REGISTRY_IMAGE"
    - docker push "$CI_REGISTRY_IMAGE"
  only:
    - develop

但我收到以下错误,这不足以进一步挖掘:

$ service docker start
 * Starting Docker: docker
   ...done.
$ docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
$ ./gradlew bootBuildImage --imageName="$CI_REGISTRY_IMAGE"
Downloading https://services.gradle.org/distributions/gradle-6.7.1-bin.zip
.........10%..........20%..........30%..........40%..........50%.........60%..........70%..........80%..........90%..........100%
Welcome to Gradle 6.7.1!
Here are the highlights of this release:
 - File system watching is ready for production use
 - Declare the version of Java your build requires
 - Java 15 support
For more details see https://docs.gradle.org/6.7.1/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :compileJava
> Task :processResources
> Task :classes
> Task :bootJarMainClassName
> Task :bootJar
> Task :bootBuildImage
Building image 'registry.gitlab.com/company/project/web-api:latest'
 > Pulling builder image 'docker.io/paketobuildpacks/builder:base' ..................................................
> Task :bootBuildImage FAILED
5 actionable tasks: 5 executed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bootBuildImage'.
> No digest found
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 4m 32s

如果有更好的方法来使用 GitLab CI/CD 实现这一点,请告诉我 :)

任何帮助表示赞赏!

谢谢

【问题讨论】:

【参考方案1】:

仔细阅读GitLab CI文档后,发现我不得不添加这些变量:

DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"

完整的工作 .gitlab-ci.yml 文件:

stages:
  - build
  - deploy

build:
  stage: build
  rules:
    - if: $CI_MERGE_REQUEST_ID
  image: azul/zulu-openjdk:15.0.2
  script:
    - export GRADLE_OPTS="-Dorg.gradle.daemon=false -Dorg.gradle.internal.launcher.welcomeMessageEnabled=false"
    - ./gradlew clean build

docker-build:
  variables:
    DOCKER_HOST: tcp://docker:2376
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: "/certs"
    DOCKER_TLS_VERIFY: 1
    DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
  stage: build
  image: azul/zulu-openjdk:15.0.2
  services:
    - docker:dind
  script:
    - apt-get update
    - apt-get install -y apt-transport-https ca-certificates curl software-properties-common
    - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add
    - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    - apt-get update -y
    - apt-get install -y docker-ce
    - service docker start
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - ./gradlew bootBuildImage --imageName="$CI_REGISTRY_IMAGE"
    - docker push "$CI_REGISTRY_IMAGE"
  only:
    - develop

【讨论】:

以上是关于Gradle bootBuildImage 使用 docker:dind 服务在 GitLab CI/CD 中失败并显示“未找到摘要”的主要内容,如果未能解决你的问题,请参考以下文章

用于执行 gradle bootBuildImage 命令的 Docker 映像

Gradle bootBuildImage 使用 docker:dind 服务在 GitLab CI/CD 中失败并显示“未找到摘要”

使用 gradle bootBuildImage 构建的 SpringBoot Docker 映像无法在 <1024 的任何端口上启动

Gradle的使用——快速找到自己想要在gradle中使用的jar包

Android 组件化使用 Gradle 实现组件化 ( Gradle 变量定义与使用 )

Android studio使用之[gradle的使用]