title: Gitea Actions/Github Actions 构建 SpringBoot 多平台 Docker 镜像
description: 借助 Gitea Actions/Github Actions 实现发布版本后,自动打包 SpringBoot 项目并构建成 Docker 镜像,推送到阿里云 Docker 仓库中
date: 2023-03-25 16:11:12
categories:

tags:
- git
- gitea
- gitea actions
- springboot
- docker image

借助 Gitea Actions/Github Actions 实现发布版本后,自动打包 SpringBoot 项目并构建成 Docker 镜像,推送到阿里云 Docker 仓库中

一、准备Springboot项目

使用 idea 新建即可,选择Gradle,java版本选择11

二、准备Dockerfile

FROM docker.io/seepine/openjdk:11-jre as builder
COPY build/libs/*.jar /work/application.jar
WORKDIR /work
RUN java -Djarmode=layertools -jar application.jar extract

FROM docker.io/seepine/openjdk:11-jre
WORKDIR work
COPY --from=builder /work/spring-boot-loader/ ./
COPY --from=builder /work/snapshot-dependencies/ ./
COPY --from=builder /work/dependencies/ ./
COPY --from=builder /work/application/ ./

ENV JAVA_OPTS="-server -Xms512m -Xmx512m" \
    SPRING_PROFILES_ACTIVE="prod"

ENTRYPOINT wait.sh && java ${JAVA_OPTS} -Ddruid.mysql.usePingMethod=false \
    -Djava.security.egd=file:/dev/./urandom \
    -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE} \
    org.springframework.boot.loader.JarLauncher

三、准备build.yaml

新建.gitea/workflows/build.yaml文件
需要提前配置 Docker 仓库的密码到 Secrets 中。

name: Build Image

# 打标签时触发构建,另外标签需v开头,例如v1.0.0,同时需要配置 DOCKER_PASSWORD 的 secrets
# 构建后镜像为 ${docker_registry}/${docker_username}/${repo_name}:1.0.0
on:
  push:
    tags:
      - v*

env:
  # 此处可修改为你任意 docker 镜像仓库地址和用户名
  DOCKER_REGISTRY: registry.cn-hangzhou.aliyuncs.com
  DOCKER_USERNAME: seepine

jobs:
  build-image:
    runs-on: ubuntu-latest
    steps:
      # 拉取代码
      - name: Checkout
        uses: actions/checkout@v3

      # java环境
      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          distribution: zulu
          java-version: 11
          cache: gradle

      # 打包
      - name: Gradle Build
        run: ./gradlew bootJar -x test --no-daemon

      - name: Docker build push
        uses: seepine/action-docker-build-push@v1
        with:
          registry: ${{ env.DOCKER_REGISTRY }}
          username: ${{ env.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          platforms: linux/amd64,linux/arm64

四、发布版本

当我们发布一个 v1.0.0 版本时,将会看到 Actions 进行工作,最终执行完成后将会推送构建成功的消息到企业微信中,消息为

springboot-demo构建成功。
镜像: registry.cn-hangzhou.aliyuncs.com/seepine/springboot-demo:1.0.0

五、运行

docker run -p 8080:8080 registry.cn-hangzhou.aliyuncs.com/seepine/springboot-demo:1.0.0