Skip to content

Commit 1828ae3

Browse files
authored
Merge pull request #65 from devilbox/update-multistage
Use new workflow files
2 parents 13b5bdb + df949db commit 1828ae3

File tree

5 files changed

+165
-98
lines changed

5 files changed

+165
-98
lines changed

.github/workflows/docker-multistage-build.yml

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,33 @@ on:
3535
description: 'Dertermines whether we upload the artifact not.'
3636
required: true
3737
type: boolean
38-
pull_base_image:
39-
description: 'Pull Docker base image before building?.'
38+
push_image:
39+
description: 'Push docker image after build (and test if ran)?'
4040
required: false
41+
type: boolean
4142
default: false
43+
pull_base_image:
44+
description: 'Pull Docker base image before building?'
45+
required: false
4246
type: boolean
47+
default: false
48+
###
49+
### Secrets
50+
###
51+
secrets:
52+
dockerhub_username:
53+
description: 'The username for Dockerhub.'
54+
required: true
55+
dockerhub_password:
56+
description: 'The password for Dockerhub.'
57+
required: true
4358

4459
jobs:
4560
# -----------------------------------------------------------------------------------------------
4661
# JOB: BUILD
4762
# -----------------------------------------------------------------------------------------------
4863
build:
49-
name: ${{ matrix.name }}-${{ matrix.version }}-${{ inputs.stage }} (${{ matrix.arch }}) ${{ matrix.refs }}
64+
name: ${{ matrix.NAME }}-${{ matrix.VERSION }}-${{ inputs.stage }} (${{ matrix.ARCH }}) ${{ matrix.REFS }}
5065
runs-on: ubuntu-latest
5166
strategy:
5267
fail-fast: false
@@ -63,11 +78,11 @@ jobs:
6378
fetch-depth: 0
6479
if: ${{ !inputs.has_refs }}
6580

66-
- name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})"
81+
- name: "[SETUP] Checkout repository (ref: ${{ matrix.REFS }})"
6782
uses: actions/checkout@v3
6883
with:
6984
fetch-depth: 0
70-
ref: ${{ matrix.refs }}
85+
ref: ${{ matrix.REFS }}
7186
if: ${{ inputs.has_refs }}
7287

7388
- name: "[SETUP] Install QEMU environment"
@@ -77,40 +92,32 @@ jobs:
7792
image: tonistiigi/binfmt:latest
7893
platforms: all
7994

80-
- name: "[SETUP] Install Docker Buildx"
81-
id: buildx
82-
uses: docker/setup-buildx-action@v2
83-
8495
- name: "[SETUP] Set artifact names"
8596
id: set-artifact-name
8697
run: |
87-
PRE_HASH="$( git rev-parse HEAD | head -c 10 )"
88-
VERSION="${{ matrix.version }}"
89-
ARCH="$( echo "${{ matrix.arch }}" | sed 's|/|-|g' )"
98+
VERSION="$( echo "${{ matrix.VERSION }}" )"
99+
ARCH="$( echo "${{ matrix.ARCH }}" | sed 's|/|-|g' )"
90100
91-
NAME_PREV="${{ inputs.artifact_prefix }}-${PRE_HASH}-${VERSION}-${ARCH}-${{ inputs.stage_prev }}"
92-
NAME_CURR="${{ inputs.artifact_prefix }}-${PRE_HASH}-${VERSION}-${ARCH}-${{ inputs.stage }}"
101+
NAME_PREV="${{ inputs.artifact_prefix }}-${VERSION}-${ARCH}-${{ inputs.stage_prev }}"
102+
NAME_CURR="${{ inputs.artifact_prefix }}-${VERSION}-${ARCH}-${{ inputs.stage }}"
93103
echo "prev=${NAME_PREV}" >> $GITHUB_OUTPUT
94104
echo "curr=${NAME_CURR}" >> $GITHUB_OUTPUT
95105
96-
- name: "[DEBUG] Show setup"
97-
shell: bash
106+
- name: "[SETUP] Determine Docker tag"
107+
id: tag
108+
uses: cytopia/[email protected]
109+
110+
# https://github.com/alpinelinux/docker-alpine/issues/98
111+
- name: "[SETUP] Fix Docker IP forwarding"
98112
run: |
99-
echo '(qemu) Platforms: ${{ steps.qemu.outputs.platforms }}'
100-
echo "(buildx) Name: ${{ steps.buildx.outputs.name }}"
101-
echo "(buildx) Endpoint: ${{ steps.buildx.outputs.endpoint }}"
102-
echo "(buildx) Status: ${{ steps.buildx.outputs.status }}"
103-
echo "(buildx) Flags: ${{ steps.buildx.outputs.flags }}"
104-
echo "(buildx) Platforms: ${{ steps.buildx.outputs.platforms }}"
113+
sysctl net.ipv4.ip_forward
114+
sudo sysctl -w net.ipv4.ip_forward=1
115+
sudo systemctl restart docker
105116
106117
107118
# ------------------------------------------------------------
108119
# Artifact Import
109120
# ------------------------------------------------------------
110-
111-
###
112-
### Download and import previously built image (if it exists)
113-
###
114121
- name: "[Artifact Load] Download previously built image"
115122
uses: cytopia/[email protected]
116123
with:
@@ -132,14 +139,14 @@ jobs:
132139
uses: cytopia/[email protected]
133140
with:
134141
command: |
135-
make docker-pull-base-image VERSION=${{ matrix.version }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }}
142+
make docker-pull-base-image VERSION=${{ matrix.VERSION }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} ARCH=${{ matrix.ARCH }}
136143
if: ${{ inputs.pull_base_image }}
137144

