-
-
Notifications
You must be signed in to change notification settings - Fork 366
329 lines (291 loc) · 12.2 KB
/
docker-image-build-dev.yml
File metadata and controls
329 lines (291 loc) · 12.2 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
name: Build & Push - Dev - Split Strategy
# Automatically builds and pushes a multi-platform dev image based on commits involving key files in any branch
on:
push:
branches-ignore:
- 'main'
paths:
- 'empty_library/**'
- 'cps/**'
- 'scripts/**'
- '**/Dockerfile'
- '**/requirements.txt'
- '**/optional-requirements.txt'
- '**/compile_translations.sh'
- 'koreader/**'
- '**/cps.py'
workflow_dispatch:
inputs:
ref:
description: 'Git ref (branch or SHA) to build'
required: false
default: ''
branch:
description: 'Branch name for tagging'
required: false
default: ''
jobs:
# --------------------------------------------------------------------------------
# JOB 1: Build Intel (amd64) on GitHub Runners
# --------------------------------------------------------------------------------
build-amd64:
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
steps:
- name: Set build ref
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.ref }}" ]; then
echo "BUILD_REF=${{ inputs.ref }}" >> $GITHUB_ENV
else
echo "BUILD_REF=${{ github.ref }}" >> $GITHUB_ENV
fi
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.branch }}" ]; then
echo "BRANCH_NAME=${{ inputs.branch }}" >> $GITHUB_ENV
else
echo "BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV
fi
- name: Checkout correct ref
uses: actions/checkout@v4
with:
ref: ${{ env.BUILD_REF }}
# --- 1. Calculate Variables ---
- name: Set repo name to lowercase
run: echo "LOWER_REPO_NAME=$(echo ${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Determine Docker Image Tag
id: tag
run: |
if [[ "${{ env.BRANCH_NAME }}" == "main" ]]; then
echo "IMAGE_TAG=dev" >> $GITHUB_ENV
else
sanitized_branch=$(echo "${{ env.BRANCH_NAME }}" | sed 's/[^a-zA-Z0-9.-]/-/g')
echo "IMAGE_TAG=dev-${sanitized_branch}" >> $GITHUB_ENV
fi
# --- 2. Logins ---
- name: DockerHub Login
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PA_TOKEN }}
- name: GitHub Container Registry Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# --- 3. Build & Push (Digest Only) ---
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
provenance: false
context: .
file: ./Dockerfile
# We build AMD64 here natively
platforms: linux/amd64
# CACHE STRATEGY: Use GitHub Actions Cache (Ephemeral Runner)
cache-from: type=gha
cache-to: type=gha,mode=max
outputs: |
type=image,name=${{ secrets.DOCKERHUB_USERNAME }}/calibre-web-automated,push-by-digest=true,name-canonical=true,push=true
type=image,name=ghcr.io/${{ github.repository_owner }}/${{ env.LOWER_REPO_NAME }},push-by-digest=true,name-canonical=true,push=true
build-args: |
BUILD_DATE=${{ github.event.repository.updated_at }}
VERSION=${{ vars.CURRENT_DEV_VERSION }}-DEV_BUILD-${{ env.IMAGE_TAG }}-${{ vars.CURRENT_DEV_BUILD_NUM }}
# --- 4. Export Digest for Merge Job ---
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-amd64
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
# --------------------------------------------------------------------------------
# JOB 2: Build ARM (arm64 Only) on Oracle Runner
# --------------------------------------------------------------------------------
build-arm:
# Use your self-hosted runner labels here
runs-on: [self-hosted, linux, ARM64]
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
steps:
- name: Set build ref
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.ref }}" ]; then
echo "BUILD_REF=${{ inputs.ref }}" >> $GITHUB_ENV
else
echo "BUILD_REF=${{ github.ref }}" >> $GITHUB_ENV
fi
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.branch }}" ]; then
echo "BRANCH_NAME=${{ inputs.branch }}" >> $GITHUB_ENV
else
echo "BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV
fi
- name: Checkout correct ref
uses: actions/checkout@v4
with:
ref: ${{ env.BUILD_REF }}
# --- 1. Calculate Variables ---
- name: Set repo name to lowercase
run: echo "LOWER_REPO_NAME=$(echo ${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Determine Docker Image Tag
id: tag
run: |
if [[ "${{ env.BRANCH_NAME }}" == "main" ]]; then
echo "IMAGE_TAG=dev" >> $GITHUB_ENV
else
sanitized_branch=$(echo "${{ env.BRANCH_NAME }}" | sed 's/[^a-zA-Z0-9.-]/-/g')
echo "IMAGE_TAG=dev-${sanitized_branch}" >> $GITHUB_ENV
fi
# --- 2. Logins ---
- name: DockerHub Login
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PA_TOKEN }}
- name: GitHub Container Registry Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# --- 3. Build & Push (Digest Only) ---
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
provenance: false
context: .
file: ./Dockerfile
# We build ARM64 here natively
platforms: linux/arm64
# CACHE STRATEGY: Use Local Disk Cache (Persistent Runner)
# Saves bandwidth and time by storing cache directly on the Oracle VM
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
outputs: |
type=image,name=${{ secrets.DOCKERHUB_USERNAME }}/calibre-web-automated,push-by-digest=true,name-canonical=true,push=true
type=image,name=ghcr.io/${{ github.repository_owner }}/${{ env.LOWER_REPO_NAME }},push-by-digest=true,name-canonical=true,push=true
build-args: |
BUILD_DATE=${{ github.event.repository.updated_at }}
VERSION=${{ vars.CURRENT_DEV_VERSION }}-DEV_BUILD-${{ env.IMAGE_TAG }}-${{ vars.CURRENT_DEV_BUILD_NUM }}
# --- 4. Rotate Cache (Prevent Infinite Growth) ---
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
# --- 5. Export Digest for Merge Job ---
- name: Export digest
run: |
rm -rf /tmp/digests
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-arm
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
# --- 6. Janitor: Clean up Docker Garbage ---
- name: Prune Docker Garbage
if: always() # Run even if build fails to keep disk clean
run: |
echo "Pruning dangling images..."
docker image prune -f
echo "Pruning old build cache (older than 1 week)..."
docker builder prune -f --filter "until=168h"
# --------------------------------------------------------------------------------
# JOB 3: Merge & Tag (Runs on GitHub)
# --------------------------------------------------------------------------------
merge:
runs-on: ubuntu-latest
needs: [build-amd64, build-arm]
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
steps:
# --- 1. Calculate Variables ---
- name: Set build ref
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.ref }}" ]; then
echo "BUILD_REF=${{ inputs.ref }}" >> $GITHUB_ENV
else
echo "BUILD_REF=${{ github.ref }}" >> $GITHUB_ENV
fi
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.branch }}" ]; then
echo "BRANCH_NAME=${{ inputs.branch }}" >> $GITHUB_ENV
else
echo "BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV
fi
- name: Checkout correct ref
uses: actions/checkout@v4
with:
ref: ${{ env.BUILD_REF }}
- name: Set repo name to lowercase
run: echo "LOWER_REPO_NAME=$(echo ${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Determine Docker Image Tags
id: tag
run: |
# 1. Determine the "Floating" Tag (e.g., 'dev' or 'dev-featurebranch')
if [[ "${{ env.BRANCH_NAME }}" == "main" ]]; then
echo "FLOAT_TAG=dev" >> $GITHUB_ENV
else
sanitized_branch=$(echo "${{ env.BRANCH_NAME }}" | sed 's/[^a-zA-Z0-9.-]/-/g')
echo "FLOAT_TAG=dev-${sanitized_branch}" >> $GITHUB_ENV
fi
# 2. Determine the "Unique" Tag (e.g., 'dev-371')
# We use the build number that was passed to the build jobs
# Note: We append the build number to the 'dev' prefix to keep them grouped
echo "UNIQUE_TAG=dev-${{ vars.CURRENT_DEV_BUILD_NUM }}" >> $GITHUB_ENV
# --- 2. Logins ---
- name: DockerHub Login
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PA_TOKEN }}
- name: GitHub Container Registry Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# --- 3. Merge Digests ---
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
# 1. Push to DockerHub (Both Tags)
docker buildx imagetools create \
-t ${{ secrets.DOCKERHUB_USERNAME }}/calibre-web-automated:${{ env.FLOAT_TAG }} \
-t ${{ secrets.DOCKERHUB_USERNAME }}/calibre-web-automated:${{ env.UNIQUE_TAG }} \
$(printf '${{ secrets.DOCKERHUB_USERNAME }}/calibre-web-automated@sha256:%s ' *)
# 2. Push to GHCR (Both Tags)
docker buildx imagetools create \
-t ghcr.io/${{ github.repository_owner }}/${{ env.LOWER_REPO_NAME }}:${{ env.FLOAT_TAG }} \
-t ghcr.io/${{ github.repository_owner }}/${{ env.LOWER_REPO_NAME }}:${{ env.UNIQUE_TAG }} \
$(printf 'ghcr.io/${{ github.repository_owner }}/${{ env.LOWER_REPO_NAME }}@sha256:%s ' *)
# --- 4. Increment Build Number (Last Step) ---
- name: Increment dev build number
uses: action-pack/increment@v2
id: increment
with:
name: 'CURRENT_DEV_BUILD_NUM'
token: ${{ secrets.DEV_BUILD_NUM_INCREMENTOR_TOKEN }}