-
Notifications
You must be signed in to change notification settings - Fork 765
142 lines (124 loc) · 4.01 KB
/
Copy pathdocker-build.yml
File metadata and controls
142 lines (124 loc) · 4.01 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
name: Build and Push Docker Image
on:
workflow_dispatch: # 允许手动触发
push:
branches:
- main
- develop
tags:
- 'v*'
pull_request:
branches:
- main
env:
REGISTRY: docker.io
IMAGE_NAME: feather2dev/paper-burner-x
jobs:
build-and-push:
# PR 上仅需跑 test 必要检查;跳过镜像构建以避免 lock 不一致导致失败
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# 避免在 PR 场景下出现 "-<sha>" 这种以连字符开头的非法标签
# 统一使用稳定前缀,确保 Docker tag 合法(如 "sha-ac262a7")
type=sha,prefix=sha-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Image digest
run: echo ${{ steps.build.outputs.digest }}
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16-alpine
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: server/package-lock.json
- name: Install dependencies
working-directory: ./server
run: npm install --no-audit --no-fund
- name: Generate Prisma Client
working-directory: ./server
run: npx prisma generate
- name: Run database migrations
working-directory: ./server
env:
DATABASE_URL: postgresql://test:test@localhost:5432/test
run: npx prisma migrate deploy
- name: Lint check (placeholder)
working-directory: ./server
run: npm run lint
continue-on-error: true
- name: Test server startup
working-directory: ./server
env:
DATABASE_URL: postgresql://test:test@localhost:5432/test
JWT_SECRET: test-secret-key-for-ci
run: |
timeout 10s node src/index.js || code=$?
if [ $code -eq 124 ]; then
echo "Server started successfully"
exit 0
else
echo "Server failed to start"
exit 1
fi
- name: Run unit tests
working-directory: ./server
env:
NODE_ENV: test
DATABASE_URL: postgresql://test:test@localhost:5432/test
JWT_SECRET: test-secret-key-for-ci
run: npm test