-
Notifications
You must be signed in to change notification settings - Fork 0
351 lines (329 loc) · 16.3 KB
/
docker-build.yml
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
name: Docker Build
on:
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'
- '.github/**'
- '!.github/workflows/docker-build.yml'
- 'docs/**'
- '.gitignore'
- 'LICENSE'
permissions:
contents: read
packages: write
pull-requests: read
id-token: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
detect-changes:
runs-on: self-hosted
outputs:
base_matrix: ${{ steps.set-matrix.outputs.base_matrix }}
clients_matrix: ${{ steps.set-matrix.outputs.clients_matrix }}
protocols_matrix: ${{ steps.set-matrix.outputs.protocols_matrix }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 0 # Fetch all history for all tags and branches
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@bab30c2299617f6615ec02a68b9a40d10bd21366 # v45
with:
since_last_remote_commit: true
fetch_depth: 0
- name: List all changed files
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
for file in ${ALL_CHANGED_FILES}; do
echo "$file was changed"
done
- name: Debug changed files outputs
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
echo "Debug outputs from changed-files action:"
echo "Changed files:"
echo "$ALL_CHANGED_FILES"
- name: Generate build matrices
id: set-matrix
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
# First find all Docker build contexts (directories containing Dockerfile)
DOCKER_DIRS=$(find . -name Dockerfile -exec dirname {} \; | sed 's|^./||' | sort)
echo "Found Docker directories:"
echo "$DOCKER_DIRS"
# Get list of changed files
echo "Changed files in this commit:"
echo "$ALL_CHANGED_FILES"
# For each changed file, find which Docker directory it belongs to
CHANGED_DIRS=$(for file in ${ALL_CHANGED_FILES}; do
echo "Checking file: $file"
echo "$DOCKER_DIRS" | while read -r docker_dir; do
if [[ "$file" =~ ^"$docker_dir"(/|$) ]]; then
echo "$docker_dir"
fi
done
done | sort -u)
echo "Changed directories:"
echo "$CHANGED_DIRS"
# Create matrices using jq, filtering by directory structure
BASE=$(echo "$CHANGED_DIRS" | grep "^node-base$" || true)
CLIENTS=$(echo "$CHANGED_DIRS" | grep "^clients/" || true)
PROTOCOLS=$(echo "$CHANGED_DIRS" | grep "^[^/]\+/[^/]\+-[^/]\+$" || true)
echo "Base dirs: $BASE"
echo "Client dirs: $CLIENTS"
echo "Protocol dirs: $PROTOCOLS"
# Only output matrices if we have changes
if [[ -n "$BASE" ]]; then
echo "base_matrix=$(echo "$BASE" | jq -Rsc 'split("\n")[:-1] | {include: map({image_path: .})}')" >> $GITHUB_OUTPUT
else
echo 'base_matrix={"include":[]}' >> $GITHUB_OUTPUT
fi
if [[ -n "$CLIENTS" ]]; then
echo "clients_matrix=$(echo "$CLIENTS" | jq -Rsc 'split("\n")[:-1] | {include: map({image_path: .})}')" >> $GITHUB_OUTPUT
else
echo 'clients_matrix={"include":[]}' >> $GITHUB_OUTPUT
fi
if [[ -n "$PROTOCOLS" ]]; then
echo "protocols_matrix=$(echo "$PROTOCOLS" | jq -Rsc 'split("\n")[:-1] | {include: map({image_path: .})}')" >> $GITHUB_OUTPUT
else
echo 'protocols_matrix={"include":[]}' >> $GITHUB_OUTPUT
fi
build-base:
needs: detect-changes
if: ${{ fromJson(needs.detect-changes.outputs.base_matrix).include[0] }}
runs-on: self-hosted
strategy:
matrix: ${{ fromJson(needs.detect-changes.outputs.base_matrix) }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate version
id: version
run: |
SHA=$(git rev-parse --short HEAD)
echo "image_name=node-base" >> $GITHUB_OUTPUT
echo "image_tag=${SHA}" >> $GITHUB_OUTPUT
echo "sha=${SHA}" >> $GITHUB_OUTPUT
- name: Generate build contexts
id: build-contexts
run: |
# First, find all directories containing Dockerfiles to build our valid image list
VALID_IMAGES=$(find . -name Dockerfile -exec dirname {} \; | while read dir; do
# Convert directory path to image name (e.g., ./ethereum/ethereum-erigon -> ethereum-erigon)
basename "$dir" | tr '[:upper:]' '[:lower:]'
# For protocol directories, also add the protocol-client format
if [[ "$dir" =~ ^./[^/]+/[^/]+ ]]; then
echo "$dir" | sed 's|^./\([^/]\+\)/\([^/]\+\)|\1-\2|' | tr '[:upper:]' '[:lower:]'
fi
done | sort -u)
# Now extract FROM directives and filter against our valid images
BUILD_CONTEXTS=$(grep -h "^FROM.*\${.*_IMAGE}" ./${{ matrix.image_path }}/Dockerfile | while read -r line; do
# Extract variable name from FROM line
var_name=$(echo "$line" | grep -o '\${[^}]*}' | tr -d '${}\n')
# Extract image name and remove any default tag
image=$(grep "^ARG ${var_name}=" ./${{ matrix.image_path }}/Dockerfile | cut -d'=' -f2 | cut -d':' -f1)
# Check if this image is in our valid image list
if echo "$VALID_IMAGES" | grep -q "^${image}$"; then
# Use short name for context key but specify it as a docker-image context with SHA tag
echo "${image}=docker-image://ghcr.io/blockjoy/${image}:${{ steps.version.outputs.sha }}"
fi
done | sort -u | tr '\n' ',' | sed 's/,$//')
echo "contexts=${BUILD_CONTEXTS}" >> $GITHUB_OUTPUT
echo "Generated build contexts: ${BUILD_CONTEXTS}"
echo "Valid images: ${VALID_IMAGES}"
echo "Current image path: '${{ matrix.image_path }}'"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3
- name: Build and push base image
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
with:
context: ./${{ matrix.image_path }}
push: true
build-args: |
GRAFANA_LOKI_API_KEY=${{ secrets.GRAFANA_LOKI_API_KEY }}
GRAFANA_PROM_API_KEY=${{ secrets.GRAFANA_PROM_API_KEY }}
build-contexts: ${{ steps.build-contexts.outputs.contexts }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ghcr.io/blockjoy/${{ steps.version.outputs.image_name }}:${{ steps.version.outputs.image_tag }}
build-clients:
needs: [detect-changes, build-base]
if: |
fromJson(needs.detect-changes.outputs.clients_matrix).include[0] &&
(needs.build-base.result == 'success' || needs.build-base.result == 'skipped' || needs.build-base.result == null)
runs-on: self-hosted
strategy:
matrix: ${{ fromJson(needs.detect-changes.outputs.clients_matrix) }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate version
id: version
run: |
SHA=$(git rev-parse --short HEAD)
CLIENT_NAME=$(basename ${{ matrix.image_path }})
IMAGE_NAME="${CLIENT_NAME}"
if [[ "${{ matrix.image_path }}" =~ /exec/ ]]; then
# Only extract version for execution clients
CLIENT_UPPER=$(echo "${CLIENT_NAME}" | tr '[:lower:]' '[:upper:]')
CLIENT_VERSION=$(grep -E "ENV ${CLIENT_UPPER}_VERSION=[[:space:]]*v?[0-9]+\.[0-9]+\.[0-9]+[-.a-zA-Z0-9]*" "${{ matrix.image_path }}/Dockerfile" | grep -oE "v?[0-9]+\.[0-9]+\.[0-9]+[-.a-zA-Z0-9]*")
if [[ ! -z "$CLIENT_VERSION" ]]; then
# Remove 'v' prefix and ensure version is Docker-compatible (only alphanumeric and . - _)
CLIENT_VERSION=${CLIENT_VERSION#v}
CLIENT_VERSION=$(echo "$CLIENT_VERSION" | tr -cd '[:alnum:].-')
IMAGE_TAG="${CLIENT_VERSION}-${SHA}"
else
IMAGE_TAG="${SHA}"
fi
else
IMAGE_TAG="${SHA}"
fi
echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "sha=${SHA}" >> $GITHUB_OUTPUT
- name: Generate build contexts
id: build-contexts
run: |
# First, find all directories containing Dockerfiles to build our valid image list
VALID_IMAGES=$(find . -name Dockerfile -exec dirname {} \; | while read dir; do
# Convert directory path to image name (e.g., ./ethereum/ethereum-erigon -> ethereum-erigon)
basename "$dir" | tr '[:upper:]' '[:lower:]'
# For protocol directories, also add the protocol-client format
if [[ "$dir" =~ ^./[^/]+/[^/]+ ]]; then
echo "$dir" | sed 's|^./\([^/]\+\)/\([^/]\+\)|\1-\2|' | tr '[:upper:]' '[:lower:]'
fi
done | sort -u)
# Now extract FROM directives and filter against our valid images
BUILD_CONTEXTS=$(grep -h "^FROM.*\${.*_IMAGE}" ./${{ matrix.image_path }}/Dockerfile | while read -r line; do
# Extract variable name from FROM line
var_name=$(echo "$line" | grep -o '\${[^}]*}' | tr -d '${}\n')
# Extract image name and remove any default tag
image=$(grep "^ARG ${var_name}=" ./${{ matrix.image_path }}/Dockerfile | cut -d'=' -f2 | cut -d':' -f1)
# Check if this image is in our valid image list
if echo "$VALID_IMAGES" | grep -q "^${image}$"; then
# Use short name for context key but specify it as a docker-image context with SHA tag
echo "${image}=docker-image://ghcr.io/blockjoy/${image}:${{ steps.version.outputs.sha }}"
fi
done | sort -u | tr '\n' ',' | sed 's/,$//')
echo "contexts=${BUILD_CONTEXTS}" >> $GITHUB_OUTPUT
echo "Generated build contexts: ${BUILD_CONTEXTS}"
echo "Valid images: ${VALID_IMAGES}"
echo "Current image path: '${{ matrix.image_path }}'"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3
- name: Build and push client images
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
with:
context: ./${{ matrix.image_path }}
push: true
build-args: |
GRAFANA_LOKI_BASICAUTH=${{ secrets.GRAFANA_LOKI_BASICAUTH }}
GRAFANA_PROM_BASICAUTH=${{ secrets.GRAFANA_PROM_BASICAUTH }}
CLOUDFLARE_API_KEY=${{ secrets.CLOUDFLARE_API_KEY }}
BASE_IMAGE=node-base:${{ steps.version.outputs.sha }}
build-contexts: ${{ steps.build-contexts.outputs.contexts }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ghcr.io/blockjoy/${{ steps.version.outputs.image_name }}:${{ steps.version.outputs.image_tag }}
build-protocols:
needs: [detect-changes]
if: fromJson(needs.detect-changes.outputs.protocols_matrix).include[0]
runs-on: self-hosted
strategy:
matrix: ${{ fromJson(needs.detect-changes.outputs.protocols_matrix) }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate version
id: version
run: |
SHA=$(git rev-parse --short HEAD)
if [[ -n "${{ matrix.image_path }}" ]]; then
# Only try to generate image name if matrix path is not empty
IMAGE_NAME=$(basename $(dirname "${{ matrix.image_path }}"))"-"$(basename "${{ matrix.image_path }}")
echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT
else
echo "::error ::No image path provided in matrix"
exit 1
fi
echo "image_tag=${SHA}" >> $GITHUB_OUTPUT
echo "sha=${SHA}" >> $GITHUB_OUTPUT
- name: Generate build contexts
id: build-contexts
run: |
# First, find all directories containing Dockerfiles to build our valid image list
VALID_IMAGES=$(find . -name Dockerfile -exec dirname {} \; | while read dir; do
# Convert directory path to image name (e.g., ./ethereum/ethereum-erigon -> ethereum-erigon)
basename "$dir" | tr '[:upper:]' '[:lower:]'
# For protocol directories, also add the protocol-client format
if [[ "$dir" =~ ^./[^/]+/[^/]+ ]]; then
echo "$dir" | sed 's|^./\([^/]\+\)/\([^/]\+\)|\1-\2|' | tr '[:upper:]' '[:lower:]'
fi
done | sort -u)
# Now extract FROM directives and filter against our valid images
BUILD_CONTEXTS=$(grep -h "^FROM.*\${.*_IMAGE}" ./${{ matrix.image_path }}/Dockerfile | while read -r line; do
# Extract variable name from FROM line
var_name=$(echo "$line" | grep -o '\${[^}]*}' | tr -d '${}\n')
# Extract image name and remove any default tag
image=$(grep "^ARG ${var_name}=" ./${{ matrix.image_path }}/Dockerfile | cut -d'=' -f2 | cut -d':' -f1)
# Check if this image is in our valid image list
if echo "$VALID_IMAGES" | grep -q "^${image}$"; then
# Use short name for context key but specify it as a docker-image context with SHA tag
echo "${image}=docker-image://ghcr.io/blockjoy/${image}:${{ steps.version.outputs.sha }}"
fi
done | sort -u | tr '\n' ',' | sed 's/,$//')
echo "contexts=${BUILD_CONTEXTS}" >> $GITHUB_OUTPUT
echo "Generated build contexts: ${BUILD_CONTEXTS}"
echo "Valid images: ${VALID_IMAGES}"
echo "Current image path: '${{ matrix.image_path }}'"
- name: Generate build args
id: build-args
run: |
# Extract all *_IMAGE args from Dockerfile with their specified tags
IMAGE_ARGS=$(grep "^ARG.*_IMAGE=" ./${{ matrix.image_path }}/Dockerfile | while read -r line; do
var_name=$(echo "$line" | cut -d'=' -f1 | cut -d' ' -f2)
# Use the full image reference from the Dockerfile, including its tag
image_ref=$(echo "$line" | cut -d'=' -f2)
echo "${var_name}=${image_ref}"
done | tr '\n' '\n')
echo "image_args<<EOF" >> $GITHUB_OUTPUT
echo "$IMAGE_ARGS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3
- name: Build and push protocol images
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
with:
context: ./${{ matrix.image_path }}
push: true
build-args: |
GRAFANA_LOKI_BASICAUTH=${{ secrets.GRAFANA_LOKI_BASICAUTH }}
GRAFANA_PROM_BASICAUTH=${{ secrets.GRAFANA_PROM_BASICAUTH }}
CLOUDFLARE_API_KEY=${{ secrets.CLOUDFLARE_API_KEY }}
${{ steps.build-args.outputs.image_args }}
build-contexts: ${{ steps.build-contexts.outputs.contexts }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ghcr.io/blockjoy/${{ steps.version.outputs.image_name }}:${{ steps.version.outputs.image_tag }}