-
Notifications
You must be signed in to change notification settings - Fork 3
306 lines (277 loc) · 13 KB
/
docker-multistage-configure.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
---
name: "Configure multistage docker images (multi -flavours, -versions, -architectures)"
on:
workflow_call:
###
### Input Variables
###
inputs:
versions:
description: 'The JSON string for versions. ( list of objects: [{NAME, VERSION[], ARCH[]}] )'
required: true
type: string
refs:
description: 'The JSON string for refs. ( object: {BRANCH, NUM_LATEST_TAGS} )'
required: true
type: string
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 can deploy (login and push).'
required: true
type: boolean
fields_build:
description: 'The JSON string for build fields to extract'
required: false
type: string
default: '[{"VERSION":"VERSION"}, {"FLAVOUR":"FLAVOUR"}, {"ARCH":"ARCH"}]'
fields_deploy:
description: 'The JSON string for deploy fields to extract'
required: false
type: string
default: '[{"VERSION":"VERSION"}, {"FLAVOUR":"FLAVOUR"}]'
###
### Input Secrets
###
secrets:
dockerhub_username:
description: 'The username for Dockerhub.'
required: false
dockerhub_password:
description: 'The password for Dockerhub.'
required: false
###
### Outputs
###
outputs:
# Repeat input variables
versions:
description: "(string) Copied from inputs: The JSON string for versions."
value: ${{ jobs.configure.outputs.versions }}
refs:
description: "(string) Copied from inputs: The JSON string for refs."
value: ${{ jobs.configure.outputs.refs }}
# Determined settings (flags)
can_login:
description: "(boolean) Can we login to Dockerhub?"
value: ${{ jobs.configure.outputs.can_login }}
can_push:
description: "(boolean) Can we push to Dockerhub?"
value: ${{ jobs.configure.outputs.can_push }}
# Determined settings (values)
has_refs:
description: "(string) 'true' or 'false' Do we have refs to build?"
value: ${{ jobs.configure.outputs.has_refs }}
matrix_build:
description: "(string) The determined JSON string build matrix."
value: ${{ jobs.configure.outputs.matrix_build }}
matrix_deploy:
description: "(string) The determined JSON string deploy matrix."
value: ${{ jobs.configure.outputs.matrix_deploy }}
artifact_prefix:
description: "(string) The determined unique artifact prefix."
value: ${{ jobs.configure.outputs.artifact_prefix }}
jobs:
# -----------------------------------------------------------------------------------------------
# JOB (1/3): CONFIGURE
# -----------------------------------------------------------------------------------------------
configure:
name: Configure
if: ${{ inputs.enabled }}
runs-on: ubuntu-latest
###
### Outputs
###
outputs:
# Copied from inputs
versions: ${{ inputs.versions }}
refs: ${{ inputs.refs }}
# Flags
can_login: ${{ steps.set-login.outputs.can_login }}
can_push: ${{ steps.set-push.outputs.can_push }}
# Settings
has_refs: ${{ steps.set-refs.outputs.has_refs }}
artifact_prefix: ${{ steps.set-artifact-prefix.outputs.prefix }}
# Values
matrix_build: ${{ steps.set-matrix.outputs.matrix_build }}
matrix_deploy: ${{ steps.set-matrix.outputs.matrix_deploy }}
###
### Steps
###
steps:
# ------------------------------------------------------------
# Set flags
# ------------------------------------------------------------
###
### Can we login to Dockerhub?
###
- name: "[Set-Output] can_login (Set Docker login capabilities)"
id: set-login
shell: bash
run: |
if [ "${{ env.ENV_USER }}" = '' ] || [ "${{ env.ENV_PASS }}" = '' ]; then
echo "can_login=false" >> $GITHUB_OUTPUT
else
echo "can_login=true" >> $GITHUB_OUTPUT
fi
env:
ENV_USER: ${{ secrets.dockerhub_username }}
ENV_PASS: ${{ secrets.dockerhub_password }}
###
### Can we push to Dockerhub?
###
- name: "[Set-Output] can_push (Set Docker push capabilities)"
id: set-push
shell: bash
run: |
if [ "${{ steps.set-login.outputs.can_login }}" = "true" ] && [ "${{ inputs.can_deploy }}" = "true" ]; then
echo "can_push=true" >> $GITHUB_OUTPUT
else
echo "can_push=false" >> $GITHUB_OUTPUT
fi
# ------------------------------------------------------------
# Set values
# ------------------------------------------------------------
###
### Do we have refs to build against?
###
- name: "Evaluate Refs (branches and latest tags)"
id: eval-refs
shell: bash
run: |
DEFAULT_BRANCH="$( echo '${{ inputs.refs }}' | jq -M -c -r '.DEFAULT_BRANCH' )"
BRANCHES="$( echo '${{ inputs.refs }}' | jq -M -c -r '.BRANCHES' )"
NUM_LATEST_TAGS="$( echo '${{ inputs.refs }}' | jq -M -c -r '.NUM_LATEST_TAGS' )"
echo "default_branch=${DEFAULT_BRANCH}" >> $GITHUB_OUTPUT
echo "branches=${BRANCHES}" >> $GITHUB_OUTPUT
echo "num_latest_tags=${NUM_LATEST_TAGS}" >> $GITHUB_OUTPUT
echo "DEFAULT_BRANCH=${DEFAULT_BRANCH}"
echo "BRANCHES=${BRANCHES}"
echo "NUM_LATEST_TAGS=${NUM_LATEST_TAGS}"
###
### Set Refs
###
- name: "[Set-Output] has_refs and ref-matrix"
id: set-refs
uses: cytopia/[email protected]
with:
repository_default_branch: ${{ steps.eval-refs.outputs.default_branch }}
branches: ${{ steps.eval-refs.outputs.branches }}
num_latest_tags: ${{ steps.eval-refs.outputs.num_latest_tags }}
# Only use refs if the job is a schedule or if the job is schedule and was triggered manually
disable_refs: ${{ !(github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') }}
###
### Unique Artifact prefix
###
- name: "[Set-Output] artifact_prefix (unique Artifact prefix)"
id: set-artifact-prefix
shell: bash
run: |
PRE_RUN="$( echo '${{ github.run_id }}' )"
PRE_SHA="$( echo '${{ github.sha }}' | md5sum | head -c 10 )"
echo "prefix=${PRE_RUN}-${PRE_SHA}" >> $GITHUB_OUTPUT
###
### Set Build and Deploy Matrix
###
- name: "[Set-Output] matrix_build and matrix_deploy (Build Matrix & Deploy Matrix)"
id: set-matrix
shell: bash
run: |
FIELDS_BUILD="$( echo '${{ inputs.fields_build }}' | jq -M -c )"
FIELDS_DEPLOY="$( echo '${{ inputs.fields_deploy }}' | jq -M -c )"
VERSIONS="$( echo '${{ inputs.versions }}' | jq -M -c )"
if [ "${{ steps.set-refs.outputs.has_refs }}" = "true" ]; then
REFS="$( echo '${{ steps.set-refs.outputs.matrix }}' | jq -M -c )"
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ARCH:. ARCH[], ...})
INNER_STRING="$( echo "${FIELDS_BUILD}" \
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
| jq -r '. | join(", ")' )"
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}, REFS: ${REFS}[]})'" )"
MATRIX_BUILD="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ....})
INNER_STRING="$( echo "${FIELDS_DEPLOY}" \
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
| jq -r '. | join(", ")' )"
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}, REFS: ${REFS}[]})'" )"
MATRIX_DEPLOY="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
else
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ARCH:. ARCH[], ...})
INNER_STRING="$( echo "${FIELDS_BUILD}" \
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
| jq -r '. | join(", ")' )"
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}})'" )"
MATRIX_BUILD="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ....})
INNER_STRING="$( echo "${FIELDS_DEPLOY}" \
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
| jq -r '. | join(", ")' )"
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}})'" )"
MATRIX_DEPLOY="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
fi
# ------------------------------------------------------------
# Debug
# ------------------------------------------------------------
- name: "[DEBUG] Show GitHub Context"
shell: bash
run: |
echo 'Context | Value'
echo '-------------------------------------|-----------------------------------'
echo 'github.actor | ${{ github.actor }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.repository_owner | ${{ github.repository_owner }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.event.pull_request.user.login | ${{ github.event.pull_request.user.login }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.event_name | ${{ github.event_name }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.ref | ${{ github.ref }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.ref_name | ${{ github.ref_name }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.head_ref | ${{ github.head_ref }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.base_ref | ${{ github.base_ref }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.ref_type | ${{ github.ref_type }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.repository | ${{ github.repository }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.action | ${{ github.action }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.action_ref | ${{ github.action_ref }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.run_id | ${{ github.run_id }}'
echo '-------------------------------------|-----------------------------------'
echo 'github.sha | ${{ github.sha }}'
- name: "[DEBUG] Show Workflow Inputs"
shell: bash
run: |
echo 'enabled: ${{ inputs.enabled }}'
echo 'can_deploy: ${{ inputs.can_deploy }}'
echo 'versions: ${{ inputs.versions }}'
echo 'refs: ${{ inputs.refs }}'
- name: "[DEBUG] Show Workflow Outputs"
shell: bash
run: |
# Copied from inputs
echo 'versions: ${{ inputs.versions }}'
echo 'refs: ${{ inputs.refs }}'
# Flags
echo 'can_login: ${{ steps.set-login.outputs.can_login }}'
echo 'can_push: ${{ steps.set-push.outputs.can_push }}'
# Settins
echo 'has_refs: ${{ steps.set-refs.outputs.has_refs }}'
# Values
echo 'artifact_prefix: ${{ steps.set-artifact-prefix.outputs.prefix }}'
echo 'matrix_build: ${{ steps.set-matrix.outputs.matrix_build }}'
echo 'matrix_deploy: ${{ steps.set-matrix.outputs.matrix_deploy }}'