Skip to content

Commit f756c73

Browse files
add :deploy-dev.yml
1 parent d865748 commit f756c73

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

.github/workflows/deploy-dev.yml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
name: Deploy Dev to VPS
2+
3+
on:
4+
push:
5+
branches: [ "dev" ]
6+
7+
env:
8+
REGISTRY: ghcr.io
9+
IMAGE_NAME: ${{ github.repository }}
10+
11+
jobs:
12+
build_and_deploy:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
packages: write
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v5
24+
with:
25+
python-version: '3.11'
26+
27+
- name: Basic Syntax Check
28+
run: |
29+
python -m py_compile server.py
30+
python -m py_compile fetcher.py
31+
32+
- name: Log in to the Container registry
33+
uses: docker/login-action@v3
34+
with:
35+
registry: ${{ env.REGISTRY }}
36+
username: ${{ github.actor }}
37+
password: ${{ secrets.GITHUB_TOKEN }}
38+
39+
- name: Extract metadata (tags, labels) for Docker
40+
id: meta
41+
uses: docker/metadata-action@v5
42+
with:
43+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
44+
tags: |
45+
type=raw,value=dev-latest
46+
type=sha,format=long,prefix=dev-sha-
47+
48+
- name: Build and push Docker image
49+
uses: docker/build-push-action@v5
50+
with:
51+
context: .
52+
push: true
53+
tags: ${{ steps.meta.outputs.tags }}
54+
labels: ${{ steps.meta.outputs.labels }}
55+
provenance: false
56+
57+
- name: Deploy to VPS via SSH
58+
uses: appleboy/ssh-action@master
59+
with:
60+
host: ${{ secrets.VPS_HOST }}
61+
username: ${{ secrets.VPS_USERNAME }}
62+
key: ${{ secrets.VPS_SSH_KEY }}
63+
port: ${{ secrets.VPS_PORT }}
64+
script: |
65+
# 登入 GHCR
66+
echo "${{ secrets.GHCR_PAT }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
67+
68+
# 確保 overlay 網路存在
69+
docker network create -d overlay app_network || true
70+
71+
# 使用明確 SHA tag 更新應用程式服務 (避免 latest 不觸發更新的問題)
72+
if docker service inspect app_dev > /dev/null 2>&1; then
73+
docker service update \
74+
--with-registry-auth \
75+
--image ghcr.io/${{ github.repository }}:dev-sha-${{ github.sha }} \
76+
--env-add REDIS_URL=redis://redis:6379/0 \
77+
--env-add SECRET_KEY=${{ secrets.SECRET_KEY }} \
78+
--env-add CORS_ORIGINS=${{ secrets.CORS_ORIGINS }} \
79+
--env-add TURNSTILE_SITE_KEY=${{ secrets.TURNSTILE_SITE_KEY }} \
80+
--env-add TURNSTILE_SECRET_KEY=${{ secrets.TURNSTILE_SECRET_KEY }} \
81+
app_dev
82+
else
83+
docker service create \
84+
--with-registry-auth \
85+
--name app_dev \
86+
--network app_network \
87+
--env REDIS_URL=redis://redis:6379/0 \
88+
--env SECRET_KEY=${{ secrets.SECRET_KEY }} \
89+
--env CORS_ORIGINS=${{ secrets.CORS_ORIGINS }} \
90+
--env TURNSTILE_SITE_KEY=${{ secrets.TURNSTILE_SITE_KEY }} \
91+
--env TURNSTILE_SECRET_KEY=${{ secrets.TURNSTILE_SECRET_KEY }} \
92+
ghcr.io/${{ github.repository }}:dev-sha-${{ github.sha }}
93+
fi
94+
95+
# 確保 Redis 服務啟動並加入網路 (依賴與正式環境共同的 redis 服務)
96+
if docker service inspect redis > /dev/null 2>&1; then
97+
docker service update \
98+
--image redis:7-alpine \
99+
redis
100+
else
101+
docker service create \
102+
--name redis \
103+
--network app_network \
104+
redis:7-alpine
105+
fi
106+
107+
# 確保 Dev 的 Cloudflare Tunnel 服務啟動並加入此網路
108+
if docker service inspect school_grades_tunnel_dev > /dev/null 2>&1; then
109+
docker service update \
110+
--env-add TUNNEL_TOKEN=${{ secrets.TUNNEL_TOKEN_DEV }} \
111+
--network-add app_network school_grades_tunnel_dev > /dev/null 2>&1 || true
112+
else
113+
docker service create \
114+
--name school_grades_tunnel_dev \
115+
--network app_network \
116+
--env TUNNEL_TOKEN=${{ secrets.TUNNEL_TOKEN_DEV }} \
117+
cloudflare/cloudflared:latest tunnel run
118+
fi
119+
# 等待更新完成並檢查狀態
120+
docker service ps app_dev --no-trunc --format "{{.CurrentState}}" | head -1

0 commit comments

Comments
 (0)