Skip to content

Commit 4bc26d7

Browse files
committed
Submit new workflow
1 parent 5a4b5d3 commit 4bc26d7

File tree

1 file changed

+251
-0
lines changed

1 file changed

+251
-0
lines changed
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
name: Build multi-arch image
2+
3+
on:
4+
workflow_call:
5+
###
6+
### Variables
7+
###
8+
inputs:
9+
enabled:
10+
description: 'Determines wheather this workflow is enabled at all (will run or skip).'
11+
required: true
12+
type: boolean
13+
can_deploy:
14+
description: 'Determines wheather this workflow will also deploy (login and push).'
15+
required: true
16+
type: boolean
17+
matrix:
18+
description: 'The version build matrix as JSON string ( list of objects: [{NAME, VERSION[], ARCH[]}] ).'
19+
required: true
20+
type: string
21+
refs:
22+
description: 'The ref build matrix as JSON string (list of git refs to build/deploy).'
23+
required: false
24+
type: string
25+
###
26+
### Secrets
27+
###
28+
secrets:
29+
dockerhub_username:
30+
description: 'The username for Dockerhub.'
31+
required: false
32+
dockerhub_password:
33+
description: 'The password for Dockerhub.'
34+
required: false
35+
36+
jobs:
37+
38+
# -----------------------------------------------------------------------------------------------
39+
# JOB (1/3): CONFIGURE
40+
# -----------------------------------------------------------------------------------------------
41+
configure:
42+
name: Configure
43+
runs-on: ubuntu-latest
44+
outputs:
45+
can_login: ${{ steps.set-login.outputs.can_login }}
46+
has_refs: ${{ steps.set-matrix.outputs.has_refs }}
47+
matrix_build: ${{ steps.set-matrix.outputs.matrix_build }}
48+
matrix_deploy: ${{ steps.set-matrix.outputs.matrix_deploy }}
49+
if: inputs.enabled
50+
steps:
51+
- name: "[Set-Output] Set Docker login capabilities"
52+
id: set-login
53+
shell: bash
54+
run: |
55+
if [ "${{ env.ENV_USER }}" = '' ] || [ "${{ env.ENV_PASS }}" = '' ]; then
56+
echo "::set-output name=can_login::0"
57+
else
58+
echo "::set-output name=can_login::1"
59+
fi
60+
env:
61+
ENV_USER: ${{ secrets.dockerhub_username }}
62+
ENV_PASS: ${{ secrets.dockerhub_password }}
63+
64+
- name: "[Set-Output] Set Build & Deploy Matrix"
65+
id: set-matrix
66+
shell: bash
67+
run: |
68+
if [ "${{ inputs.refs }}" != "" ]; then
69+
MATRIX_BUILD="$( \
70+
jq -M -c \
71+
--argjson refs '${{ inputs.refs }}' \
72+
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[], arch:.ARCH[], refs:$refs[]})' <<<'${{ inputs.matrix }}' \
73+
)"
74+
MATRIX_DEPLOY="$( \
75+
jq -M -c \
76+
--argjson refs '${{ inputs.refs }}' \
77+
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[], refs:$refs[]})' <<<'${{ inputs.matrix }}' \
78+
)"
79+
echo "::set-output name=matrix_build::${MATRIX_BUILD}"
80+
echo "::set-output name=matrix_deploy::${MATRIX_DEPLOY}"
81+
echo "::set-output name=has_refs::1"
82+
else
83+
MATRIX_BUILD="$( \
84+
jq -M -c \
85+
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[], arch:.ARCH[]})' <<<'${{ inputs.matrix }}' \
86+
)"
87+
MATRIX_DEPLOY="$( \
88+
jq -M -c \
89+
'map({name:.NAME, version:.VERSION[], flavour:.FLAVOUR[]})' <<<'${{ inputs.matrix }}' \
90+
)"
91+
echo "::set-output name=matrix_build::${MATRIX_BUILD}"
92+
echo "::set-output name=matrix_deploy::${MATRIX_DEPLOY}"
93+
echo "::set-output name=has_refs::0"
94+
fi
95+
96+
- name: "[DEBUG] Workflow Inputs"
97+
shell: bash
98+
run: |
99+
echo 'enabled: ${{ inputs.enabled }} '
100+
echo 'can_deploy: ${{ inputs.can_deploy }} '
101+
echo 'matrix: ${{ inputs.matrix }} '
102+
echo 'refs: ${{ inputs.refs }} '
103+
104+
- name: "[DEBUG] Determined Settings"
105+
shell: bash
106+
run: |
107+
echo 'can_login=${{ steps.set-login.outputs.can_login }}'
108+
echo 'has_refs=${{ steps.set-matrix.outputs.has_refs }}'
109+
echo 'matrix_build=${{ steps.set-matrix.outputs.matrix_build }}'
110+
echo 'matrix_deploy=${{ steps.set-matrix.outputs.matrix_deploy }}'
111+
112+
# -----------------------------------------------------------------------------------------------
113+
# JOB (2/3): BUILD
114+
# -----------------------------------------------------------------------------------------------
115+
build:
116+
needs: [configure]
117+
name: Build ${{ matrix.name }}-${{ matrix.version }} (${{ matrix.flavour }}) (${{ matrix.arch }}) ${{ matrix.refs }}
118+
runs-on: ubuntu-latest
119+
strategy:
120+
fail-fast: false
121+
matrix:
122+
include: ${{ fromJson(needs.configure.outputs.matrix_build) }}
123+
if: inputs.enabled
124+
steps:
125+
# ------------------------------------------------------------
126+
# Setup repository
127+
# ------------------------------------------------------------
128+
- name: "[SETUP] Checkout repository (current)"
129+
uses: actions/checkout@v3
130+
with:
131+
fetch-depth: 0
132+
if: needs.configure.outputs.has_refs == 0
133+
134+
- name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})"
135+
uses: actions/checkout@v3
136+
with:
137+
fetch-depth: 0
138+
ref: ${{ matrix.refs }}
139+
if: needs.configure.outputs.has_refs != 0
140+
141+
- name: "[SETUP] Setup QEMU environment"
142+
uses: docker/setup-qemu-action@v1
143+
with:
144+
image: tonistiigi/binfmt:latest
145+
platforms: all
146+
147+
- name: "[SETUP] Determine Docker tag"
148+
id: tag
149+
uses: cytopia/docker-tag-action@v0.4.15
150+
151+
# ------------------------------------------------------------
152+
# Build
153+
# ------------------------------------------------------------
154+
- name: Build
155+
uses: cytopia/shell-command-retry-action@v0.1.2
156+
with:
157+
command: |
158+
make build NAME=${{ matrix.name }} VERSION=${{ matrix.version }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
159+
160+
# ------------------------------------------------------------
161+
# Test
162+
# ------------------------------------------------------------
163+
- name: Test
164+
uses: cytopia/shell-command-retry-action@v0.1.2
165+
with:
166+
command: |
167+
make test NAME=${{ matrix.name }} VERSION=${{ matrix.version }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
168+
169+
# ------------------------------------------------------------
170+
# Deploy
171+
# ------------------------------------------------------------
172+
- name: Docker login
173+
uses: docker/login-action@v1
174+
with:
175+
username: ${{ secrets.dockerhub_username }}
176+
password: ${{ secrets.dockerhub_password }}
177+
if: needs.configure.outputs.can_login == 1 && inputs.can_deploy
178+
179+
- name: Docker push architecture image
180+
uses: cytopia/shell-command-retry-action@v0.1.2
181+
with:
182+
command: |
183+
make push NAME=${{ matrix.name }} VERSION=${{ matrix.version }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
184+
if: needs.configure.outputs.can_login == 1 && inputs.can_deploy
185+
186+
# -----------------------------------------------------------------------------------------------
187+
# JOB (3/3): DEPLOY
188+
# -----------------------------------------------------------------------------------------------
189+
deploy:
190+
needs: [configure, build]
191+
name: Deploy ${{ matrix.name }}-${{ matrix.version }} (${{ matrix.flavour }}) ${{ matrix.refs }}
192+
runs-on: ubuntu-latest
193+
strategy:
194+
fail-fast: false
195+
matrix:
196+
include: ${{ fromJson(needs.configure.outputs.matrix_deploy) }}
197+
if: inputs.enabled && needs.configure.outputs.can_login == 1 && inputs.can_deploy
198+
steps:
199+
# ------------------------------------------------------------
200+
# Setup repository
201+
# ------------------------------------------------------------
202+
- name: "[SETUP] Checkout repository (current)"
203+
uses: actions/checkout@v3
204+
with:
205+
fetch-depth: 0
206+
if: needs.configure.outputs.has_refs == 0
207+
208+
- name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})"
209+
uses: actions/checkout@v3
210+
with:
211+
fetch-depth: 0
212+
ref: ${{ matrix.refs }}
213+
if: needs.configure.outputs.has_refs != 0
214+
215+
- name: "[SETUP] Determine Docker tag"
216+
id: tag
217+
uses: cytopia/docker-tag-action@v0.4.15
218+
219+
- name: "[SETUP] Determine manifest arches"
220+
id: manifest
221+
run: |
222+
ARCHES="$( echo '${{ inputs.matrix }}' \
223+
| jq 'group_by(.NAME, .VERSION, .ARCH)' \
224+
| jq 'map({NAME: .[].NAME, VERSION: .[].VERSION[], FLAVOUR: .[].FLAVOUR[], ARCHES: .[].ARCH|join(",")})' \
225+
| jq '.[] | select(.NAME=="${{ matrix.name }}" and .VERSION=="${{ matrix.version }}" and .FLAVOUR=="${{ matrix.flavour }}") | .ARCHES' \
226+
| jq -c -M \
227+
)"
228+
echo "::set-output name=arches::${ARCHES}"
229+
echo "ARCHES: ${ARCHES}"
230+
231+
232+
# ------------------------------------------------------------
233+
# Deploy
234+
# ------------------------------------------------------------
235+
- name: "[DEPLOY] Login"
236+
uses: docker/login-action@v1
237+
with:
238+
username: ${{ secrets.DOCKERHUB_USERNAME }}
239+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
240+
241+
- name: "[DEPLOY] Create Docker manifest for architectures: ${{ steps.manifest.outputs.arches }}"
242+
uses: cytopia/shell-command-retry-action@v0.1.2
243+
with:
244+
command: |
245+
make manifest-create NAME=${{ matrix.name }} VERSION=${{ matrix.version }} FLAVOUR=${{ matrix.flavour }} ARCHES=${{ steps.manifest.outputs.arches }} TAG=${{ steps.tag.outputs.docker-tag }}
246+
247+
- name: "[DEPLOY] Publish Docker manifest: ${{ steps.tag.outputs.docker-tag }}"
248+
uses: cytopia/shell-command-retry-action@v0.1.2
249+
with:
250+
command: |
251+
make manifest-push NAME=${{ matrix.name }} VERSION=${{ matrix.version }} FLAVOUR=${{ matrix.flavour }} TAG=${{ steps.tag.outputs.docker-tag }}

0 commit comments

Comments
 (0)