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

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

一、准备Quarkus项目

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

二、准备Dockerfile

1
2
3
4
5
6
7
FROM seepine/alpine-glibc
WORKDIR /work/
COPY ./build/*-runner /work/application
RUN chmod 775 /work
EXPOSE 8080
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]

三、准备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
# 打标签时触发构建,另外标签需v开头,例如v1.0.0,同时需要配置 DOCKER_PASSWORD 的 secrets
# 若需要企业微信通知,还需要配置 WECHAT_WORK_BOT_WEBHOOK 的 secrets
# 构建后镜像为 ${docker_registry}/${docker_username}/${repo_name}:1.0.0

name: Build Images

on:
push:
tags:
- v*
env:
# 此处可修改为你任意 docker 镜像仓库地址和用户名
DOCKER_REGISTRY: registry.cn-hangzhou.aliyuncs.com
DOCKER_USERNAME: seepine
jobs:
build-image:
runs-on: ubuntu-latest
container:
image: seepine/ubuntu:act
volumes:
- ubuntu_dockercache:/opt/dockercache
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Setup Graalvm
uses: graalvm/setup-graalvm@v1
with:
java-version: '17'
version: '22.3.2'
components: 'native-image'
cache: 'gradle'

- name: Build Project
run: |
chmod a+x ./gradlew
./gradlew build -Dquarkus.package.type=native -x test --no-daemon

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- 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 }}

- name: Get Meta
run: |
echo REPO_VERSION=$(echo ${{ github.ref }} | awk -F"/" '{print $3}' | awk -F"v" '{print $2}') >> $GITHUB_ENV
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_ENV

- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile
platforms: |
linux/amd64
linux/arm64
push: true
tags: |
${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ env.REPO_NAME }}:latest
${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ env.REPO_NAME }}:${{ env.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

- 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 --rm -p 8080:8080 registry.cn-hangzhou.aliyuncs.com/seepine/quarkus-demo:1.0.0

评论