-
Notifications
You must be signed in to change notification settings - Fork 39
106 lines (92 loc) · 4.13 KB
/
Docker Sz-Admin Test CI.yml
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
name: Docker Sz-Admin Test CI
on:
push:
branches: [ "test" ]
pull_request:
branches: [ "test" ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
env:
# 服务名
APP_NAME: sz-admin
# 运行环境变量
RUNNING_ACTIVE: preview
# 端口。若修改端口,请保持与 Dockerfile中的 EXPOSE属性一致
SERVICE_PORT: 9800
# nginx配置文件路径
NGINX_CONF_DIR: /home/conf/sz-admin-nginx/conf.d
# Docker 仓库域名
ACR_DOMAIN: registry.cn-beijing.aliyuncs.com
# 命名空间
ACR_ZONE: sz-action
VERSION: test
SHELL_RUN_DIR: /home/run
# 预览环境标识(预览环境某些功能将禁用)
VITE_PREVIEW: true
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install pnpm
run: npm install -g pnpm
- name: Install dependencies
run: pnpm install
- name: Build project
env:
VITE_PREVIEW: ${{ env.VITE_PREVIEW }}
run: pnpm run build
- name: Install sshpass
run: sudo apt-get update && sudo apt-get install -y sshpass
- name: Check if default.conf exists on remote
id: check-file
run: |
file_exists=$(sshpass -p ${{ secrets.REMOTE_PASSWORD_TEST }} ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER_TEST }}@${{ secrets.REMOTE_HOST_TEST }} "if [ -f ${{ env.NGINX_CONF_DIR }}/default.conf ]; then echo true; else echo false; fi")
echo "file_exists=$file_exists" >> $GITHUB_ENV
- name: Upload default.conf if needed
if: ${{ env.file_exists == 'false' }}
run: sshpass -p ${{ secrets.REMOTE_PASSWORD_TEST }} scp -o StrictHostKeyChecking=no ./nginx/default.conf ${{ secrets.REMOTE_USER_TEST }}@${{ secrets.REMOTE_HOST_TEST }}:${{ env.NGINX_CONF_DIR }}
- name: Build Docker image
run: docker build -t ${{ env.APP_NAME }}:${{ env.VERSION }} .
- name: Log in to ACR
run: echo "${{ secrets.ACR_PASSWORD }}" | docker login --username=${{ secrets.ACR_USERNAME }} ${{ env.ACR_DOMAIN }} --password-stdin
- name: Tag Docker image
run: docker tag ${{ env.APP_NAME }}:${{ env.VERSION }} ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
- name: Push Docker image to ACR
run: docker push ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
- name: Deploy to remote server
uses: appleboy/[email protected]
with:
host: ${{ secrets.REMOTE_HOST_TEST }}
username: ${{ secrets.REMOTE_USER_TEST }}
password: ${{ secrets.REMOTE_PASSWORD_TEST }}
script: |
docker pull ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
echo "==================== 生成启动命令脚本 ===================="
mkdir -p ${{ env.SHELL_RUN_DIR }}
START_SCRIPT="${{ env.SHELL_RUN_DIR }}/docker_run_${{ env.APP_NAME }}_${{ env.RUNNING_ACTIVE }}-${{ env.VERSION }}.sh"
cat > $START_SCRIPT <<EOL
#!/bin/bash
echo "==================== 停止旧应用容器 ===================="
docker stop ${{ env.APP_NAME }} || true
docker rm ${{ env.APP_NAME }} || true
docker image prune -f
docker builder prune -f
echo "==================== 启动应用容器 ===================="
docker run -itd \\
--name ${{ env.APP_NAME }} \\
--restart always \\
-p ${{ env.SERVICE_PORT }}:${{ env.SERVICE_PORT }} \\
-e TZ=Asia/Shanghai \\
-v ${{ env.NGINX_CONF_DIR }}:/etc/nginx/conf.d \\
${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ env.APP_NAME }}:${{ env.VERSION }}
EOL
chmod +x $START_SCRIPT
echo "启动脚本已生成:$START_SCRIPT"
echo "可以运行以下命令手动启动容器:"
echo "bash $START_SCRIPT"
bash $START_SCRIPT