Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Maven Plugin > 2.4.x build image publish on GitLab registry

I'm currently developing a GitLab CI/CD pipeline which compiles, tests and builds a standard Spring Boot application.

I want to package it in a docker image and publish that to the GitLab registry to use it later on.

Spring Boot recently added the build-image goal to its maven plugin which also has the ability to publish the image to a registry.

My problem is, that I can't get the auth to work.

I'm using a maven:3.6.3-jdk-11-slim image for the job with the docker:dind service to have access to a docker daemon. Building the image runs fine, but publishing fails. I configured the maven plugin in the project pom to use properties for auth, which will be overwritten by the CLI in my CI/CD Job as follows:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <docker>
            <publishRegistry>
                <username>${CI_REGISTRY_USER}</username>
                <password>${CI_REGISTRY_PASSWORD}</password>
                <url>${CI_REGISTRY}</url>
            </publishRegistry>
        </docker>
    </configuration>
</plugin>

Properties defined in the POM with no value (Will be filled in by CLI call) :

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>11</java.version>
    <CI_REGISTRY/>
    <CI_REGISTRY_USER/>
    <CI_REGISTRY_PASSWORD/>
</properties>

My maven CLI call in the Pipeline/Job uses the GitLab registry variables :

docker image job:
  stage: Build
  image: maven:3.6.3-jdk-11-slim
  services:
    - docker:dind
  script:
    - echo "java.runtime.version=11" > system.properties
    - mvn spring-boot:build-image -DCI_REGISTRY=$CI_REGISTRY -DCI_REGISTRY_USER=$CI_REGISTRY_USER -DCI_REGISTRY_PASSWORD=$CI_REGISTRY_PASSWORD -Dspring-boot.build-image.imageName=SpringBootImage_${CI_JOB_ID} -Dspring-boot.build-image.publish=true

I was following the instructions via GitLab and Spring Boot documentation, but cant seem to identify my problem.

GitLab Registry Auth documentation Spring Boot Maven Plugin image publishing documentation

like image 216
Mattizin Avatar asked Nov 01 '25 18:11

Mattizin


1 Answers

I know it's been a while but in case others are trying to accomplish this

This works well

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>                   
    <mainClass>com.foo.Main</mainClass>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>build-image</goal>
      </goals>
      <phase>deploy</phase>
      <configuration>
        <image>
          <name>${env.CI_REGISTRY_IMAGE}/${project.artifactId}:${project.version} </name>
          <publish>true</publish>
        </image>
        <docker>
          <publishRegistry>
             <username>${env.CI_REGISTRY_USER}</username>
             <password>${env.CI_REGISTRY_PASSWORD}</password>
          </publishRegistry>
        </docker>
      </configuration>
    </execution>
  </executions>
</plugin>

It uses gitlab-ci built-in env vars to configure everything

like image 56
Hilikus Avatar answered Nov 04 '25 08:11

Hilikus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!