如何将 spring boot maven 插件构建映像与 skaffold 和 dekorate 一起使用?

How do I use spring boot maven plugin build-image with skaffold and dekorate?

我正在尝试使用 Skaffold、Dekorate 和 Spring Boot。

我找不到任何使用 Spring Boot 2.3+

的新构建包功能的示例
apiVersion: skaffold/v2beta9
kind: Config
metadata:
  name: tellus-upgrade
build:
  artifacts:
    - image: tellus-admin
      custom:
        buildCommand: ./mvnw -pl tellus-admin org.springframework.boot:spring-boot-maven-plugin:2.4.0:build-image -Dspring-boot.build-image.imageName=$IMAGE -Drevision=dev-SNAPSHOT -DskipTests=true
        dependencies:
          paths:
            - tellus-admin/src
            - tellus-admin/pom.xml
    - image: tellus-config-server
      custom:
        buildCommand: ./mvnw -pl tellus-config-server org.springframework.boot:spring-boot-maven-plugin:2.4.0:build-image -Dspring-boot.build-image.imageName=$IMAGE -Drevision=dev-SNAPSHOT -DskipTests=true
        dependencies:
          paths:
            - tellus-config-server/src
            - tellus-config-server/pom.xml
deploy:
  kubectl:
    manifests:
    - kubernetes/defaults.yml
    - kubernetes/db/kubernetes.yml
    - kubernetes/dev/dnsutils.yml
    - kubernetes/kafka-connect/kubernetes.yml
    - tellus-admin/target/classes/META-INF/dekorate/kubernetes.yml
    - tellus-config-server/target/classes/META-INF/dekorate/kubernetes.yml

当我 运行 skaffold dev 我得到错误: 由于第一次构建失败而退出开发模式:自定义脚本没有生成带有标签 [tellus-config-server:RELEASE_2020_2_0-226-g9be76a373-dirty]

的图像

但是从日志来看,映像似乎是构建的...

[INFO] Successfully built image 'docker.io/library/tellus-config-server:RELEASE_2020_2_0-226-g9be76a373-dirty'
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  17.004 s
[INFO] Finished at: 2020-11-15T22:31:59+11:00
[INFO] ------------------------------------------------------------------------
Building [tellus-admin]...
exiting dev mode because first build failed: the custom script didn't produce an image with tag [tellus-config-server:RELEASE_2020_2_0-226-g9be76a373-dirty]

spring-boot-maven-plugin:build-image 将图像加载到本地 Docker 守护程序中,但不会推送图像。我从未尝试过,但您也许可以使用 com.spotify:dockerfile-maven-plugin:push 目标。

更新:这是一个应该做正确事情的 Skaffold 自定义构建脚本:

#!/bin/sh
set -e
cd "$BUILD_CONTEXT"
mvn -pl "" -Drevision=dev-SNAPSHOT -DskipTests=true \
  org.springframework.boot:spring-boot-maven-plugin:build-image \
  -Dspring-boot.build-image.imageName="$IMAGE"
if [ "$PUSH_IMAGE" = true ]; then
    docker push "$IMAGE"
fi

您可以将其保存到文件 mvn-build-image.sh,然后修改您的 skaffold.yaml,例如:

artifacts:
- image: tellus-admin
  custom:
    buildCommand: ./mvn-build-image.sh tellus-admin 

您可能需要查看 Skaffold's Jib integration 以简化此过程。

如果问题是基于 Paketo/spring-boot-maven-plugin 只生成一个本地容器镜像 - 而不是推送它,因为 - then the Image Publishing ability of the spring-boot-maven-plugin 应该可以解决问题。因此,只需将以下内容附加到您的 mvn spring-boot:build-image 命令:

mvn spring-boot:build-image -Dspring-boot.build-image.publish=true

您也可以像这样明确配置图像名称:

mvn spring-boot:build-image -Dspring-boot.build-image.imageName=docker.example.com/library/tellus-config-server:latest -Dspring-boot.build-image.publish=true

As the docs state 您还可以在 pom.xml:

中配置图像发布的各个方面
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <name>docker.example.com/library/${project.artifactId}</name>
            <publish>true</publish>
        </image>
        <docker>
            <publishRegistry>
                <username>user</username>
                <password>secret</password>
                <url>https://docker.example.com/v1/</url>
                <email>user@example.com</email>
            </publishRegistry>
        </docker>
    </configuration>
</plugin>

有了该配置,您甚至不需要明确使用 -Dspring-boot.build-image.publish=true 参数,因为我们将 <image><publish> 配置为 true

因此无需使用 Jib 或自定义构建脚本。甚至还有 Cloud Native Buildpack support currently in beta for Skaffold - so that could be another option to have a look on (because the spring-boot-maven-plugin is also "only" an abstraction for the Cloud Native Buildpack / Paketo.io integration), if you'd like to switch to pack CLI.