Gradle bootBuildImage fails with 'No digest found' in GitLab CI/CD using docker:dind service

Viewed 1149

I want to build the docker image of a Spring Boot 2.4 service using the bootBuildImage task and push it to the GitLab Registry.

I have the following .gitlab-ci.yml file:

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

But I'm getting the following error and that is not helpful enough to dig further:

$ 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

If there is a better approach to achieve this with GitLab CI/CD, please let me know :)

Any help appreciated!

Thanks

1 Answers

After reading more carefully the GitLab CI documentation, found that I had to add these variables:

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

The full working .gitlab-ci.yml file:

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
Related