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