|
| 1 | +name: 构建并发布 Docker 镜像 |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + tags: ['v*.*.*'] |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + packages: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + build-and-push: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: 检出代码 |
| 18 | + uses: actions/checkout@v6 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: 设置 Node.js |
| 23 | + uses: actions/setup-node@v6 |
| 24 | + with: |
| 25 | + node-version: '20' |
| 26 | + |
| 27 | + - name: 设置 pnpm |
| 28 | + uses: pnpm/action-setup@v4 |
| 29 | + with: |
| 30 | + version: latest |
| 31 | + |
| 32 | + - name: 缓存 pnpm 依赖 |
| 33 | + uses: actions/cache@v5 |
| 34 | + with: |
| 35 | + path: | |
| 36 | + ~/.pnpm-store |
| 37 | + node_modules |
| 38 | + key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 39 | + restore-keys: | |
| 40 | + ${{ runner.os }}-pnpm- |
| 41 | +
|
| 42 | + - name: 安装依赖 |
| 43 | + run: pnpm install --frozen-lockfile |
| 44 | + |
| 45 | + - name: 构建项目 |
| 46 | + run: pnpm run build |
| 47 | + |
| 48 | + - name: 验证构建产物 |
| 49 | + run: | |
| 50 | + if [ ! -d "docs" ]; then |
| 51 | + echo "构建失败:docs 目录不存在" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + if [ ! -f "docs/index.html" ]; then |
| 55 | + echo "构建失败:docs/index.html 不存在" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | +
|
| 59 | + BUILD_TIME=$(stat -c %Y docs/index.html 2>/dev/null || stat -f %m docs/index.html 2>/dev/null || echo "0") |
| 60 | + CURRENT_TIME=$(date +%s) |
| 61 | + TIME_DIFF=$((CURRENT_TIME - BUILD_TIME)) |
| 62 | +
|
| 63 | + echo "构建产物信息:" |
| 64 | + echo " 时间差: ${TIME_DIFF}秒" |
| 65 | +
|
| 66 | + if [ $TIME_DIFF -gt 300 ]; then |
| 67 | + echo "警告: 构建产物可能不是最新的(超过5分钟)" |
| 68 | + fi |
| 69 | +
|
| 70 | + echo "构建产物验证通过" |
| 71 | + ls -la docs/ |
| 72 | +
|
| 73 | + - name: 获取当前日期 |
| 74 | + id: date |
| 75 | + run: echo "date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT |
| 76 | + |
| 77 | + - name: 获取版本号 |
| 78 | + id: version |
| 79 | + run: | |
| 80 | + VERSION=$(node -p "require('./package.json').version") |
| 81 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 82 | + echo "当前版本: $VERSION" |
| 83 | +
|
| 84 | + - name: 准备 CI 构建环境 |
| 85 | + run: | |
| 86 | + cp .dockerignore.ci .dockerignore |
| 87 | + echo "已切换到 CI 构建模式" |
| 88 | +
|
| 89 | + - name: 设置 QEMU |
| 90 | + uses: docker/setup-qemu-action@v3 |
| 91 | + |
| 92 | + - name: 设置 Docker Buildx |
| 93 | + uses: docker/setup-buildx-action@v3 |
| 94 | + |
| 95 | + # 登录 GHCR(始终执行) |
| 96 | + - name: 登录 GitHub Container Registry |
| 97 | + uses: docker/login-action@v3 |
| 98 | + with: |
| 99 | + registry: ghcr.io |
| 100 | + username: ${{ github.actor }} |
| 101 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 102 | + |
| 103 | + # 登录 Docker Hub(仅在配置了用户名时执行) |
| 104 | + - name: 登录 Docker Hub |
| 105 | + if: vars.DOCKERHUB_USERNAME != '' |
| 106 | + uses: docker/login-action@v3 |
| 107 | + with: |
| 108 | + username: ${{ vars.DOCKERHUB_USERNAME }} |
| 109 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 110 | + |
| 111 | + - name: 构建并推送多架构镜像 |
| 112 | + uses: docker/build-push-action@v6 |
| 113 | + with: |
| 114 | + context: . |
| 115 | + file: ./Dockerfile.ci |
| 116 | + platforms: linux/amd64,linux/arm64 |
| 117 | + push: true |
| 118 | + no-cache: false |
| 119 | + pull: true |
| 120 | + tags: | |
| 121 | + ghcr.io/${{ github.repository_owner }}/taoyuan:latest |
| 122 | + ghcr.io/${{ github.repository_owner }}/taoyuan:${{ steps.version.outputs.version }} |
| 123 | + ghcr.io/${{ github.repository_owner }}/taoyuan:${{ github.sha }} |
| 124 | + ${{ vars.DOCKERHUB_USERNAME && format('{0}/taoyuan:latest', vars.DOCKERHUB_USERNAME) || '' }} |
| 125 | + ${{ vars.DOCKERHUB_USERNAME && format('{0}/taoyuan:{1}', vars.DOCKERHUB_USERNAME, steps.version.outputs.version) || '' }} |
| 126 | + ${{ vars.DOCKERHUB_USERNAME && format('{0}/taoyuan:{1}', vars.DOCKERHUB_USERNAME, github.sha) || '' }} |
| 127 | + cache-from: type=gha |
| 128 | + cache-to: type=gha,mode=max |
| 129 | + build-args: | |
| 130 | + BUILD_DATE=${{ steps.date.outputs.date }} |
| 131 | + VERSION=${{ steps.version.outputs.version }} |
| 132 | + COMMIT_SHA=${{ github.sha }} |
| 133 | + labels: | |
| 134 | + org.opencontainers.image.title=桃源乡 |
| 135 | + org.opencontainers.image.description=桃源乡 - 文字农场模拟游戏 |
| 136 | + org.opencontainers.image.version=${{ steps.version.outputs.version }} |
| 137 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 138 | + org.opencontainers.image.revision=${{ github.sha }} |
| 139 | + org.opencontainers.image.created=${{ steps.date.outputs.date }} |
0 commit comments