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