-
Notifications
You must be signed in to change notification settings - Fork 0
183 lines (149 loc) · 5.25 KB
/
deploy.yml
File metadata and controls
183 lines (149 loc) · 5.25 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
name: CI/CD Pipeline
# on:
# push:
# branches: [ main, develop ]
# pull_request:
# branches: [ main ]
# 手动触发
on:
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME_BACKEND: ${{ github.repository }}/chatapp-backend
IMAGE_NAME_FRONTEND: ${{ github.repository }}/chatapp-frontend
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
- name: Cache Maven dependencies
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Run backend tests
env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
working-directory: ./backend
run: mvn clean test
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install frontend dependencies
working-directory: ./frontend
run: npm ci
# - name: Run frontend tests
# working-directory: ./frontend
# run: npm test -- --coverage --watchAll=false
- name: Upload coverage reports
uses: codecov/codecov-action@v3
with:
directory: ./frontend/coverage
build-and-push:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for backend
id: meta-backend
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push backend image
uses: docker/build-push-action@v5
with:
context: ./backend
push: true
tags: ${{ steps.meta-backend.outputs.tags }}
labels: ${{ steps.meta-backend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Extract metadata (tags, labels) for frontend
id: meta-frontend
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push frontend image
uses: docker/build-push-action@v5
with:
context: ./frontend
push: true
tags: ${{ steps.meta-frontend.outputs.tags }}
labels: ${{ steps.meta-frontend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
needs: build-and-push
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy to server
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.PORT }}
script: |
# 创建应用目录
mkdir -p /opt/chatapp
cd /opt/chatapp
# 停止现有服务
docker-compose -f docker-compose.yml down || true
# 清理旧镜像
docker system prune -af
# 拉取最新镜像
echo ${{ secrets.REPOSITORY_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# 创建环境变量文件
cat > .env << EOF
DOCKER_REGISTRY=ghcr.io/richardthunder/chatapp
GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }}
VERSION=latest
EOF
# 下载最新的 docker-compose 文件
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3.raw" \
-o docker-compose.yml \
https://api.github.com/repos/${{ github.repository }}/contents/docker-compose.yml
# 启动服务
docker-compose -f docker-compose.yml pull
docker-compose -f docker-compose.yml up -d
# 等待服务启动
sleep 30
# 健康检查
curl -f http://localhost:8080/actuator/health || exit 1
curl -f http://localhost/ || exit 1
# 清理
docker logout ghcr.io