138145
- name: Build
139146
uses: cytopia/[email protected]
140147
with:
141148
command: |
142-
make build VERSION=${{ matrix.version }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }}
149+
make build VERSION=${{ matrix.VERSION }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} ARCH=${{ matrix.ARCH }}
143150
144151
145152
# ------------------------------------------------------------
@@ -149,24 +156,44 @@ jobs:
149156
uses: cytopia/[email protected]
150157
with:
151158
command: |
152-
make test VERSION=${{ matrix.version }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }}
159+
make test VERSION=${{ matrix.VERSION }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} ARCH=${{ matrix.ARCH }}
153160
if: ${{ inputs.run_tests }}
154161

155162

156163
# ------------------------------------------------------------
157-
# Artifact Export
164+
# Push
158165
# ------------------------------------------------------------
166+
- name: Docker Tag
167+
uses: cytopia/[email protected]
168+
with:
169+
command: |
170+
make tag VERSION="${{ matrix.VERSION }}" STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} TAG=${{ steps.tag.outputs.docker-tag }}
159171
160-
###
161-
### Export current image
162-
###
172+
- name: Docker login
173+
uses: docker/login-action@v2
174+
with:
175+
username: ${{ secrets.dockerhub_username }}
176+
password: ${{ secrets.dockerhub_password }}
177+
if: ${{ inputs.push_image }}
178+
179+
- name: Docker push
180+
uses: cytopia/[email protected]
181+
with:
182+
command: |
183+
make push VERSION="${{ matrix.version }}" STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
184+
if: ${{ inputs.push_image }}
185+
186+
187+
# ------------------------------------------------------------
188+
# Artifact Export
189+
# ------------------------------------------------------------
163190
- name: "[Artifact Save] Upload built artifact"
164191
uses: cytopia/[email protected]
165192
with:
166193
name: ${{ steps.set-artifact-name.outputs.curr }}
167194
path: ${{ steps.set-artifact-name.outputs.curr }}
168195
pre_command: |
169-
make save-verify VERSION=${{ matrix.version }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }} OUTFILE=${{ steps.set-artifact-name.outputs.curr }} INFILE=${{ steps.set-artifact-name.outputs.curr }}
196+
make save-verify VERSION=${{ matrix.VERSION }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} ARCH=${{ matrix.ARCH }} OUTFILE=${{ steps.set-artifact-name.outputs.curr }} INFILE=${{ steps.set-artifact-name.outputs.curr }}
170197
post_command: |
171198
make load INFILE={{download_path}}
172199
if: ${{ inputs.upload_artifact }}

.github/workflows/docker-multistage-configure.yml

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ on:
2323
description: 'Determines whether this workflow can deploy (login and push).'
2424
required: true
2525
type: boolean
26-
is_scheduled:
27-
description: "Usage for a scheduled job? Only then we will evaluate git refs and add them to build matrix."
28-
required: true
29-
type: boolean
26+
fields_build:
27+
description: 'The JSON string for build fields to extract'
28+
required: false
29+
type: string
30+
default: '[{"VERSION":"VERSION"}, {"FLAVOUR":"FLAVOUR"}, {"ARCH":"ARCH"}]'
31+
fields_deploy:
32+
description: 'The JSON string for deploy fields to extract'
33+
required: false
34+
type: string
35+
default: '[{"VERSION":"VERSION"}, {"FLAVOUR":"FLAVOUR"}, {"HELM":"EXTRA"}]'
3036

