抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

借助 Gitea Actions/Github Actions 实现发布版本后,自动打包 SpringBoot 项目并构建成 Docker 镜像,推送到阿里云 Docker 仓库中,并通过企业微信机器人发送消息通知

一、准备Springboot项目

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

二、准备Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 中,若需要企业微信通知,也需要配置企业微信机器人的 WebHook 到 Secrets 中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
name: CI

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

jobs:
build-image:
runs-on: ubuntu-latest
container:
# 使用支持docker的act镜像
image: seepine/ubuntu:act
volumes:
# 缓存docker构建步骤
- ubuntu_dockercache:/opt/dockercache
env:
# 此处可修改为你任意 docker 镜像仓库地址和用户名
DOCKER_REGISTRY: registry.cn-hangzhou.aliyuncs.com
DOCKER_USERNAME: seepine
steps:
# 拉取代码
- name: Checkout
uses: actions/checkout@v3

# java环境
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: zulu
java-version: 11
# 缓存gradle,若是使用maven构建,改为maven即可,具体可查看 https://github.com/actions/setup-java
cache: gradle

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

# Docker配置qemu
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

# Docker配置多平台环境
- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v2

# 登录镜像仓库
- name: Login to DockerHub
uses: docker/login-action@v2
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ env.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

# 生成名称和版本,例如 seepine/springboot-demo ,由发布 v1.2.5 触发,则分别为
# REPO_NAME=springboot-demo
# REPO_VERSION=1.2.5
- name: Get Meta
id: meta
run: |
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
echo REPO_VERSION=$(echo ${{ github.ref }} | awk -F"/" '{print $3}' | awk -F"v" '{print $2}') >> $GITHUB_OUTPUT

# 打包构建
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
platforms: |
linux/amd64
linux/arm64
push: true
# 即最终打包成例如 registry.cn-hangzhou.aliyuncs.com/seepine/springboot-demo:latest
# 和 registry.cn-hangzhou.aliyuncs.com/seepine/springboot-demo:1.2.5
tags: |
${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ steps.meta.outputs.REPO_NAME }}:latest
${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
cache-from: type=local,src=/opt/dockercache/.buildx-${{ steps.meta.outputs.REPO_NAME }}-cache
cache-to: type=local,dest=/opt/dockercache/.buildx-${{ steps.meta.outputs.REPO_NAME }}-cache-new,mode=max

# 重建docker缓存
- name: Rebuild docker cache
run: |
rm -rf /opt/dockercache/.buildx-${{ steps.meta.outputs.REPO_NAME }}-cache
mv /opt/dockercache/.buildx-${{ steps.meta.outputs.REPO_NAME }}-cache-new /opt/dockercache/.buildx-${{ steps.meta.outputs.REPO_NAME }}-cache

# 发送通知
- name: WeChat Work notification
uses: seepine/action-wechat-work@master
# 若没有配置机器人WebHook则跳过
if: ${{ env.WECHAT_WORK_BOT_WEBHOOK != '' }}
env:
WECHAT_WORK_BOT_WEBHOOK: ${{ secrets.WECHAT_WORK_BOT_WEBHOOK }}
with:
msgtype: markdown
content: "${{ steps.meta.outputs.REPO_NAME }}构建成功。\n
> 镜像: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}"

四、发布版本

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

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

五、运行

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

评论