Skip to content

Commit 01df480

Browse files
committed
Docker name/version/arch workflow
1 parent a4c5439 commit 01df480

File tree

1 file changed

+258
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)