3137
###
3238
### Input Secrets
@@ -57,10 +63,10 @@ on:
5763
can_push:
5864
description: "(boolean) Can we push to Dockerhub?"
5965
value: ${{ jobs.configure.outputs.can_push }}
66+
# Determined settings (values)
6067
has_refs:
61-
description: "(boolean) Do we have refs? (used for nightly builds)"
68+
description: "(string) 'true' or 'false' Do we have refs to build?"
6269
value: ${{ jobs.configure.outputs.has_refs }}
63-
# Determined settings (values)
6470
matrix_build:
6571
description: "(string) The determined JSON string build matrix."
6672
value: ${{ jobs.configure.outputs.matrix_build }}
@@ -91,9 +97,10 @@ jobs:
9197
# Flags
9298
can_login: ${{ steps.set-login.outputs.can_login }}
9399
can_push: ${{ steps.set-push.outputs.can_push }}
100+
# Settings
94101
has_refs: ${{ steps.set-refs.outputs.has_refs }}
95-
# Values
96102
artifact_prefix: ${{ steps.set-artifact-prefix.outputs.prefix }}
103+
# Values
97104
matrix_build: ${{ steps.set-matrix.outputs.matrix_build }}
98105
matrix_deploy: ${{ steps.set-matrix.outputs.matrix_deploy }}
99106

