|
| 1 | +name: Docker Publish |
| 2 | + |
| 3 | +on: |
| 4 | + # Tag push:仅预热缓存,不推镜像 |
| 5 | + push: |
| 6 | + tags: ['v*.*.*'] |
| 7 | + # Release:发布时才推 latest + vX.Y.Z |
| 8 | + release: |
| 9 | + types: [published] |
| 10 | + # 手动触发:自动发现当前最新 vX.Y.Z,并与 latest 一起发布(同一构建) |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +env: |
| 14 | + REGISTRY: ghcr.io |
| 15 | + IMAGE_NAME: ${{ github.repository }} # 将在步骤中强制转小写 |
| 16 | + |
| 17 | +jobs: |
| 18 | + # 1) Tag push:构建但不推送(预热缓存) |
| 19 | + build-cache-on-tag: |
| 20 | + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + packages: write |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Force IMAGE_NAME lowercase (GHCR requires lowercase) |
| 29 | + run: echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" |
| 30 | + |
| 31 | + - uses: docker/setup-qemu-action@v3 |
| 32 | + |
| 33 | + - name: Set up Docker Buildx (pin BuildKit version) |
| 34 | + uses: docker/setup-buildx-action@v3 |
| 35 | + with: |
| 36 | + driver-opts: | |
| 37 | + image=moby/buildkit:v0.25.1 |
| 38 | +
|
| 39 | + - name: Log in to GitHub Container Registry |
| 40 | + uses: docker/login-action@v3 |
| 41 | + with: |
| 42 | + registry: ${{ env.REGISTRY }} |
| 43 | + username: ${{ github.actor }} |
| 44 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + |
| 46 | + - name: Build (no push, warm cache) |
| 47 | + uses: docker/build-push-action@v5 |
| 48 | + with: |
| 49 | + context: . |
| 50 | + platforms: linux/amd64,linux/arm64 |
| 51 | + push: false |
| 52 | + cache-from: type=gha |
| 53 | + cache-to: type=gha,mode=max |
| 54 | + |
| 55 | + # 2) Release published:推送 latest + vX.Y.Z(同一构建 -> 同 digest) |
| 56 | + build-and-push-on-release: |
| 57 | + if: github.event_name == 'release' && github.event.action == 'published' |
| 58 | + runs-on: ubuntu-latest |
| 59 | + permissions: |
| 60 | + contents: read |
| 61 | + packages: write |
| 62 | + steps: |
| 63 | + - uses: actions/checkout@v4 |
| 64 | + |
| 65 | + - name: Force IMAGE_NAME lowercase (GHCR requires lowercase) |
| 66 | + run: echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" |
| 67 | + |
| 68 | + - uses: docker/setup-qemu-action@v3 |
| 69 | + |
| 70 | + - name: Set up Docker Buildx (pin BuildKit version) |
| 71 | + uses: docker/setup-buildx-action@v3 |
| 72 | + with: |
| 73 | + driver-opts: | |
| 74 | + image=moby/buildkit:v0.25.1 |
| 75 | +
|
| 76 | + - name: Log in to GitHub Container Registry |
| 77 | + uses: docker/login-action@v3 |
| 78 | + with: |
| 79 | + registry: ${{ env.REGISTRY }} |
| 80 | + username: ${{ github.actor }} |
| 81 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 82 | + |
| 83 | + - name: Docker metadata (latest only on Release) |
| 84 | + id: meta |
| 85 | + uses: docker/metadata-action@v5 |
| 86 | + with: |
| 87 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 88 | + tags: | |
| 89 | + type=raw,value=latest,enable=${{ github.event_name == 'release' && github.event.action == 'published' }} |
| 90 | + type=ref,event=tag |
| 91 | +
|
| 92 | + - name: Compute VERSION |
| 93 | + id: version |
| 94 | + run: | |
| 95 | + V="${{ steps.meta.outputs.version }}" |
| 96 | + if [ -z "$V" ]; then V="${{ github.event.release.tag_name }}"; fi |
| 97 | + echo "VALUE=$V" >> "$GITHUB_OUTPUT" |
| 98 | +
|
| 99 | + - name: Build and push (Release) |
| 100 | + uses: docker/build-push-action@v5 |
| 101 | + with: |
| 102 | + context: . |
| 103 | + platforms: linux/amd64,linux/arm64 |
| 104 | + push: true |
| 105 | + tags: ${{ steps.meta.outputs.tags }} |
| 106 | + labels: ${{ steps.meta.outputs.labels }} |
| 107 | + cache-from: type=gha |
| 108 | + cache-to: type=gha,mode=max |
| 109 | + build-args: | |
| 110 | + VCS_REF=${{ github.sha }} |
| 111 | + VERSION=${{ steps.version.outputs.VALUE }} |
| 112 | +
|
| 113 | + # 3) 手动触发:自动发现“当前最新的 vX.Y.Z”,一次构建推 latest + vX.Y.Z(同 digest) |
| 114 | + manual-publish-latest: |
| 115 | + if: github.event_name == 'workflow_dispatch' |
| 116 | + runs-on: ubuntu-latest |
| 117 | + permissions: |
| 118 | + contents: read |
| 119 | + packages: write |
| 120 | + steps: |
| 121 | + - uses: actions/checkout@v4 |
| 122 | + with: |
| 123 | + fetch-depth: 0 # 保证能拿到 tags 历史 |
| 124 | + |
| 125 | + - name: Force IMAGE_NAME lowercase (GHCR requires lowercase) |
| 126 | + run: echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" |
| 127 | + |
| 128 | + - name: Fetch all tags |
| 129 | + run: | |
| 130 | + git fetch --tags --force --prune --prune-tags |
| 131 | +
|
| 132 | + - name: Resolve latest semver tag (vX.Y.Z) |
| 133 | + id: pick |
| 134 | + shell: bash |
| 135 | + run: | |
| 136 | + TAG=$(git tag -l 'v*' --sort=-v:refname | head -n 1) |
| 137 | + if [[ -z "$TAG" ]]; then |
| 138 | + echo "No tags found matching pattern v*" |
| 139 | + exit 1 |
| 140 | + fi |
| 141 | + if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 142 | + echo "Latest tag is '$TAG' but not pure vX.Y.Z. Adjust logic if you want to allow pre-releases." |
| 143 | + exit 1 |
| 144 | + fi |
| 145 | + VER="${TAG#v}" |
| 146 | + echo "FULL=$TAG" >> "$GITHUB_OUTPUT" |
| 147 | + echo "VER=$VER" >> "$GITHUB_OUTPUT" |
| 148 | + echo "Resolved latest tag: $TAG" |
| 149 | +
|
| 150 | + - uses: docker/setup-qemu-action@v3 |
| 151 | + |
| 152 | + - name: Set up Docker Buildx (pin BuildKit version) |
| 153 | + uses: docker/setup-buildx-action@v3 |
| 154 | + with: |
| 155 | + driver-opts: | |
| 156 | + image=moby/buildkit:v0.25.1 |
| 157 | +
|
| 158 | + - name: Log in to GitHub Container Registry |
| 159 | + uses: docker/login-action@v3 |
| 160 | + with: |
| 161 | + registry: ${{ env.REGISTRY }} |
| 162 | + username: ${{ github.actor }} |
| 163 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 164 | + |
| 165 | + - name: Docker metadata (manual latest + vX.Y.Z) |
| 166 | + id: meta |
| 167 | + uses: docker/metadata-action@v5 |
| 168 | + with: |
| 169 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 170 | + tags: | |
| 171 | + type=raw,value=latest |
| 172 | + type=raw,value=${{ steps.pick.outputs.FULL }} |
| 173 | +
|
| 174 | + - name: Build and push (manual) |
| 175 | + uses: docker/build-push-action@v5 |
| 176 | + with: |
| 177 | + context: . |
| 178 | + platforms: linux/amd64,linux/arm64 |
| 179 | + push: true |
| 180 | + tags: ${{ steps.meta.outputs.tags }} |
| 181 | + labels: ${{ steps.meta.outputs.labels }} |
| 182 | + cache-from: type=gha |
| 183 | + cache-to: type=gha,mode=max |
| 184 | + build-args: | |
| 185 | + VCS_REF=${{ github.sha }} |
| 186 | + VERSION=${{ steps.pick.outputs.VER }} |
0 commit comments