Repository navigation
224 lines (197 loc) · 7.3 KB
/
Copy pathdocker.yml
File metadata and controls
224 lines (197 loc) · 7.3 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
name: docker-build-push
on:
push:
tags:
- "v*.*.*"
- "v*"
permissions:
contents: read
id-token: write
env:
REGISTRY_IMAGE: ${{ vars.DOCKER_USER }}/mega2
jobs:
build:
strategy:
fail-fast: true
matrix:
include:
- platform: linux/amd64
runner: ubuntu-24.04
arch: X64
slug: amd64
- platform: linux/arm64
runner: ubuntu-24.04-arm
arch: ARM64
slug: arm64
runs-on: ${{ matrix.runner }}
steps:
- name: Validate stable release tag
shell: bash
env:
REF_NAME: ${{ github.ref_name }}
run: |
if [[ ! "$REF_NAME" =~ ^v(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)$ ]]; then
echo "Docker publication requires a stable v<major>.<minor>.<patch> tag" >&2
exit 1
fi
- name: Check native runner architecture
env:
EXPECTED_ARCH: ${{ matrix.arch }}
run: test "$RUNNER_ARCH" = "$EXPECTED_ARCH"
- name: Free space for the Rust build
run: |
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /usr/local/share/powershell
df -h /
- name: Checkout
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKER_USER }}
password: ${{ secrets.DOCKER_PAT }}
- name: Extract image labels
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
flavor: latest=false
tags: |
type=ref,event=tag
type=semver,pattern={{version}}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
provenance: true
- name: Export digest
shell: bash
env:
DIGEST: ${{ steps.build.outputs.digest }}
run: |
[[ "$DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]]
mkdir -p "$RUNNER_TEMP/digests"
touch "$RUNNER_TEMP/digests/${DIGEST#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.slug }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
docker:
needs: build
if: ${{ !cancelled() }}
runs-on: ubuntu-24.04
concurrency:
group: mega2-docker-publish
queue: max
steps:
- name: Require both native builds
env:
BUILD_RESULT: ${{ needs.build.result }}
run: test "$BUILD_RESULT" = success
- name: Checkout tags for channel guard
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Decide mutable tags
id: channels
shell: bash
env:
REF_NAME: ${{ github.ref_name }}
run: |
python3 - <<'PY' >> "$GITHUB_OUTPUT"
import os
import re
import subprocess
def version(tag):
match = re.fullmatch(r"v(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)", tag)
return tuple(map(int, match.groups())) if match else None
current = version(os.environ["REF_NAME"])
if current is None:
raise SystemExit("Docker publication requires a stable v<major>.<minor>.<patch> tag")
tags = subprocess.check_output(["git", "tag", "--list", "v*"], text=True).splitlines()
versions = [parsed for tag in tags if (parsed := version(tag)) is not None]
if current is not None and os.environ["REF_NAME"] not in tags:
raise SystemExit("Current release tag was not fetched")
latest = current is not None and current == max(versions)
minor_versions = [item for item in versions if current is not None and item[:2] == current[:2]]
minor_latest = current is not None and current == max(minor_versions)
print(f"latest={str(latest).lower()}")
print(f"minor_latest={str(minor_latest).lower()}")
print(f"minor_tag={current[0]}.{current[1]}" if current is not None else "minor_tag=")
PY
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKER_USER }}
password: ${{ secrets.DOCKER_PAT }}
- name: Extract image tags
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
flavor: latest=false
tags: |
type=ref,event=tag
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}},enable=${{ steps.channels.outputs.minor_latest }}
type=raw,value=latest,enable=${{ steps.channels.outputs.latest }}
- name: Download digests
uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Publish multi-platform manifest
shell: bash
env:
META_TAGS: ${{ steps.meta.outputs.tags }}
REF_NAME: ${{ github.ref_name }}
MINOR_TAG: ${{ steps.channels.outputs.minor_tag }}
PUBLISH_MINOR: ${{ steps.channels.outputs.minor_latest }}
PUBLISH_LATEST: ${{ steps.channels.outputs.latest }}
run: |
shopt -s nullglob
digest_files=("$RUNNER_TEMP"/digests/*)
if (( ${#digest_files[@]} != 2 )); then
echo "Expected exactly two architecture digests" >&2
exit 1
fi
sources=()
for file in "${digest_files[@]}"; do
digest="${file##*/}"
if [[ ! "$digest" =~ ^[0-9a-f]{64}$ ]]; then
echo "Invalid sha256 digest filename" >&2
exit 1
fi
sources+=("$REGISTRY_IMAGE@sha256:$digest")
done
immutable_tags=()
while IFS= read -r tag; do
[[ -n "$tag" ]] || continue
if [[ "$tag" != "$REGISTRY_IMAGE:latest" && "$tag" != "$REGISTRY_IMAGE:$MINOR_TAG" ]]; then
immutable_tags+=(--tag "$tag")
fi
done <<< "$META_TAGS"
if (( ${#immutable_tags[@]} == 0 )); then
echo "No immutable version tags were generated" >&2
exit 1
fi
docker buildx imagetools create "${immutable_tags[@]}" "${sources[@]}"
docker buildx imagetools inspect --raw "$REGISTRY_IMAGE:$REF_NAME" |
jq -e '[.manifests[] | select(.platform.os == "linux") | .platform.architecture] | sort == ["amd64", "arm64"]'
if [[ "$PUBLISH_MINOR" == true ]]; then
docker buildx imagetools create --tag "$REGISTRY_IMAGE:$MINOR_TAG" "$REGISTRY_IMAGE:$REF_NAME"
fi
if [[ "$PUBLISH_LATEST" == true ]]; then
docker buildx imagetools create --tag "$REGISTRY_IMAGE:latest" "$REGISTRY_IMAGE:$REF_NAME"
fi