-
Notifications
You must be signed in to change notification settings - Fork 4
266 lines (242 loc) · 10 KB
/
docker-name-version-arch.yml
File metadata and controls
266 lines (242 loc) · 10 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
name: Build multi-arch image
on:
workflow_call:
###
### Variables
###
inputs:
enabled:
description: 'Determines whether this workflow is enabled at all (will run or skip).'
required: true
type: boolean
can_deploy:
description: 'Determines whether this workflow will also deploy (login and push).'
required: true
type: boolean
matrix:
description: 'The version build matrix as JSON string ( list of objects: [{NAME, VERSION[], ARCH[]}] ).'
required: true
type: string
refs:
description: 'The ref build matrix as JSON string (list of git refs to build/deploy).'
required: false
type: string
###
### Secrets
###
secrets:
dockerhub_username:
description: 'The username for Dockerhub.'
required: false
dockerhub_password:
description: 'The password for Dockerhub.'
required: false
jobs:
# -----------------------------------------------------------------------------------------------
# JOB (1/3): CONFIGURE
# -----------------------------------------------------------------------------------------------
configure:
name: Configure
runs-on: ubuntu-latest
outputs:
can_login: ${{ steps.set-login.outputs.can_login }}
has_refs: ${{ steps.set-matrix.outputs.has_refs }}
matrix_build: ${{ steps.set-matrix.outputs.matrix_build }}
matrix_deploy: ${{ steps.set-matrix.outputs.matrix_deploy }}
if: inputs.enabled
steps:
- name: "[Set-Output] Set Docker login capabilities"
id: set-login
shell: bash
run: |
if [ "${{ env.ENV_USER }}" = '' ] || [ "${{ env.ENV_PASS }}" = '' ]; then
echo "can_login=0" >> $GITHUB_OUTPUT
else
echo "can_login=1" >> $GITHUB_OUTPUT
fi
env:
ENV_USER: ${{ secrets.dockerhub_username }}
ENV_PASS: ${{ secrets.dockerhub_password }}
- name: "[Set-Output] Set Build & Deploy Matrix"
id: set-matrix
shell: bash
run: |
if [ "${{ inputs.refs }}" != "" ]; then
MATRIX_BUILD="$( \
jq -M -c \
--argjson refs '${{ inputs.refs }}' \
'map({name:.NAME, version:.VERSION[], arch:.ARCH[], refs:$refs[]})' <<<'${{ inputs.matrix }}' \
)"
MATRIX_DEPLOY="$( \
jq -M -c \
--argjson refs '${{ inputs.refs }}' \
'map({name:.NAME, version:.VERSION[], refs:$refs[]})' <<<'${{ inputs.matrix }}' \
)"
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
echo "has_refs=1" >> $GITHUB_OUTPUT
else
MATRIX_BUILD="$( \
jq -M -c \
'map({name:.NAME, version:.VERSION[], arch:.ARCH[]})' <<<'${{ inputs.matrix }}' \
)"
MATRIX_DEPLOY="$( \
jq -M -c \
'map({name:.NAME, version:.VERSION[]})' <<<'${{ inputs.matrix }}' \
)"
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
echo "has_refs=0" >> $GITHUB_OUTPUT
fi
- name: "[DEBUG] Workflow Inputs"
shell: bash
run: |
echo 'enabled: ${{ inputs.enabled }} '
echo 'can_deploy: ${{ inputs.can_deploy }} '
echo 'matrix: ${{ inputs.matrix }} '
echo 'refs: ${{ inputs.refs }} '
- name: "[DEBUG] Determined Settings"
shell: bash
run: |
echo 'can_login=${{ steps.set-login.outputs.can_login }}'
echo 'has_refs=${{ steps.set-matrix.outputs.has_refs }}'
echo 'matrix_build=${{ steps.set-matrix.outputs.matrix_build }}'
echo 'matrix_deploy=${{ steps.set-matrix.outputs.matrix_deploy }}'
# -----------------------------------------------------------------------------------------------
# JOB (2/3): BUILD
# -----------------------------------------------------------------------------------------------
build:
needs: [configure]
name: Build ${{ matrix.name }}-${{ matrix.version }} (${{ matrix.arch }}) ${{ matrix.refs }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.configure.outputs.matrix_build) }}
if: inputs.enabled
steps:
# ------------------------------------------------------------
# Setup repository
# ------------------------------------------------------------
- name: "[SETUP] Checkout repository (current)"
uses: actions/checkout@v3
with:
fetch-depth: 0
if: needs.configure.outputs.has_refs == 0
- name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})"
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ matrix.refs }}
if: needs.configure.outputs.has_refs != 0
- name: "[SETUP] Setup QEMU environment"
uses: docker/setup-qemu-action@v2
with:
image: tonistiigi/binfmt:latest
platforms: all
- name: "[SETUP] Determine Docker tag"
id: tag
uses: cytopia/docker-tag-action@v0.4.22
# https://github.com/alpinelinux/docker-alpine/issues/98
- name: "[SETUP] Fix Docker IP forwarding"
run: |
sysctl net.ipv4.ip_forward
sudo sysctl -w net.ipv4.ip_forward=1
sudo systemctl restart docker
# ------------------------------------------------------------
# Build
# ------------------------------------------------------------
- name: Pull Base
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make docker-pull-base-image NAME=${{ matrix.name }} VERSION="${{ matrix.version }}" ARCH=${{ matrix.arch }}
- name: Build
uses: cytopia/shell-command-retry-action@v0.1.6
with:
retries: 10
pause: 5
command: |
make build NAME=${{ matrix.name }} VERSION="${{ matrix.version }}" ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
# ------------------------------------------------------------
# Test
# ------------------------------------------------------------
- name: Test
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make test NAME=${{ matrix.name }} VERSION="${{ matrix.version }}" ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
# ------------------------------------------------------------
# Deploy
# ------------------------------------------------------------
- name: Docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.dockerhub_username }}
password: ${{ secrets.dockerhub_password }}
if: needs.configure.outputs.can_login == 1 && inputs.can_deploy
- name: Docker push architecture image
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make push NAME=${{ matrix.name }} VERSION="${{ matrix.version }}" ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
if: needs.configure.outputs.can_login == 1 && inputs.can_deploy
# -----------------------------------------------------------------------------------------------
# JOB (3/3): DEPLOY
# -----------------------------------------------------------------------------------------------
deploy:
needs: [configure, build]
name: Deploy ${{ matrix.name }}-${{ matrix.version }} ${{ matrix.refs }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.configure.outputs.matrix_deploy) }}
if: inputs.enabled && needs.configure.outputs.can_login == 1 && inputs.can_deploy
steps:
# ------------------------------------------------------------
# Setup repository
# ------------------------------------------------------------
- name: "[SETUP] Checkout repository (current)"
uses: actions/checkout@v3
with:
fetch-depth: 0
if: needs.configure.outputs.has_refs == 0
- name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})"
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ matrix.refs }}
if: needs.configure.outputs.has_refs != 0
- name: "[SETUP] Determine Docker tag"
id: tag
uses: cytopia/docker-tag-action@v0.4.22
- name: "[SETUP] Determine manifest arches"
id: manifest
run: |
ARCHES="$( echo '${{ inputs.matrix }}' \
| jq 'group_by(.NAME, .VERSION, .ARCH)' \
| jq 'map({NAME: .[].NAME, VERSION: .[].VERSION[], ARCHES: .[].ARCH|join(",")})' \
| jq '.[] | select(.NAME=="${{ matrix.name }}" and .VERSION=="${{ matrix.version }}") | .ARCHES' \
| jq -c -M \
)"
echo "arches=${ARCHES}" >> $GITHUB_OUTPUT
echo "ARCHES: ${ARCHES}"
# ------------------------------------------------------------
# Deploy
# ------------------------------------------------------------
- name: "[DEPLOY] Login"
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: "[DEPLOY] Create Docker manifest for architectures: ${{ steps.manifest.outputs.arches }}"
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make manifest-create NAME=${{ matrix.name }} VERSION="${{ matrix.version }}" ARCHES=${{ steps.manifest.outputs.arches }} TAG=${{ steps.tag.outputs.docker-tag }}
- name: "[DEPLOY] Publish Docker manifest: ${{ steps.tag.outputs.docker-tag }}"
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make manifest-push NAME=${{ matrix.name }} VERSION="${{ matrix.version }}" TAG=${{ steps.tag.outputs.docker-tag }}