-
Notifications
You must be signed in to change notification settings - Fork 2
292 lines (248 loc) · 9.6 KB
/
ui-release.yml
File metadata and controls
292 lines (248 loc) · 9.6 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
name: PromptoLab-UI CI/CD Pipeline
on:
push:
branches: [master, main, develop]
paths:
- 'prompto-lab-ui/**'
- '.github/workflows/ui-release.yml'
pull_request:
branches: [master, main]
paths:
- 'prompto-lab-ui/**'
- '.github/workflows/ui-release.yml'
env:
NODE_VERSION: '18'
REGISTRY: ${{ secrets.DOCKER_REPO }}
UI_DIR: 'prompto-lab-ui'
jobs:
# Job 1: 检测变更和代码质量检查
code-quality:
name: 前端代码质量检查
runs-on: ubuntu-latest
if: contains(github.event.head_commit.message, '<Auto>') || github.event_name == 'pull_request'
outputs:
should-deploy: ${{ steps.check.outputs.should-deploy }}
version: ${{ steps.extract.outputs.version }}
module: ${{ steps.extract.outputs.module }}
has-ui-changes: ${{ steps.changes.outputs.ui }}
steps:
- name: Checkout代码
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 检测文件变更
id: changes
run: |
# 检查是否有前端文件变更
if git diff --name-only HEAD~1 HEAD | grep -q "^prompto-lab-ui/"; then
echo "ui=true" >> $GITHUB_OUTPUT
echo "检测到前端文件变更"
else
echo "ui=false" >> $GITHUB_OUTPUT
echo "未检测到前端文件变更,跳过前端部署"
fi
- name: 提取提交信息
id: extract
if: steps.changes.outputs.ui == 'true'
run: |
commit_message="${{ github.event.head_commit.message }}"
echo "提交信息: $commit_message"
# 默认值
version="$(date +%Y%m%d-%H%M%S)"
module="promptolab-ui"
skip_tests="false"
skip_build="false"
skip_deploy="false"
# 解析参数
if [[ "$commit_message" =~ -v:([^\ ]*) ]]; then
version="${BASH_REMATCH[1]}"
fi
if [[ "$commit_message" =~ -m:([^\ ]*) ]]; then
module="${BASH_REMATCH[1]}"
fi
if [[ "$commit_message" =~ -skip:([^\ ]*) ]]; then
skip_option="${BASH_REMATCH[1]}"
case $skip_option in
tests) skip_tests="true" ;;
build) skip_build="true" ;;
deploy) skip_deploy="true" ;;
all) skip_tests="true"; skip_build="true"; skip_deploy="true" ;;
esac
fi
echo "version=$version" >> $GITHUB_OUTPUT
echo "module=$module" >> $GITHUB_OUTPUT
echo "skip-tests=$skip_tests" >> $GITHUB_OUTPUT
echo "skip-build=$skip_build" >> $GITHUB_OUTPUT
echo "skip-deploy=$skip_deploy" >> $GITHUB_OUTPUT
- name: 检查是否应该部署
id: check
if: steps.changes.outputs.ui == 'true'
run: |
if [[ "${{ github.ref }}" == "refs/heads/master" ]] || [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "should-deploy=true" >> $GITHUB_OUTPUT
else
echo "should-deploy=false" >> $GITHUB_OUTPUT
fi
- name: 设置Node.js
if: steps.changes.outputs.ui == 'true'
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: '${{ env.UI_DIR }}/package-lock.json'
- name: 安装依赖
if: steps.changes.outputs.ui == 'true'
working-directory: ${{ env.UI_DIR }}
run: npm ci
- name: 代码格式检查
if: steps.changes.outputs.ui == 'true' && steps.extract.outputs.skip-tests != 'true'
working-directory: ${{ env.UI_DIR }}
run: |
# 如果有eslint配置
if [ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ]; then
npm run lint || echo "Lint检查完成"
fi
- name: 类型检查
if: steps.changes.outputs.ui == 'true' && steps.extract.outputs.skip-tests != 'true'
working-directory: ${{ env.UI_DIR }}
run: npm run type-check
- name: 单元测试
if: steps.changes.outputs.ui == 'true' && steps.extract.outputs.skip-tests != 'true'
working-directory: ${{ env.UI_DIR }}
run: |
# 如果有测试脚本
if npm run | grep -q "test"; then
npm run test || echo "测试完成"
else
echo "未配置测试脚本,跳过测试"
fi
# Job 2: 构建应用
build:
name: 构建前端应用
runs-on: ubuntu-latest
needs: code-quality
if: needs.code-quality.outputs.should-deploy == 'true' && needs.code-quality.outputs.has-ui-changes == 'true' && needs.code-quality.result == 'success'
steps:
- name: Checkout代码
uses: actions/checkout@v4
- name: 设置Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: '${{ env.UI_DIR }}/package-lock.json'
- name: 安装依赖
working-directory: ${{ env.UI_DIR }}
run: npm ci
- name: 构建应用
working-directory: ${{ env.UI_DIR }}
run: npm run build
- name: 上传构建产物
uses: actions/upload-artifact@v4
with:
name: ui-dist-files
path: ${{ env.UI_DIR }}/dist/
retention-days: 1
# Job 3: 构建Docker镜像
docker-build:
name: 构建前端Docker镜像
runs-on: ubuntu-latest
needs: [code-quality, build]
if: needs.code-quality.outputs.should-deploy == 'true' && needs.code-quality.outputs.has-ui-changes == 'true' && needs.build.result == 'success'
steps:
- name: Checkout代码
uses: actions/checkout@v4
- name: 下载构建产物
uses: actions/download-artifact@v4
with:
name: ui-dist-files
path: ${{ env.UI_DIR }}/dist/
- name: 设置Docker Buildx
uses: docker/setup-buildx-action@v3
- name: 登录Docker Registry
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_REPO }}
password: ${{ secrets.DOCKER_PWD }}
- name: 构建并推送Docker镜像
uses: docker/build-push-action@v5
with:
context: ${{ env.UI_DIR }}
file: ${{ env.UI_DIR }}/Dockerfile
push: true
tags: |
${{ env.REGISTRY }}/${{ needs.code-quality.outputs.module }}:${{ needs.code-quality.outputs.version }}
${{ env.REGISTRY }}/${{ needs.code-quality.outputs.module }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
# Job 4: 部署到服务器
deploy:
name: 部署前端到服务器
runs-on: ubuntu-latest
needs: [code-quality, docker-build]
if: needs.code-quality.outputs.should-deploy == 'true' && needs.code-quality.outputs.has-ui-changes == 'true' && needs.docker-build.result == 'success'
environment: production
steps:
- name: 部署到服务器
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_ADDRESS }}
username: ${{ secrets.SERVER_USERNAME || 'root' }}
password: ${{ secrets.SERVER_PWD }}
port: ${{ secrets.SERVER_PORT || '22' }}
script: |
set -e
MODULE="${{ needs.code-quality.outputs.module }}"
VERSION="${{ needs.code-quality.outputs.version }}"
REGISTRY="${{ env.REGISTRY }}"
PORT="${{ secrets.UI_APP_PORT || '80' }}"
echo "开始部署前端 $MODULE:$VERSION"
# 拉取最新镜像
docker pull $REGISTRY/$MODULE:$VERSION
# 停止并删除旧容器
if [ "$(docker ps -q -f name=$MODULE)" ]; then
echo "停止旧容器..."
docker stop $MODULE
fi
if [ "$(docker ps -aq -f name=$MODULE)" ]; then
echo "删除旧容器..."
docker rm $MODULE
fi
# 启动新容器
echo "启动新容器..."
docker run -d \
--name $MODULE \
-p $PORT:80 \
--restart unless-stopped \
--label "version=$VERSION" \
--label "deployed-at=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--label "type=frontend" \
$REGISTRY/$MODULE:$VERSION
# 等待容器启动
sleep 10
# 健康检查
if curl -f http://localhost:$PORT > /dev/null 2>&1; then
echo "✅ 前端部署成功!应用运行在端口 $PORT"
else
echo "❌ 前端健康检查失败"
exit 1
fi
# 清理未使用的镜像
docker image prune -f
echo "🎉 前端部署完成!"
# Job 5: 通知
notify:
name: 前端部署通知
runs-on: ubuntu-latest
needs: [code-quality, build, docker-build, deploy]
if: always() && needs.code-quality.outputs.should-deploy == 'true' && needs.code-quality.outputs.has-ui-changes == 'true'
steps:
- name: 发送通知
run: |
if [[ "${{ needs.deploy.result }}" == "success" ]]; then
echo "✅ 前端部署成功通知"
# 这里可以添加钉钉、企业微信等通知
else
echo "❌ 前端部署失败通知"
# 这里可以添加失败通知
fi