使用 Cloud Native Build Packs/Paketo.io 和 spring-boot-maven-plugin 为 GitHub 容器注册表链接配置自定义容器映像 LABEL

jon*_*ckt 2 buildpack spring-boot spring-boot-maven-plugin github-actions github-package-registry

我使用https://start.spring.io/创建了一个简单的 Spring Boot 应用程序。现在我想使用 Paketo.io / Cloud Native Build Pack 支持来spring-boot-maven-plugin构建容器映像,并使用 GitHub Actions 将其推送到 GitHub 容器注册表。

我的pom.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.jonashackt</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloworld</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>15</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

mvn spring-boot:build-image我成功地使用GitHub Actions 工作流程中的命令创建了 Docker 映像。我还成功将其推送到 GitHub 容器注册表(按照本指南)。这是我的build.yml工作流程:

name: publish

on: [push]

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up JDK 15
      uses: actions/setup-java@v1
      with:
        java-version: 15

    - name: Build the hello-world Docker image
      run: |
        echo 'Login to GitHub Container Registry'
        echo $CR_PAT | docker login ghcr.io -u jonashackt --password-stdin

        echo 'Build a container image from our Spring Boot app using Paketo.io / Cloud Native Build Packs'     
        mvn spring-boot:build-image --batch-mode --no-transfer-progress

        echo 'tag Paketo build image to have the right GitHub Container Registry coordinates'
        docker tag helloworld:0.0.1-SNAPSHOT ghcr.io/jonashackt/helloworld:latest
        
        docker push ghcr.io/jonashackt/helloworld:latest
      env:
        CR_PAT: ${{ secrets.CR_PAT }}
Run Code Online (Sandbox Code Playgroud)

但容器注册表映像未链接到 GitHub 存储库。由于这必须通过LABEL以下内部符合特定 OCI 的要求来完成Dockerfile

LABEL org.opencontainers.image.source="https://github.com/jonashackt/helloworld"
Run Code Online (Sandbox Code Playgroud)

如何LABEL使用 Cloud Native Build Packs/Paketo 以及 来配置这一点spring-boot-maven-plugin

jon*_*ckt 5

由于透明地包装了 Paketo.io / Cloud Native Build Pack,因此最好的方法是从https://paketo.io/docsspring-boot-maven-plugin开始。有一节介绍如何将自定义标签应用于应用程序图像

Paketo 用户可以使用图像标签构建包向应用程序图像添加标签。

由于org.opencontainers.image.source是 OCI 特定的标签,Image Labels Buildpack将为我们设置正确的标签。我们所要做的就是将一个环境变量传递给我们前缀为 的 Paketo 构建BP_OCI_。查看文档中可能的 OCI 特定标签。例如,如果我们运行以下 Paketo 构建:

pack build spring-boot-buildpack
  --path . \
  --builder paketobuildpacks/builder:base \
  --env "BP_OCI_DESCRIPTION=Demo Application"
Run Code Online (Sandbox Code Playgroud)

生成的应用程序映像将具有LABEL由值定义的org.opencontainers.image.description Demo Application。因此,为了进行设置,我们需要为 Paketo 构建org.opencontainers.image.source定义环境变量!BP_OCI_SOURCE

但是当我们在这里使用时spring-boot-maven-plugin,我们需要在我们的 中以某种方式配置这个环境变量pom.xml,因为我们不直接与 Paketo CLI 交互。文档告诉我们,我们可以使用image.env标签内的标签configuration来定义

应传递给构建器的环境变量。

为了LABEL org.opencontainers.image.source https://github.com/yourGitHubUserOrOrgaName/yourRepositoryName使用 spring-boot-maven-plugin 配置 Paketo 构建的特定于 OCI 的应用程序映像,请将以下标签添加到您的项目中pom.xml(这里还有一个完全可理解的示例项目,包括pom.xml):

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <env>
                            <BP_OCI_SOURCE>https://github.com/yourGitHubUserOrOrgaName/yourRepositoryName</BP_OCI_SOURCE>
                        </env>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>
Run Code Online (Sandbox Code Playgroud)

现在您的图像已链接到您的 GitHub 存储库。如果您查看帐户的包并单击构建映像,您应该会看到README.md映射的所有信息,如下所示:

在此输入图像描述