name: Docker Image CI on: push: tags: - 'v*.*.*' # 只在以 v 开头的标签推送时触发,例如 v1.0.0 jobs: release: name: Release Docker image runs-on: ubuntu-latest permissions: packages: write contents: write id-token: write issues: write steps: - name: Check out the repo uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 with: driver-opts: | # 使用更节省空间的缓存存储模式 image=moby/buildkit:master # 限制并行构建的worker数量减少内存压力 --allow-insecure-entitlement security.insecure - name: Log in to the GitHub Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.TOKEN }} - name: Extract version from tag id: get_version run: | echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV - name: Build and push Docker image id: build_push uses: docker/build-push-action@v6 with: context: . push: true tags: | ghcr.io/${{ github.repository }}:${{ env.VERSION }} ghcr.io/${{ github.repository }}:latest platforms: linux/amd64,linux/arm64 # 使用 registry 缓存避免占用本地磁盘 cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max # 启用压缩减少传输数据量 compression: zstd - name: Clean up Docker resources run: | # 清理构建缓存和临时镜像 docker system prune -af docker builder prune -af