-
Notifications
You must be signed in to change notification settings - Fork 0
94 lines (81 loc) · 2.77 KB
/
ci-cd.yml
File metadata and controls
94 lines (81 loc) · 2.77 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
name: CI-CD
on:
workflow_dispatch: # 手动触发(push 触发已禁用:ECS 过期,自动部署必定失败)
env:
REGISTRY: ghcr.io
IMAGE_OWNER: achernar-eridani # 固定的 GitHub 用户名(小写)
TAG: ${{ github.sha }}
jobs:
# ---------- 1) 可扩展的占位测试 ----------
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
echo "TODO: add tests"
echo "::notice::Skip tests for now"
# ---------- 2) Build & Push ----------
build:
needs: test
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- name: Build & push frontend
uses: docker/build-push-action@v5
with:
context: ./client
file: ./client/Dockerfile.prod
platforms: linux/amd64
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_OWNER }}/blog-frontend:${{ env.TAG }}
${{ env.REGISTRY }}/${{ env.IMAGE_OWNER }}/blog-frontend:latest
push: true
- name: Build & push backend
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile.backend.prod
platforms: linux/amd64
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_OWNER }}/blog-backend:${{ env.TAG }}
${{ env.REGISTRY }}/${{ env.IMAGE_OWNER }}/blog-backend:latest
push: true
# ---------- 3) Deploy ----------
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.ECS_HOST }}
username: ${{ secrets.ECS_USER }}
key: ${{ secrets.ECS_SSH_KEY }}
script: |
set -e
cd ~/apps/Myshkin451-blog
export TAG=${{ env.TAG }}
echo "Previous tags:" && docker compose -f docker-compose.prod.yml images
# 拉取镜像
docker compose -f docker-compose.prod.yml pull frontend backend
# 启动 / 滚动更新
docker compose -f docker-compose.prod.yml up -d frontend backend
# 简单健康检查(失败即退出,让 GA 标红)
echo "Health checking..."
for i in {1..10}; do
if curl -fsSL https://myshkin451.com/api/health; then
echo "✓ Health OK"; exit 0
fi
echo "retry $i"; sleep 3
done
echo "✗ Health check failed"; exit 1