@@ -135,6 +142,10 @@ jobs:
135142
echo "can_push=false" >> $GITHUB_OUTPUT
136143
fi
137144
145+
# ------------------------------------------------------------
146+
# Set values
147+
# ------------------------------------------------------------
148+
138149
###
139150
### Do we have refs to build against?
140151
###
@@ -154,20 +165,18 @@ jobs:
154165
echo "BRANCHES=${BRANCHES}"
155166
echo "NUM_LATEST_TAGS=${NUM_LATEST_TAGS}"
156167
157-
- name: "[Set-Output] has_refs and matrix (ref-matrix) (branches and latest tags)"
168+
###
169+
### Set Refs
170+
###
171+
- name: "[Set-Output] has_refs and ref-matrix"
158172
id: set-refs
159-
uses: cytopia/[email protected].12
173+
uses: cytopia/[email protected].13
160174
with:
161175
repository_default_branch: ${{ steps.eval-refs.outputs.default_branch }}
162176
branches: ${{ steps.eval-refs.outputs.branches }}
163177
num_latest_tags: ${{ steps.eval-refs.outputs.num_latest_tags }}
164178
# Only use refs if the job is a schedule or if the job is schedule and was triggered manually
165-
disable_refs: ${{ !(inputs.is_scheduled && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')) }}
166-
167-
168-
# ------------------------------------------------------------
169-
# Set values
170-
# ------------------------------------------------------------
179+
disable_refs: ${{ !(github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') }}
171180

172181
###
173182
### Unique Artifact prefix
@@ -176,9 +185,9 @@ jobs:
176185
id: set-artifact-prefix
177186
shell: bash
178187
run: |
179-
PRE_DATE="$( date +"%s" )"
180-
PRE_RAND="$( echo $RANDOM | md5sum | head -c 10 )"
181-
echo "prefix=${PRE_DATE}-${PRE_RAND}" >> $GITHUB_OUTPUT
188+
PRE_RUN="$( echo '${{ github.run_id }}' )"
189+
PRE_SHA="$( echo '${{ github.sha }}' | md5sum | head -c 10 )"
190+
echo "prefix=${PRE_RUN}-${PRE_SHA}" >> $GITHUB_OUTPUT
182191
183192
###
184193
### Set Build and Deploy Matrix
@@ -187,32 +196,52 @@ jobs:
187196
id: set-matrix
188197
shell: bash
189198
run: |
199+
FIELDS_BUILD="$( echo '${{ inputs.fields_build }}' | jq -M -c )"
200+
FIELDS_DEPLOY="$( echo '${{ inputs.fields_deploy }}' | jq -M -c )"
201+
VERSIONS="$( echo '${{ inputs.versions }}' | jq -M -c )"
202+
190203
if [ "${{ steps.set-refs.outputs.has_refs }}" = "true" ]; then
191-
MATRIX_BUILD="$( \
192-
jq -M -c \
193-
--argjson refs '${{ steps.set-refs.outputs.matrix }}' \
194-
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[], arch:.ARCH[], refs:$refs[]})' <<<'${{ inputs.versions }}' \
195-
)"
196-
MATRIX_DEPLOY="$( \
197-
jq -M -c \
198-
--argjson refs '${{ steps.set-refs.outputs.matrix }}' \
199-
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[], refs:$refs[]})' <<<'${{ inputs.versions }}' \
200-
)"
201-
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
202-
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
203-
echo "has_refs=true" >> $GITHUB_OUTPUT
204+
REFS="$( echo '${{ steps.set-refs.outputs.matrix }}' | jq -M -c )"
205+
206+
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ARCH:. ARCH[], ...})
207+
INNER_STRING="$( echo "${FIELDS_BUILD}" \
208+
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
209+
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
210+
| jq -r '. | join(", ")' )"
211+
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}, REFS: ${REFS}[]})'" )"
212+
MATRIX_BUILD="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
213+
214+
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ....})
215+
INNER_STRING="$( echo "${FIELDS_DEPLOY}" \
216+
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
217+
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
218+
| jq -r '. | join(", ")' )"
219+
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}, REFS: ${REFS}[]})'" )"
220+
MATRIX_DEPLOY="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
221+
222+
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
223+
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
224+
204225
else
205-
MATRIX_BUILD="$( \
206-
jq -M -c \
207-
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[], arch:.ARCH[]})' <<<'${{ inputs.versions }}' \
208-
)"
209-
MATRIX_DEPLOY="$( \
210-
jq -M -c \
211-
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[]})' <<<'${{ inputs.versions }}' \
212-
)"
213-
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
214-
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
215-
echo "has_refs=false" >> $GITHUB_OUTPUT
226+
227+
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ARCH:. ARCH[], ...})
228+
INNER_STRING="$( echo "${FIELDS_BUILD}" \
229+
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
230+
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
231+
| jq -r '. | join(", ")' )"
232+
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}})'" )"
233+
MATRIX_BUILD="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
234+
235+
# map({NAME: .NAME, VERSION: .VERSION[], FLAVOUR: .FLAVOUR[], ....})
236+
INNER_STRING="$( echo "${FIELDS_DEPLOY}" \
237+
| jq -r '.[] | to_entries[] | "\(.value): .\(.key)[]"' \
238+
| jq --raw-input --slurp 'split("\n") | map(select(. != ""))' \
239+
| jq -r '. | join(", ")' )"
240+
OUTER_STRING="$( echo "'map({NAME: .NAME, ${INNER_STRING}})'" )"
241+
MATRIX_DEPLOY="$( eval jq -M -c "${OUTER_STRING}" <<<"${VERSIONS}" )"
242+
243+
echo "matrix_build=${MATRIX_BUILD}" >> $GITHUB_OUTPUT
244+
echo "matrix_deploy=${MATRIX_DEPLOY}" >> $GITHUB_OUTPUT
216245
fi
217246
218247
# ------------------------------------------------------------
@@ -232,6 +261,8 @@ jobs:
232261
echo '-------------------------------------|-----------------------------------'
233262
echo 'github.event_name | ${{ github.event_name }}'
234263
echo '-------------------------------------|-----------------------------------'
264+
echo 'github.ref | ${{ github.ref }}'
265+
echo '-------------------------------------|-----------------------------------'
235266
echo 'github.ref_name | ${{ github.ref_name }}'
236267
echo '-------------------------------------|-----------------------------------'
237268
echo 'github.head_ref | ${{ github.head_ref }}'
@@ -241,14 +272,20 @@ jobs:
241272
echo 'github.ref_type | ${{ github.ref_type }}'
242273
echo '-------------------------------------|-----------------------------------'
243274
echo 'github.repository | ${{ github.repository }}'
275+
echo '-------------------------------------|-----------------------------------'
276+
echo 'github.action | ${{ github.action }}'
277+
echo '-------------------------------------|-----------------------------------'
278+
echo 'github.action_ref | ${{ github.action_ref }}'
279+
echo '-------------------------------------|-----------------------------------'
280+
echo 'github.run_id | ${{ github.run_id }}'
281+
echo '-------------------------------------|-----------------------------------'
282+
echo 'github.sha | ${{ github.sha }}'
244283
245284
- name: "[DEBUG] Show Workflow Inputs"
246285
shell: bash
247286
run: |
248287
echo 'enabled: ${{ inputs.enabled }}'
249288
echo 'can_deploy: ${{ inputs.can_deploy }}'
250-
echo 'is_scheduled: ${{ inputs.is_scheduled }}'
251-
echo 'can_deploy: ${{ inputs.can_deploy }}'
252289
echo 'versions: ${{ inputs.versions }}'
253290
echo 'refs: ${{ inputs.refs }}'
254291
@@ -261,6 +298,7 @@ jobs:
261298
# Flags
262299
echo 'can_login: ${{ steps.set-login.outputs.can_login }}'
263300
echo 'can_push: ${{ steps.set-push.outputs.can_push }}'
301+
# Settins
264302
echo 'has_refs: ${{ steps.set-refs.outputs.has_refs }}'
265303
# Values
266304
echo 'artifact_prefix: ${{ steps.set-artifact-prefix.outputs.prefix }}'

0 commit comments

Comments
 (0)