在 spring-boot:build-image

问题描述

我想集成 Spring Boot Maven 插件功能来构建 OCI 映像并将其发布到远程存储库

我的目标

我想使用以下插件配置:

<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>

现在我想通过命令行传递 docker.publishRegistry 变量。

到目前为止我所尝试的

我尝试使用 -Ddocker.publishRegistry.username 属性传递参数,但没有奏效。

当您查看插件的源代码时,Docker 没有分配给它的 Parameter 属性

/**
 * 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

所以我猜是不可能通过命令行定义这个参数的吗?

当前的解决方法

目前我正在通过全局 maven 属性定义属性并在 docker 范围内重用它们。 我的 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>

我正在构建:

./mvnw -B -s \
  -Dspring-boot.build-image.publish=true \
  -Ddocker-registry-username="$USERNAME" \
  -Ddocker-registry-password="$PASSWORD" \
  -Ddocker-registry="$REGISTRY" \
  clean deploy

解决方法

对于您的问题,我没有确切的解决方案:“在命令行上传递 publishRegistry 参数”,但是如果可以的话,我还有另一种解决方法可以防止您在 pom.xml 中暴露您的凭据。

我所做的是将参数和凭据放在我的 .m2/settings.xml 中的配置文件中,如下所示:

    <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>

然后在命令行上,您可以简单地传递配置文件的名称以将凭据合并到当前构建中。

mvn clean install -Pdocker-io-credentials