-
Notifications
You must be signed in to change notification settings - Fork 190
103 lines (98 loc) · 3.7 KB
/
Copy pathdeploy.yml
File metadata and controls
103 lines (98 loc) · 3.7 KB
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
102
103
# ThingsPanel 后端社区版自动部署工作流
#
# 功能说明:
# 1. 自动编译并部署 ThingsPanel 后端社区版到指定服务器
# 2. 保留上传的文件目录,其他目录重新部署
#
# 触发方式:
# - 代码推送: 当 main 分支有代码推送时自动触发部署
# - 手动触发: 可以通过 GitHub Actions 页面手动触发
#
# 部署步骤:
# 1. 检出代码
# 2. 设置 Go 环境 (v1.22)
# 3. 打包源码
# 4. 上传到部署服务器
# 5. 部署和重启服务
#
# 服务器部署过程:
# 1. 清理旧文件(保留 files 目录)
# 2. 解压新代码
# 3. 编译项目
# 4. 使用 PM2 重启服务
#
# 必需的 Secrets:
# - C_SSH_KEY: 社区版服务器 root 账号的 SSH 私钥
#
# 注意事项:
# 1. 部署时会保留 files 目录,不会被清理
# 2. 需要服务器已安装 Go 环境
# 3. 需要服务器已安装 PM2
# 4. 请确保服务器路径 /home/thingspanel/thingspanel-backend-community 存在
name: deploy
# 触发构建时机
on:
push:
branches: [ "main" ]
workflow_dispatch:
# 任务
jobs:
build:
# Github Action CI/CD的机器选择。
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
- name: tar
run: tar -zcvf thingspanel-backend-community.tar.gz ./*
- name: Transfer packets server
uses: appleboy/scp-action@master
with:
# Github Action 的服务器文件路径
host: 47.92.253.145
username: root
key: ${{ secrets.C_SSH_KEY }}
source: "thingspanel-backend-community.tar.gz"
target: "/home/thingspanel/"
# 部署运行
- name: Deploy
uses: appleboy/ssh-action@master
with:
host: 47.92.253.145
username: root
key: ${{ secrets.C_SSH_KEY }}
port: 22
script: |
source ~/.bash_profile
CONFIG_BACKUP="/home/thingspanel/thingspanel-backend-community.conf.yml.deploy-backup"
if [ -f /home/thingspanel/thingspanel-backend-community/configs/conf.yml ]; then
cp -p /home/thingspanel/thingspanel-backend-community/configs/conf.yml "$CONFIG_BACKUP"
else
rm -f "$CONFIG_BACKUP"
fi
find /home/thingspanel/thingspanel-backend-community -mindepth 1 ! -path '/home/thingspanel/thingspanel-backend-community/files*' -exec rm -rf {} +
tar -zxvf /home/thingspanel/thingspanel-backend-community.tar.gz -C /home/thingspanel/thingspanel-backend-community
if [ -f "$CONFIG_BACKUP" ]; then
cp -p "$CONFIG_BACKUP" /home/thingspanel/thingspanel-backend-community/configs/conf.yml
rm -f "$CONFIG_BACKUP"
else
echo "missing production config backup" >&2
exit 1
fi
cd /home/thingspanel/thingspanel-backend-community/ || exit 1
BUILD_VERSION="dev-${{ github.sha }}"
go build -ldflags "-X project/pkg/global.SYSTEM_VERSION=$BUILD_VERSION" -o thingspanel-backend-community || exit 1
test "$(./thingspanel-backend-community --version)" = "$BUILD_VERSION" || exit 1
/root/.local/share/pnpm/pm2 stop thingspanel-backend-community
/root/.local/share/pnpm/pm2 start thingspanel-backend-community
for attempt in $(seq 1 30); do
if ss -ltn | grep -q ':9999 '; then
break
fi
sleep 2
done
ss -ltn | grep -q ':9999 ' || { echo "backend did not listen on 9999" >&2; exit 1; }
curl --fail --silent --show-error --max-time 5 http://127.0.0.1:9999/health >/dev/null || exit 1