Pass dockerPublisher maven properties over command line in spring-boot:build-image
Asked Answered
S

3

7

I want to integrate the spring boot maven plugins capability to build and publish an OCI Image to a remote Repository

My Goal

I want to use the following plugin configuration:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <image>
      <name>${docker.image.prefix}/${project.artifactId}:${project.version}</name>
    </image>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>build-image</goal>
      </goals>
    </execution>
  </executions>
</plugin>

And now I want to pass the docker.publishRegistry variables by command line.

What I've tried so far

I've tried to pass the parameter with the -Ddocker.publishRegistry.username property but that didn't work.

When you take a look at the source code of the plugin Docker has no Parameter property assigned to it:

/**
 * Alias for {@link Image#publish} to support configuration via command-line property.
 */
@Parameter(property = "spring-boot.build-image.publish", readonly = true)
Boolean publish;

/**
 * Docker configuration options.
 * @since 2.4.0
 */
@Parameter
private Docker docker;

https://github.com/spring-projects/spring-boot/blob/82b90d57496ba85be316b9eb88a36d81f2cc9baa/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java#L159

So I guess it is not possible to define this parameter by command line or is it?

Current Workaround

Currently I'm defining the properties by global maven properties and reuse them in the docker scope. My pom.xml:

<properties>
  <docker-registry>https://example.org</docker-registry>
  <docker-registry-username>username</docker-registry-username>
  <docker-registry-username>password</docker-registry-username>
</properties>
<!-- ... -->
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <image>
      <name>${docker.image.prefix}/${project.artifactId}:${project.version}</name>
    </image>
    <docker>
      <publishRegistry>
        <username>${docker-registry-username}</username>
        <password>${docker-registry-password}</password>
        <url>${docker-registry}</url>
      </publishRegistry>
    </docker>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>build-image</goal>
      </goals>
    </execution>
  </executions>
</plugin>

And I'm building with:

./mvnw -B -s \
  -Dspring-boot.build-image.publish=true \
  -Ddocker-registry-username="$USERNAME" \
  -Ddocker-registry-password="$PASSWORD" \
  -Ddocker-registry="$REGISTRY" \
  clean deploy
Subinfeudation answered 29/1, 2021 at 16:4 Comment(1)
I am facing the exact same problem!Conventional
C
2

I have not the exact solution to you question: "passing publishRegistry parameters on the command line", but If I may, I have another workaround that shields you from exposing your credential in the pom.xml.

What i have done is to put the parameters and credential in a profile in my .m2/settings.xml like this:

    <profiles>
        <profile>
            <id>docker-io-credentials</id>
            <properties>
                <docker-reg>docker.io</docker-reg>
                <docker-reg.user>your-user-name</docker-reg.user>
                <docker-reg.pwd>your-token-or-passwd</docker-reg.pwd>
                <docker-reg.url>${docker-reg}/library/${docker-reg.user}</docker-reg.url>
            </properties>
        </profile>
    </profiles>

Then on the command-line you can simply pass the profile's name to merge the credential to the current build.

mvn clean install -Pdocker-io-credentials
Crack answered 14/7, 2021 at 22:30 Comment(0)
I
1

You can define placeholders in the spring-boot plugin's configuration, which refer to environment variables. This will be slightly less complex, so like

...
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>main.Class</mainClass>
                <image>
                    <name>registry-url/library/image-name:${project.version}</name>
                </image>
                
                <docker>
                    <publishRegistry>
                        <username>docker-user</username>
                        <password>${env.docker_registry_password}</password>
                        <url>https://registry-url/v1/</url>
                        <email>[email protected]</email>
                    </publishRegistry>
                </docker>
            </configuration>
...

See more on this topic here: https://www.baeldung.com/maven-env-variables

Insulate answered 4/3, 2022 at 15:45 Comment(0)
R
0

Just to mention that the Spring team is aware and did not consider this a bug but rather a documentation issue: https://github.com/spring-projects/spring-boot/issues/31024#issuecomment-1127905504

Very similar to what @twobiers suggested in his workaround:

<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
    <docker>
        <publishRegistry>
            <url>${docker.publishRegistry.url}</url>
            <username>${docker.publishRegistry.username}</username>
            <password>${docker.publishRegistry.password}</password>
        </publishRegistry>
    </docker>
</configuration>

and then I build (and publish to Github Packages Registry) my project with:

./mvnw spring-boot:build-image \
  -Ddocker.publishRegistry.username=${{ github.actor }} \
  -Ddocker.publishRegistry.password=${{ secrets.GITHUB_TOKEN }} \
  -Ddocker.publishRegistry.url=ghcr.io \
  -Dspring-boot.build-image.publish=true \
  -Dspring-boot.build-image.imageName="ghcr.io/${{ github.repository }}:latest" \
  -DskipTests
Recency answered 5/2, 2023 at 0:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.