|
| 1 | +name: Build and Push Multi-Arch Docker Image |
| 2 | + |
| 3 | +# 触发条件:推送 v* 标签时(如 git tag v1.1.0 && git push --tags) |
| 4 | +on: |
| 5 | + push: |
| 6 | + tags: |
| 7 | + - 'v*' |
| 8 | + |
| 9 | +# 权限:允许 Actions 推送到 GHCR |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + packages: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + build-and-push: |
| 16 | + runs-on: ubuntu-latest # AMD64 主机,用于交叉构建 ARM |
| 17 | + steps: |
| 18 | + # 检出代码 |
| 19 | + - name: Checkout |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + # 设置 Node.js |
| 23 | + - name: Set up Node.js |
| 24 | + uses: actions/setup-node@v4 |
| 25 | + with: |
| 26 | + node-version: '22' |
| 27 | + cache: yarn |
| 28 | + |
| 29 | + # 安装依赖 |
| 30 | + - name: Install dependencies |
| 31 | + run: yarn install --frozen-lockfile |
| 32 | + |
| 33 | + # 构建 Nuxt 项目 |
| 34 | + - name: Build Nuxt |
| 35 | + run: yarn build |
| 36 | + |
| 37 | + # 设置 Docker Buildx(启用多架构) |
| 38 | + - name: Set up Docker Buildx |
| 39 | + uses: docker/setup-buildx-action@v3 |
| 40 | + |
| 41 | + # 登录 GHCR(使用 GitHub Token) |
| 42 | + - name: Login to GHCR |
| 43 | + uses: docker/login-action@v3 |
| 44 | + with: |
| 45 | + registry: ghcr.io |
| 46 | + username: ${{ github.actor }} |
| 47 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + |
| 49 | + # 设置 QEMU(用于 ARM 交叉构建) |
| 50 | + - name: Set up QEMU |
| 51 | + uses: docker/setup-qemu-action@v3 |
| 52 | + |
| 53 | + # 从标签提取版本(匹配 v1.0.0 格式) |
| 54 | + - name: Extract version from tag |
| 55 | + id: version |
| 56 | + run: | |
| 57 | + VERSION="${{ github.ref_name }}" |
| 58 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 59 | +
|
| 60 | + # 构建并推送多架构镜像 |
| 61 | + - name: Build and push multi-arch image |
| 62 | + uses: docker/build-push-action@v6 |
| 63 | + with: |
| 64 | + context: . |
| 65 | + file: ./dockerfile # 你的 Dockerfile 路径 |
| 66 | + push: true # 直接推送 |
| 67 | + platforms: linux/amd64,linux/arm64 # 目标架构 |
| 68 | + tags: | |
| 69 | + ghcr.io/wechat-article/wechat-article-exporter:${{ steps.version.outputs.version }} |
| 70 | + ghcr.io/wechat-article/wechat-article-exporter:${{ steps.version.outputs.version }}-amd64 |
| 71 | + ghcr.io/wechat-article/wechat-article-exporter:${{ steps.version.outputs.version }}-arm64 |
| 72 | + build-args: | |
| 73 | + VERSION=${{ steps.version.outputs.version }} # 传递到 Dockerfile 的 ARG |
| 74 | + cache-from: type=gha # 使用 GitHub Actions 缓存加速 |
| 75 | + cache-to: type=gha,mode=max |
0 commit comments