chore: 增加docker自动构建
This commit is contained in:
parent
39dff9efc5
commit
d3646c86ec
57
Dockerfile
Normal file
57
Dockerfile
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
# Stage 1: 官方 GraalVM 社区版(已含 native-image)
|
||||||
|
FROM ghcr.io/graalvm/native-image-community:latest AS builder
|
||||||
|
|
||||||
|
RUN microdnf install -y \
|
||||||
|
gcc gcc-c++ make git wget tar gzip which findutils maven \
|
||||||
|
&& microdnf clean all
|
||||||
|
|
||||||
|
# ---------- 构建 musl ----------
|
||||||
|
ARG MUSL_VER=1.2.5
|
||||||
|
WORKDIR /tmp
|
||||||
|
RUN wget -q https://musl.libc.org/releases/musl-${MUSL_VER}.tar.gz \
|
||||||
|
&& tar -xzf musl-${MUSL_VER}.tar.gz \
|
||||||
|
&& cd musl-${MUSL_VER} \
|
||||||
|
&& ./configure --prefix=/opt/musl-${MUSL_VER} --disable-shared \
|
||||||
|
&& make -j"$(nproc)" \
|
||||||
|
&& make install \
|
||||||
|
&& ln -s /opt/musl-${MUSL_VER} /opt/musl \
|
||||||
|
&& cd / && rm -rf /tmp/musl-${MUSL_VER}*
|
||||||
|
|
||||||
|
RUN ln -s /opt/musl/bin/musl-gcc /usr/local/bin/x86_64-linux-musl-gcc \
|
||||||
|
&& ln -s /opt/musl/bin/musl-gcc /usr/local/bin/x86_64-linux-musl-cc
|
||||||
|
|
||||||
|
ENV PATH="/opt/musl/bin:${PATH}"
|
||||||
|
ENV CC="musl-gcc"
|
||||||
|
ENV C_INCLUDE_PATH="/opt/musl/include"
|
||||||
|
ENV LIBRARY_PATH="/opt/musl/lib"
|
||||||
|
|
||||||
|
# ---------- 静态 zlib ----------
|
||||||
|
ARG ZLIB_VERSION=1.3.1
|
||||||
|
WORKDIR /tmp
|
||||||
|
RUN wget -q https://zlib.net/zlib-${ZLIB_VERSION}.tar.gz \
|
||||||
|
&& tar -xzf zlib-${ZLIB_VERSION}.tar.gz \
|
||||||
|
&& cd zlib-${ZLIB_VERSION} \
|
||||||
|
&& CC=musl-gcc ./configure --static --prefix=/opt/musl \
|
||||||
|
&& make -j"$(nproc)" \
|
||||||
|
&& make install \
|
||||||
|
&& cd / && rm -rf /tmp/zlib-${ZLIB_VERSION}*
|
||||||
|
|
||||||
|
# ---------- Maven 缓存优化 ----------
|
||||||
|
WORKDIR /app
|
||||||
|
COPY pom.xml ./
|
||||||
|
|
||||||
|
# 先拉依赖并缓存
|
||||||
|
RUN mvn -B -P native-linux dependency:go-offline
|
||||||
|
|
||||||
|
# ---------- 复制源码 ----------
|
||||||
|
COPY . /app
|
||||||
|
|
||||||
|
# ---------- 编译 native image ----------
|
||||||
|
RUN mvn -P native-linux -DskipTests clean package
|
||||||
|
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
# Stage 2: 输出产物镜像(可以直接 cp 出二进制)
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
FROM busybox AS export
|
||||||
|
WORKDIR /export
|
||||||
|
COPY --from=builder /app/org.jcnc.snow.cli.SnowCLI /export/Snow
|
||||||
8
docker-compose.yml
Normal file
8
docker-compose.yml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
services:
|
||||||
|
snow-export:
|
||||||
|
image: my-snow-export
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
command: [ "/bin/sh", "-c", "cp /export/Snow /output/Snow" ]
|
||||||
|
volumes:
|
||||||
|
- ./target:/output
|
||||||
46
pom.xml
46
pom.xml
@ -70,11 +70,7 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<profiles>
|
<profiles>
|
||||||
<!--
|
<!-- 原生镜像构建: Linux 平台(使用 Docker builder 中的 musl 工具链) -->
|
||||||
原生镜像构建: Linux 平台
|
|
||||||
- 使用 GraalVM 的 native-image 工具,生成静态链接的可执行文件
|
|
||||||
- 依赖 musl libc,需提前安装并配置 musl-gcc 工具链
|
|
||||||
-->
|
|
||||||
<profile>
|
<profile>
|
||||||
<id>native-linux</id>
|
<id>native-linux</id>
|
||||||
<activation>
|
<activation>
|
||||||
@ -82,25 +78,39 @@
|
|||||||
<family>unix</family>
|
<family>unix</family>
|
||||||
</os>
|
</os>
|
||||||
</activation>
|
</activation>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.graalvm.buildtools</groupId>
|
<groupId>org.graalvm.buildtools</groupId>
|
||||||
<artifactId>native-maven-plugin</artifactId>
|
<artifactId>native-maven-plugin</artifactId>
|
||||||
<version>${native.maven.plugin.version}</version>
|
<version>${native.maven.plugin.version}</version>
|
||||||
<!-- 启用插件扩展,允许在 build 生命周期中无须额外配置 -->
|
|
||||||
<extensions>true</extensions>
|
<extensions>true</extensions>
|
||||||
|
|
||||||
|
<configuration>
|
||||||
|
<mainClass>org.jcnc.snow.cli.SnowCLI</mainClass>
|
||||||
|
<imageName>Snow</imageName>
|
||||||
|
<outputDirectory>${project.build.directory}</outputDirectory>
|
||||||
|
<buildArgs>
|
||||||
|
<buildArg>--static</buildArg>
|
||||||
|
<buildArg>--libc=musl</buildArg>
|
||||||
|
<buildArg>--emit=build-report</buildArg>
|
||||||
|
<buildArg>-O2</buildArg>
|
||||||
|
<buildArg>-H:Class=org.jcnc.snow.cli.SnowCLI</buildArg>
|
||||||
|
<buildArg>-H:CCompilerPath=/opt/musl/bin/musl-gcc</buildArg>
|
||||||
|
<buildArg>-H:CLibraryPath=/opt/musl/lib</buildArg>
|
||||||
|
</buildArgs>
|
||||||
|
</configuration>
|
||||||
|
|
||||||
<executions>
|
<executions>
|
||||||
<!-- 打包阶段生成原生可执行文件 -->
|
|
||||||
<execution>
|
<execution>
|
||||||
<id>build-native</id>
|
<id>build-native</id>
|
||||||
<goals>
|
<goals>
|
||||||
<!-- compile-no-fork 在当前 JVM 进程中执行 native-image -->
|
|
||||||
<goal>compile-no-fork</goal>
|
<goal>compile-no-fork</goal>
|
||||||
</goals>
|
</goals>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
</execution>
|
</execution>
|
||||||
<!-- 测试阶段运行原生镜像的测试 -->
|
|
||||||
<execution>
|
<execution>
|
||||||
<id>test-native</id>
|
<id>test-native</id>
|
||||||
<goals>
|
<goals>
|
||||||
@ -109,24 +119,6 @@
|
|||||||
<phase>test</phase>
|
<phase>test</phase>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
|
||||||
<buildArgs>
|
|
||||||
<!-- 静态链接 -->
|
|
||||||
<buildArg>--static</buildArg>
|
|
||||||
<!-- 指定 musl libc -->
|
|
||||||
<buildArg>--libc=musl</buildArg>
|
|
||||||
<!-- 输出构建报告 -->
|
|
||||||
<buildArg>--emit build-report</buildArg>
|
|
||||||
<!-- 优化级别 O2 -->
|
|
||||||
<buildArg>-O2</buildArg>
|
|
||||||
</buildArgs>
|
|
||||||
<environment>
|
|
||||||
<!-- 指定使用 musl 工具链 -->
|
|
||||||
<PATH>/opt/musl-1.2.5/bin:${env.PATH}</PATH>
|
|
||||||
<C_INCLUDE_PATH>/opt/musl-1.2.5/include</C_INCLUDE_PATH>
|
|
||||||
<LIBRARY_PATH>/opt/musl-1.2.5/lib</LIBRARY_PATH>
|
|
||||||
</environment>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user