-
Notifications
You must be signed in to change notification settings - Fork 0
214 lines (200 loc) · 8.99 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
name: Docker Build
on:
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'
- '.github/**'
- '!.github/workflows/docker-build.yml'
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: ubuntu-latest
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:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v40
with:
files: |
**
- name: Generate build matrices
id: set-matrix
run: |
# Get list of changed files and filter to unique directories containing Dockerfile
CHANGED_DIRS=$(echo "${{ steps.changed-files.outputs.all_changed_files }}" | tr ' ' '\n' | while read -r file; do
dir=$(dirname "$file")
if [[ -f "$dir/Dockerfile" ]]; then
echo "$dir"
fi
done | sort -u)
# Create matrices using jq, filtering by directory structure
echo "base_matrix=$(echo "$CHANGED_DIRS" | grep "^node-base$" | jq -Rsc 'split("\n")[:-1] | {include: map({image_path: .})}')" >> $GITHUB_OUTPUT
echo "clients_matrix=$(echo "$CHANGED_DIRS" | grep "^clients/" | jq -Rsc 'split("\n")[:-1] | {include: map({image_path: .})}')" >> $GITHUB_OUTPUT
# Any directory that's not node-base or clients/ and contains a Dockerfile is a protocol
echo "protocols_matrix=$(echo "$CHANGED_DIRS" | grep -v "^node-base$" | grep -v "^clients/" | jq -Rsc 'split("\n")[:-1] | {include: map({image_path: .})}')" >> $GITHUB_OUTPUT
build-base:
needs: detect-changes
if: ${{ fromJson(needs.detect-changes.outputs.base_matrix).include[0] }}
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.detect-changes.outputs.base_matrix) }}
steps:
- uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@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
- name: Generate build arguments
id: build-args
run: |
# Extract all ARG declarations that end with _IMAGE from Dockerfile
BUILD_ARGS=$(grep "^ARG.*_IMAGE=" ./${{ matrix.image_path }}/Dockerfile | while read -r line; do
# Extract the variable name (everything before =)
var_name=$(echo "$line" | sed 's/ARG \(.*\)=.*/\1/')
# Construct the full image path with version
echo "${var_name}=ghcr.io/blockjoy/${var_name/_IMAGE/}:${{ steps.version.outputs.image_tag }}"
done | tr '\n' ',' | sed 's/,$//')
# Add the standard build args
BUILD_ARGS="${BUILD_ARGS},GRAFANA_LOKI_API_KEY=${{ secrets.GRAFANA_LOKI_API_KEY }},GRAFANA_PROM_API_KEY=${{ secrets.GRAFANA_PROM_API_KEY }}"
echo "args=${BUILD_ARGS}" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push base image
uses: docker/build-push-action@v5
with:
context: ./${{ matrix.image_path }}
push: true
build-args: ${{ steps.build-args.outputs.args }}
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: |
always() &&
needs.build-base.result != 'failure' &&
fromJson(needs.detect-changes.outputs.clients_matrix).include[0]
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.detect-changes.outputs.clients_matrix) }}
steps:
- uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@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="blockjoy-${CLIENT_NAME}"
if [[ -f "${{ matrix.image_path }}/Dockerfile" ]]; then
CLIENT_VERSION=$(grep -E "ENV.*_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
CLIENT_VERSION=${CLIENT_VERSION#v}
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
- name: Generate build arguments
id: build-args
run: |
# Extract all ARG declarations that end with _IMAGE from Dockerfile
BUILD_ARGS=$(grep "^ARG.*_IMAGE=" ./${{ matrix.image_path }}/Dockerfile | while read -r line; do
# Extract the variable name (everything before =)
var_name=$(echo "$line" | sed 's/ARG \(.*\)=.*/\1/')
# Construct the full image path with version
echo "${var_name}=ghcr.io/blockjoy/${var_name/_IMAGE/}:${{ steps.version.outputs.image_tag }}"
done | tr '\n' ',' | sed 's/,$//')
# Add the standard build args
BUILD_ARGS="${BUILD_ARGS},GRAFANA_LOKI_BASICAUTH=${{ secrets.GRAFANA_LOKI_BASICAUTH }},GRAFANA_PROM_BASICAUTH=${{ secrets.GRAFANA_PROM_BASICAUTH }},CLOUDFLARE_API_KEY=${{ secrets.CLOUDFLARE_API_KEY }}"
echo "args=${BUILD_ARGS}" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push client images
uses: docker/build-push-action@v5
with:
context: ./${{ matrix.image_path }}
push: true
build-args: ${{ steps.build-args.outputs.args }}
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, build-clients]
if: |
always() &&
needs.build-clients.result != 'failure' &&
fromJson(needs.detect-changes.outputs.protocols_matrix).include[0]
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.detect-changes.outputs.protocols_matrix) }}
steps:
- uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate version
id: version
run: |
SHA=$(git rev-parse --short HEAD)
IMAGE_NAME=$(basename $(dirname ${{ matrix.image_path }}))"-"$(basename ${{ matrix.image_path }})
echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT
echo "image_tag=${SHA}" >> $GITHUB_OUTPUT
- name: Generate build arguments
id: build-args
run: |
# Extract all ARG declarations that end with _IMAGE from Dockerfile
BUILD_ARGS=$(grep "^ARG.*_IMAGE=" ./${{ matrix.image_path }}/Dockerfile | while read -r line; do
# Extract the variable name (everything before =)
var_name=$(echo "$line" | sed 's/ARG \(.*\)=.*/\1/')
# Construct the full image path with version
echo "${var_name}=ghcr.io/blockjoy/${var_name/_IMAGE/}:${{ steps.version.outputs.image_tag }}"
done | tr '\n' ',' | sed 's/,$//')
# Add the standard build args
BUILD_ARGS="${BUILD_ARGS},GRAFANA_LOKI_BASICAUTH=${{ secrets.GRAFANA_LOKI_BASICAUTH }},GRAFANA_PROM_BASICAUTH=${{ secrets.GRAFANA_PROM_BASICAUTH }},CLOUDFLARE_API_KEY=${{ secrets.CLOUDFLARE_API_KEY }}"
echo "args=${BUILD_ARGS}" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push protocol images
uses: docker/build-push-action@v5
with:
context: ./${{ matrix.image_path }}
push: true
build-args: ${{ steps.build-args.outputs.args }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ghcr.io/blockjoy/${{ steps.version.outputs.image_name }}:${{ steps.version.outputs.image_tag }}