docker-build.yml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # 自动构建并推送 GHCR:push master、pull_request 或手动/workflow_dispatch 触发
  2. # 镜像 tag:latest + 日期时间 - 提交哈希(如 20260815T0054-a1b2c3d)
  3. # PR 只跑 verify(构建 + 冒烟),不推送镜像:fork PR 的 GITHUB_TOKEN 无 GHCR 写权限,
  4. # 且未合并的代码不应发布。verify 作为合并门禁,挡住不合规提交(需配合分支保护的必需检查)。
  5. # workflow_dispatch 提供两种钩入方式:
  6. # - 手动:Actions 页面点「Run workflow」或 gh workflow run docker-build.yml
  7. # - 被钩:upstream-check 现将升级推送到 PR 分支 auto-upgrade/dsh;PR 由成员批准合并后,
  8. # 合并产生的 push 事件直接触发本工作流(成员合并的 push 会正常触发 CI)
  9. #
  10. # Auto-build and push to GHCR: triggered on push to master, pull_request, or manual/API dispatch
  11. # Image tags: latest + datetime-commit-hash (e.g. 20260815T0054-a1b2c3d)
  12. # PRs run verify only (build + smoke) and never push images: a fork PR's GITHUB_TOKEN cannot
  13. # write GHCR, and unmerged code must not be published. verify acts as a merge gate against
  14. # non-compliant submissions (pair it with branch protection's required checks).
  15. # workflow_dispatch offers two hook-in paths:
  16. # - Manual: click "Run workflow" in the Actions tab or gh workflow run docker-build.yml
  17. # - Hooked: upstream-check now pushes upgrades to the PR branch auto-upgrade/dsh; once a member
  18. # approves and merges the PR, the merge push fires this workflow directly (a member's
  19. # merge push triggers CI normally)
  20. name: build-push-ghcr
  21. on:
  22. push:
  23. branches: [master]
  24. pull_request:
  25. branches: [master]
  26. workflow_dispatch:
  27. env:
  28. IMAGE: ghcr.io/xidong-ai/deepseek-harness-web-docker
  29. jobs:
  30. # 推送前冒烟:构建镜像并跑端到端检查(鉴权 token/cookie 或 basic auth / 特权 API fence / 双进程 / trustedHosts 注入),失败则不推送
  31. # push 与 PR 共用同一套检查:push 失败阻断发布,PR 失败阻断合并(需分支保护勾选该检查为必需)
  32. #
  33. # Pre-push smoke test: build the image and run end-to-end checks (token/cookie or basic auth /
  34. # privileged-API fence / both processes / trustedHosts injection); a failure blocks the push.
  35. # push and PR share the same checks: a push failure blocks release, a PR failure blocks merge
  36. # (requires the check to be marked required in branch protection)
  37. verify:
  38. runs-on: ubuntu-latest
  39. permissions:
  40. contents: read
  41. steps:
  42. - uses: actions/checkout@v4
  43. - name: 提取 DSH_VERSION
  44. id: version
  45. run: |
  46. echo "version=$(grep -E '^ARG DSH_VERSION=' Dockerfile | head -n1 | cut -d= -f2)" >> "$GITHUB_OUTPUT"
  47. - name: 构建并冒烟测试
  48. run: |
  49. scripts/smoke-test.sh "${{ steps.version.outputs.version }}"
  50. build:
  51. needs: verify
  52. # PR 只做验证,不发布镜像(fork PR 的 GITHUB_TOKEN 无 packages:write,且未合并代码不应发布);
  53. # 仅 push master / 手动 dispatch 推送 GHCR
  54. #
  55. # PRs verify only and never publish images (a fork PR's GITHUB_TOKEN has no packages:write,
  56. # and unmerged code must not be released); GHCR is pushed only on push to master / manual dispatch
  57. if: github.event_name != 'pull_request'
  58. runs-on: ubuntu-latest
  59. permissions:
  60. contents: read
  61. packages: write
  62. steps:
  63. - uses: actions/checkout@v4
  64. - name: Set up QEMU
  65. uses: docker/setup-qemu-action@v3
  66. - name: Set up Docker Buildx
  67. uses: docker/setup-buildx-action@v3
  68. - name: Log in to GHCR
  69. uses: docker/login-action@v3
  70. with:
  71. registry: ghcr.io
  72. username: ${{ github.actor }}
  73. password: ${{ secrets.GITHUB_TOKEN }}
  74. - name: 生成日期时间 - 哈希 tag
  75. id: meta
  76. run: |
  77. echo "tag=$(date +%Y%m%dT%H%M)-${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
  78. - name: Build and push
  79. uses: docker/build-push-action@v6
  80. with:
  81. context: .
  82. push: true
  83. platforms: linux/amd64,linux/arm64
  84. # gha 缓存:跨 run 复用构建层(mode=max 保留 apt/corepack 等中间层,
  85. # DSH_VERSION 变化时未变层仍命中);cache scope 为当前分支
  86. #
  87. # gha cache: reuse build layers across runs (mode=max keeps intermediate
  88. # layers such as apt/corepack, so unchanged layers still hit when DSH_VERSION changes);
  89. # cache scope is the current branch
  90. cache-from: type=gha
  91. cache-to: type=gha,mode=max
  92. tags: |
  93. ${{ env.IMAGE }}:latest
  94. ${{ env.IMAGE }}:${{ steps.meta.outputs.tag }}