簡體   English   中英

使用 Cloud Native Build Packs/Paketo.io 和 spring-boot-maven-plugin 為 GitHub 容器注冊表鏈接配置自定義容器映像 LABEL

[英]Configure custom container image LABEL using Cloud Native Build Packs/Paketo.io with spring-boot-maven-plugin for GitHub Container Registry link

我使用https://start.spring.io/創建了一個簡單的 Spring 引導應用程序。 現在我想使用spring-boot-maven-plugin Paketo.io / Cloud Native Build Pack 支持來構建容器映像並使用 ZD3B7C913CD04EBFEC0E9EC32CB6FD5CZ 將其推送到 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>

我成功地在我的 GitHub 操作工作流程中使用mvn spring-boot:build-image命令創建了 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 }}

但 Container Registry 映像未鏈接到 GitHub 存儲庫。 由於這必須使用 Dockerfile 內部的特定 OCI 兼容LABELDockerfile

LABEL org.opencontainers.image.source="https://github.com/jonashackt/helloworld"

如何使用 Cloud Native Build Packs / Paketo 和spring-boot-maven-plugin配置這個LABEL

由於spring-boot-maven-plugin透明地包裝了 Paketo.io / Cloud Native Build Packs,最好的方法是從https://paketo.io/docs開始。 有一節介紹如何將自定義標簽應用於應用程序圖像

Paketo 用戶可以使用 Image Labels Buildpack 向應用程序映像添加標簽。

由於org.opencontainers.image.source是特定於 OCI 的 label,因此 Image Labels Buildpack將為我們設置正確的 label。 我們所要做的就是將一個環境變量傳遞給我們以 BP_OCI_ 為前綴的BP_OCI_構建。 查看文檔中可能的 OCI 特定標簽 例如,如果我們運行以下 Paketo 構建:

pack build spring-boot-buildpack
  --path . \
  --builder paketobuildpacks/builder:base \
  --env "BP_OCI_DESCRIPTION=Demo Application"

生成的應用程序映像將具有一個LABEL ,其值定義為org.opencontainers.image.description Demo Application 所以為了設置org.opencontainers.image.source我們需要為我們的 Paketo 構建定義環境變量BP_OCI_SOURCE

但是當我們在這里使用spring-boot-maven-plugin時,我們需要在pom.xml中以某種方式配置這個環境變量,因為我們不直接與 Paketo CLI 交互。 文檔告訴我們,我們可以使用configuration標簽中的image.env標簽來定義

應該傳遞給構建器的環境變量。

為了將 OCI 特定的LABEL org.opencontainers.image.source https://github.com/yourGitHubUserOrOrgaName/yourRepositoryName配置到 Paketo 使用 spring-boot-maven-plugin 構建的應用程序映像中,請將以下標簽添加到您的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>

現在您的圖像已鏈接到您的 GitHub 存儲庫。 如果您查看您帳戶的軟件包並單擊構建映像,您應該會看到所有README.md信息映射如下:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM