带有私有仓库的 Spring Boot Gradle bootBuildImage 任务

Posted

技术标签:

【中文标题】带有私有仓库的 Spring Boot Gradle bootBuildImage 任务【英文标题】:Spring Boot Gradle bootBuildImage task with private repo 【发布时间】:2021-03-15 00:40:55 【问题描述】:

我正在尝试设置一个 Spring Boot / Gradle 项目以使用 bootBuildImage 来构建我的 Docker 映像,但遇到了障碍。我们在构建服务器上的外部网络访问权限有限;我们使用一个私有的 nexus 存储库。

当我尝试运行 bootBuildImage 任务时,它正在尝试从 repo.spring.io 请求文件:

[creator]     unable to invoke layer creator
[creator]     unable to contribute spring-cloud-bindings layer
[creator]     unable to get dependency spring-cloud-bindings
[creator]     unable to download https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar
[creator]     unable to request https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar

我在我们的 nexus 服务器上添加了 repo.spring.io 作为存储库,但我不清楚如何让 Spring Boot Gradle 插件使用它。我尝试将它添加到我的 repositories 块中,但它似乎没有效果。

repositories 
    // Other repositories...
    //
    
    maven 
        url "<my nexus server>/repository/spring-io-releases/"
    

我错过了什么?

【问题讨论】:

有一个相关的issue on GitHub 【参考方案1】:

我遇到了类似的问题,我需要 configure the download uri of the bellsoft-liberica JDK used inside the Spring Boot build image goal/task 不使用 github.com - 而是使用我自己的私人服务器。单独使用buildpack environment variable是做不到的,但是你可以use bindings for that!

请注意,Spring Boot Gradle Plugin 或 Spring Boot Maven Plugin 需要 Spring Boot 2.5+(bindings 选项已在 2.5 中添加)。如果您使用的是较旧的 Spring Boot 版本,则需要升级或切换到 pack CLI。

绑定可以配置either through volumes or Kubernetes secrets。我创建了a fully comprehensible guide on how to use bindings in order to change a uri used inside a buildpack - 但我将概述切换 spring-cloud-bindings-x.y.z.jar uri 的关键步骤:

1.创建绑定目录

为了将绑定配置移交给pack CLI,我们需要首先创建一个目录

mkdir spring-cloud-config && cd spring-cloud-config

2。创建文件类型,包含绑定键

现在我们需要在这个目录中创建一个名为type 的文件,其中包含the spring-boot buildpack binding type dependency-mapping 的绑定键:

echo "dependency-mapping" >> type

3.创建名为 sha256 的文件,其中包含 spring-cloud-bindings-x.y.z.jar uri

现在我们应该根据buildpack.toml 内[[metadata.dependencies]] 部分的sha256 摘要值精确命名另一个文件

[[metadata.dependencies]]
id      = "spring-cloud-bindings"
name    = "Spring Cloud Bindings"
version = "1.7.0"
uri     = "https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar"
sha256  = "e3c18bf1a3c2e52743f9ff2fa46af59e5eee0a7f0683ff562eb35aa866e4a9e9"
stacks  = [ "io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.cflinuxfs3" ]

此文件必须包含您的内部链接的 uri,包括。 spring-cloud-bindings.jar

echo "http://<my nexus server>/repository/spring-io-releases/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar" >> e3c18bf1a3c2e52743f9ff2fa46af59e5eee0a7f0683ff562eb35aa866e4a9e9

4.使用 --volume 执行 pack CLI 以使用绑定

最后我们可以发出pack CLI 命令。确保pack CLI is installed on your system:

pack build your-application-name-here \
    --path . \
    --volume $(pwd)/spring-cloud-config:/platform/bindings/spring-cloud-config \
    --builder paketobuildpacks/builder:base

或者,您可以将 bindings 选项与 Spring Boot 2.5+ Maven 或 Gradle 插件一起使用,请参阅上面的链接。

现在 spring-boot buildpack 将从 http://&lt;my nexus server&gt;/repository/spring-io-releases/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar 下载 spring-cloud-bindings-1.7.0.jar 而不是 https://repo.spring.io/release

【讨论】:

但是直到两周前我才使用 Spring 2.4.2 版本生成图像,现在它开始出现此错误。你知道为什么吗? 贵公司网络中的新防火墙规则?【参考方案2】:

我现在在 spring-cloud-bindings-1.7.1 上开始收到此错误,但过去它有效:

