This repository was archived by the owner on Jul 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 307
147 lines (128 loc) · 4.76 KB
/
Copy pathdocker-publish.yml
File metadata and controls
147 lines (128 loc) · 4.76 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
name: Docker Publish
on:
# Tag push:推送 tag 时自动构建并推送 latest + vX.Y.Z
push:
tags:
- 'v*'
# 手动触发:自动发现当前最新 vX.Y.Z,并与 latest 一起发布(同一构建)
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }} # 将在步骤中强制转小写
jobs:
# 1) Tag push:推送 tag 时自动构建并推送 latest + vX.Y.Z(同一构建 -> 同 digest)
build-and-push-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: Docker metadata (auto tag from push)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest
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 (tag)
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 }}
# 2) 手动触发:自动发现"当前最新的 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 }}