Elffuss BraidHub

kikocis/utopia public

de93f0eb4620 · 4.3 KiBRawPermalink
1# 发布容器镜像到 ghcr.io。没有这个,README 承诺的"一条命令"实际是
2# "在自己机器上编译 Rust release + LTO 二十分钟,还得先拉得到 rust/node 基础镜像"。
3name: Release
4
5on:
6 push:
7 tags: ["v*"]
8 # 正式打 tag 之前先出一个可部署的镜像用
9 workflow_dispatch:
10 inputs:
11 tag:
12 description: "Image tag to publish (e.g. 0.1.0-rc1)"
13 required: true
14
15permissions:
16 contents: read
17 packages: write
18
19jobs:
20 image:
21 runs-on: ubuntu-latest
22 steps:
23 - uses: actions/checkout@v4
24
25 - uses: docker/setup-buildx-action@v3
26
27 - uses: docker/login-action@v3
28 with:
29 registry: ghcr.io
30 username: ${{ github.actor }}
31 password: ${{ secrets.GITHUB_TOKEN }}
32
33 - id: meta
34 uses: docker/metadata-action@v5
35 with:
36 images: ghcr.io/${{ github.repository }}
37 tags: |
38 type=semver,pattern={{version}}
39 type=semver,pattern={{major}}.{{minor}}
40 type=raw,value=latest,enable=${{ github.ref_type == 'tag' }}
41 type=raw,value=${{ inputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }}
42
43 # 先只 load 到本地跑一遍。"镜像构建成功"证明不了"容器能起来"——
44 # glibc 跨发行版不匹配就是构建全绿、启动即死。
45 # amd64 only:arm64 得走 QEMU,Rust release + LTO 会拖到一小时以上。
46 - name: Build for smoke test
47 uses: docker/build-push-action@v6
48 with:
49 context: .
50 file: docker/Dockerfile
51 load: true
52 tags: utopia:smoke
53 cache-from: type=gha
54 cache-to: type=gha,mode=max
55
56 - name: Smoke test — boot it, migrate, register a user
57 run: |
58 set -euo pipefail
59 docker network create smoke
60 docker run -d --name pg --network smoke \
61 -e POSTGRES_USER=utopia -e POSTGRES_PASSWORD=utopia -e POSTGRES_DB=utopia \
62 pgvector/pgvector:pg16
63 # 轮询条件一律写进 if:set -e 下 `cmd && break` 在 cmd 失败时会终止整个
64 # 脚本,把"还没就绪"变成"构建失败"。
65 for _ in $(seq 1 40); do
66 if docker exec pg pg_isready -U utopia -d utopia >/dev/null 2>&1; then break; fi
67 sleep 2
68 done
69 # 故意不给 UTOPIA_JWT_SECRET:让这一跑覆盖自动生成那条路径。
70 # 下面的注册请求会签发并携带 token,密钥没生成好就过不了。
71 docker run -d --name app --network smoke -p 1516:1516 \
72 -e UTOPIA_DATABASE_URL=postgres://utopia:utopia@pg:5432/utopia \
73 utopia:smoke
74
75 # 必须打 /api/v1/health 并核对 JSON:健康检查挂在 nest("/api/v1") 里,
76 # 而镜像带 SPA fallback——请求 /health 会拿到 200 的 index.html,看着像通过。
77 ok=""
78 for _ in $(seq 1 40); do
79 if curl -sf -m 3 http://localhost:1516/api/v1/health -o /tmp/h.json \
80 && grep -q '"status":"ok"' /tmp/h.json; then ok=1; break; fi
81 sleep 2
82 done
83 if [ -z "$ok" ]; then echo "::error::container never became healthy"; docker logs app; exit 1; fi
84
85 # 注册一次,把迁移 + argon2 + JWT 签发整条链路真正走一遍
86 code=$(curl -s -o /tmp/reg.json -w '%{http_code}' -m 10 \
87 -X POST http://localhost:1516/api/v1/auth/register \
88 -H 'Content-Type: application/json' \
89 -d '{"email":"smoke@example.com","password":"smoke-password-123","display_name":"Smoke"}')
90 if [ "$code" != "200" ]; then
91 echo "::error::register returned $code"; cat /tmp/reg.json; docker logs app; exit 1
92 fi
93 grep -q '"token"' /tmp/reg.json || { echo "::error::no token issued"; cat /tmp/reg.json; exit 1; }
94 echo "smoke test passed: healthy, migrated, user registered, token issued"
95
96 # 冒烟通过才发布。缓存已热,这步基本是瞬间完成。
97 - name: Push
98 uses: docker/build-push-action@v6
99 with:
100 context: .
101 file: docker/Dockerfile
102 push: true
103 tags: ${{ steps.meta.outputs.tags }}
104 labels: ${{ steps.meta.outputs.labels }}
105 cache-from: type=gha
106 cache-to: type=gha,mode=max