...
[INFO]     [creator]     Paketo Spring Boot Buildpack 4.2.0
[INFO]     [creator]       https://github.com/paketo-buildpacks/spring-boot
[INFO]     [creator]       Creating slices from layers index
[INFO]     [creator]         dependencies
[INFO]     [creator]         spring-boot-loader
[INFO]     [creator]         snapshot-dependencies
[INFO]     [creator]         application
[INFO]     [creator]       Launch Helper: Contributing to layer
[INFO]     [creator]         Creating /layers/paketo-buildpacks_spring-boot/helper/exec.d/spring-cloud-bindings
[INFO]     [creator]       Spring Cloud Bindings 1.7.1: Contributing to layer
[INFO]     [creator]         Downloading from https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.1/spring-cloud-bindings-1.7.1.jar
[INFO]     [creator]     unable to invoke layer creator
[INFO]     [creator]     unable to contribute spring-cloud-bindings layer
[INFO]     [creator]     unable to get dependency spring-cloud-bindings
[INFO]     [creator]     unable to download https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.1/spring-cloud-bindings-1.7.1.jar
[INFO]     [creator]     unable to request https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.1/spring-cloud-bindings-1.7.1.jar
...

旧日志:

...
[INFO]     [creator]     Paketo Spring Boot Buildpack 4.2.0
[INFO]     [creator]       https://github.com/paketo-buildpacks/spring-boot
[INFO]     [creator]       Creating slices from layers index
[INFO]     [creator]         dependencies
[INFO]     [creator]         spring-boot-loader
[INFO]     [creator]         snapshot-dependencies
[INFO]     [creator]         application
[INFO]     [creator]       Launch Helper: Reusing cached layer
[INFO]     [creator]       Spring Cloud Bindings 1.7.1: Reusing cached layer
[INFO]     [creator]       Web Application Type: Contributing to layer
[INFO]     [creator]         Servlet web application detected
[INFO]     [creator]         Writing env.launch/BPL_JVM_THREAD_COUNT.default
[INFO]     [creator]       4 application slices
[INFO]     [creator]       Image labels:
[INFO]     [creator]         org.opencontainers.image.title
[INFO]     [creator]         org.opencontainers.image.version
[INFO]     [creator]         org.springframework.boot.spring-configuration-metadata.json
[INFO]     [creator]         org.springframework.boot.version
...
[INFO]     [creator]     *** Images (aafb789b0498):
[INFO]     [creator]           docker.io/library/cpo-process-registry:1.0.0
[INFO] 
[INFO] Successfully built image 'docker.io/library/cpo-process-registry:1.0.0'

【讨论】:

【参考方案3】:

bootBuildImage Gradle 任务使用Paketo Buildpacks 来构建您的 OCI 容器。有一堆 buildpack 参与了容器的创建,其中一个是 Spring Boot Buildpack。除此之外,Spring Boot Buildpack 会将 Spring Cloud Bindings 添加到应用程序类路径中:

 [creator]       Spring Cloud Bindings 1.7.0: Contributing to layer
 [creator]         Downloading from https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar
 [creator]         Verifying checksum
 [creator]         Copying to /layers/paketo-buildpacks_spring-boot/spring-cloud-bindings

这一切都发生在构建容器内,并且 buildpack 不知道您的 Gradle 存储库。依赖 URL 定义在buildpack itself:

[[metadata.dependencies]]
id      = "spring-cloud-bindings"
name    = "Spring Cloud Bindings"
version = "1.7.0"
uri     = "https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.7.0/spring-cloud-bindings-1.7.0.jar"
sha256  = "e3c18bf1a3c2e52743f9ff2fa46af59e5eee0a7f0683ff562eb35aa866e4a9e9"
stacks  = [ "io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.cflinuxfs3" ]

使用环境变量BPL_SPRING_CLOUD_BINDINGS_ENABLED,您可以从绑定中禁用自动配置Spring Boot环境属性,但无论如何都会拉取jar文件。

bootBuildImage 
    environment = ["BPL_SPRING_CLOUD_BINDINGS_ENABLED" : "false"]

【讨论】:

感谢您的解释!我尝试设置该环境变量,正如您所说,它仍然试图拉动并失败了我的任务。我认为我正在尝试做的事情是不可能的? BPL_SPRING_CLOUD_BINDINGS_ENABLED 在构建时不起作用:github.com/paketo-buildpacks/spring-boot/issues/118

以上是关于带有私有仓库的 Spring Boot Gradle bootBuildImage 任务的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 多模块与 Maven 私有仓库

Git+Spring-boot+Docker+ Maven +Registry私有仓库 +jenkins 持续集成测试

带有 git 私有仓库的 Jenkins kubernetes 插件

PowerMockito 在 Spring Boot 中模拟私有方法

Spring boot:提供公共和私有(受限)静态内容

Docker搭建带有访问认证的私有仓库