-
Notifications
You must be signed in to change notification settings - Fork 3
199 lines (176 loc) · 7.18 KB
/
docker-multistage-build.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
---
name: "Build multistage docker images (multi -flavours, -versions, -architectures)"
on:
workflow_call:
###
### Variables
###
inputs:
matrix:
description: 'The build matrix'
required: true
type: string
stage:
description: 'The stage to build (Examples: base, mods, prod or work).'
required: true
type: string
stage_prev:
description: 'The previous stage (used for downloading previous artifact).'
required: true
type: string
artifact_prefix:
description: 'Unique artifact name prefix (to avoid overriding existing artifcats during parallel runs).'
required: true
type: string
has_refs:
description: 'The ref build matrix as JSON string (list of git refs to build/deploy).'
required: true
type: boolean
run_tests:
description: 'Dertermines whether we run integration tests or not.'
required: true
type: boolean
upload_artifact:
description: 'Dertermines whether we upload the artifact not.'
required: true
type: boolean
push_image:
description: 'Push docker image after build (and test if ran)?'
required: false
type: boolean
default: false
pull_base_image:
description: 'Pull Docker base image before building?'
required: false
type: boolean
default: false
###
### Secrets
###
secrets:
dockerhub_username:
description: 'The username for Dockerhub.'
required: false
dockerhub_password:
description: 'The password for Dockerhub.'
required: false
jobs:
# -----------------------------------------------------------------------------------------------
# JOB: BUILD
# -----------------------------------------------------------------------------------------------
build:
name: ${{ matrix.NAME }}-${{ matrix.VERSION }}-${{ inputs.stage }} (${{ matrix.ARCH }}) ${{ matrix.REFS }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(inputs.matrix) }}
steps:
# ------------------------------------------------------------
# Setup repository
# ------------------------------------------------------------
- name: "[SETUP] Checkout repository (current)"
uses: actions/checkout@v3
with:
fetch-depth: 0
if: ${{ !inputs.has_refs }}
- name: "[SETUP] Checkout repository (ref: ${{ matrix.REFS }})"
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ matrix.REFS }}
if: ${{ inputs.has_refs }}
- name: "[SETUP] Install QEMU environment"
uses: docker/setup-qemu-action@v2
id: qemu
with:
image: tonistiigi/binfmt:latest
platforms: all
- name: "[SETUP] Set artifact names"
id: set-artifact-name
run: |
VERSION="$( echo "${{ matrix.VERSION }}" )"
ARCH="$( echo "${{ matrix.ARCH }}" | sed 's|/|-|g' )"
NAME_PREV="${{ inputs.artifact_prefix }}-${VERSION}-${ARCH}-${{ inputs.stage_prev }}"
NAME_CURR="${{ inputs.artifact_prefix }}-${VERSION}-${ARCH}-${{ inputs.stage }}"
echo "prev=${NAME_PREV}" >> $GITHUB_OUTPUT
echo "curr=${NAME_CURR}" >> $GITHUB_OUTPUT
- 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
# ------------------------------------------------------------
# Artifact Import
# ------------------------------------------------------------
- name: "[Artifact Load] Download previously built image"
uses: cytopia/download-artifact-retry-action@v0.1.5
with:
name: ${{ steps.set-artifact-name.outputs.prev }}
if: ${{ inputs.stage_prev != '' }}
- name: "[Artifact Load] Import previously built image"
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make load INFILE=${{ steps.set-artifact-name.outputs.prev }}
if: ${{ inputs.stage_prev != '' }}
# ------------------------------------------------------------
# Build
# ------------------------------------------------------------
- name: Pull
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make docker-pull-base-image VERSION=${{ matrix.VERSION }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} ARCH=${{ matrix.ARCH }}
if: ${{ inputs.pull_base_image }}
- name: Build
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make build VERSION=${{ matrix.VERSION }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} ARCH=${{ matrix.ARCH }}
# ------------------------------------------------------------
# Test
# ------------------------------------------------------------
- name: Test
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make test VERSION=${{ matrix.VERSION }} STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} ARCH=${{ matrix.ARCH }}
if: ${{ inputs.run_tests }}
# ------------------------------------------------------------
# Push
# ------------------------------------------------------------
- name: Docker Tag
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make tag VERSION="${{ matrix.VERSION }}" STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.FLAVOUR }} TAG=${{ steps.tag.outputs.docker-tag }}
- name: Docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.dockerhub_username }}
password: ${{ secrets.dockerhub_password }}
if: ${{ inputs.push_image }}
- name: Docker push
uses: cytopia/shell-command-retry-action@v0.1.6
with:
command: |
make push VERSION="${{ matrix.version }}" STAGE=${{ inputs.stage }} FLAVOUR=${{ matrix.flavour }} ARCH=${{ matrix.arch }} TAG=${{ steps.tag.outputs.docker-tag }}
if: ${{ inputs.push_image }}
# ------------------------------------------------------------
# Artifact Export
# ------------------------------------------------------------
- name: "[Artifact Save] Upload built artifact"
uses: cytopia/upload-artifact-retry-action@v0.1.7
with:
name: ${{ steps.set-artifact-name.outputs.curr }}
path: ${{ steps.set-artifact-name.outputs.curr }}
pre_command: |
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 }}
post_command: |
make load INFILE={{download_path}}
if: ${{ inputs.upload_artifact }}