@@ -49,7 +49,7 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
49
49
echo " DOCKERFILE The name of Dockerfile to use."
50
50
echo " Default: Dockerfile"
51
51
echo " DOCKER_FROM The base image to use."
52
- echo " Default: Whatever is defined as default in the Dockerfile. "
52
+ echo " Default: 'python:3.7-alpine' "
53
53
echo " DOCKER_TARGET A specific target to build."
54
54
echo " It's currently not possible to pass multiple targets."
55
55
echo " Default: main ldap"
@@ -153,6 +153,11 @@ if [ ! -f "${DOCKERFILE}" ]; then
153
153
fi
154
154
fi
155
155
156
+ # ##
157
+ # Determining the value for DOCKER_FROM
158
+ # ##
159
+ DOCKER_FROM=" ${DOCKER_FROM-python: 3.7-alpine} "
160
+
156
161
# ##
157
162
# Variables for labelling the docker image
158
163
# ##
@@ -233,6 +238,49 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
233
238
# Proceeding to buils stage, except if `--push-only` is passed
234
239
# ##
235
240
if [ " ${2} " != " --push-only" ] ; then
241
+ # ##
242
+ # Checking if the build is necessary,
243
+ # meaning build only if one of those values changed:
244
+ # - Python base image digest (Label: PYTHON_BASE_DIGEST)
245
+ # - netbox git ref (Label: NETBOX_GIT_REF)
246
+ # - netbox-docker git ref (Label: org.label-schema.vcs-ref)
247
+ # ##
248
+ # Load information from registry (only for docker.io)
249
+ SHOULD_BUILD=" false"
250
+ BUILD_REASON=" "
251
+ if [ -z " ${GH_ACTION} " ]; then
252
+ # Asuming non Github builds should always proceed
253
+ SHOULD_BUILD=" true"
254
+ BUILD_REASON=" ${BUILD_REASON} interactive"
255
+ fi
256
+ if [ $DOCKER_REGISTRY = " docker.io" ] && [ $SHOULD_BUILD = " false" ]; then
257
+ source ./build-functions/get-public-image-config.sh
258
+ IFS=' :' read -ra DOCKER_FROM_SPLIT <<< " ${DOCKER_FROM}"
259
+ if ! [[ ${DOCKER_FROM_SPLIT[0]} =~ " .*/.*" ]]; then
260
+ # Need to use "library/..." for images the have no two part name
261
+ DOCKER_FROM_SPLIT[0]=" library/${DOCKER_FROM_SPLIT[0]} "
262
+ fi
263
+ PYTHON_LAST_LAYER=$( get_image_last_layer ${DOCKER_FROM_SPLIT[0]} ${DOCKER_FROM_SPLIT[1]} )
264
+ IMAGES_LAYERS_OLD=($( get_image_layers ${DOCKER_ORG} /${DOCKER_REPO} ${TAG} ) )
265
+ NETBOX_GIT_REF_OLD=$( get_image_label NETBOX_GIT_REF ${DOCKER_ORG} /${DOCKER_REPO} ${TAG} )
266
+ GIT_REF_OLD=$( get_image_label org.label-schema.vcs-ref ${DOCKER_ORG} /${DOCKER_REPO} ${TAG} )
267
+
268
+ if ! printf ' %s\n' ${IMAGES_LAYERS_OLD[@]} | grep -q -P " ^${PYTHON_LAST_LAYER} \$ " ; then
269
+ SHOULD_BUILD=" true"
270
+ BUILD_REASON=" ${BUILD_REASON} python"
271
+ fi
272
+ if [ " ${NETBOX_GIT_REF} " != " ${NETBOX_GIT_REF_OLD} " ]; then
273
+ SHOULD_BUILD=" true"
274
+ BUILD_REASON=" ${BUILD_REASON} netbox"
275
+ fi
276
+ if [ " ${GIT_REF} " != " ${GIT_REF_OLD} " ]; then
277
+ SHOULD_BUILD=" true"
278
+ BUILD_REASON=" ${BUILD_REASON} netbox-docker"
279
+ fi
280
+ else
281
+ SHOULD_BUILD=" true"
282
+ BUILD_REASON=" ${BUILD_REASON} no-check"
283
+ fi
236
284
# ##
237
285
# Composing all arguments for `docker build`
238
286
# ##
@@ -269,6 +317,10 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
269
317
--label " NETBOX_GIT_URL=${NETBOX_GIT_URL} "
270
318
)
271
319
fi
320
+ if [ -n " ${BUILD_REASON} " ]; then
321
+ BUILD_REASON=$( sed -e ' s/^[[:space:]]*//' -e ' s/[[:space:]]*$//' <<< " $BUILD_REASON" )
322
+ DOCKER_BUILD_ARGS+=( --label " BUILD_REASON=${BUILD_REASON} " )
323
+ fi
272
324
273
325
# --build-arg
274
326
DOCKER_BUILD_ARGS+=( --build-arg " NETBOX_PATH=${NETBOX_PATH} " )
@@ -287,9 +339,15 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
287
339
# ##
288
340
# Building the docker image
289
341
# ##
290
- echo " 🐳 Building the Docker image '${TARGET_DOCKER_TAG} '."
291
- $DRY docker build " ${DOCKER_BUILD_ARGS[@]} " .
292
- echo " ✅ Finished building the Docker images '${TARGET_DOCKER_TAG} '"
342
+ if [ " ${SHOULD_BUILD} " == " true" ]; then
343
+ echo " 🐳 Building the Docker image '${TARGET_DOCKER_TAG} '."
344
+ echo " Build reason set to: ${BUILD_REASON} "
345
+ $DRY docker build " ${DOCKER_BUILD_ARGS[@]} " .
346
+ echo " ✅ Finished building the Docker images '${TARGET_DOCKER_TAG} '"
347
+ else
348
+ echo " Build skipped because sources didn't change"
349
+ echo " ::set-output name=skipped::true"
350
+ fi
293
351
fi
294
352
295
353
# ##
0 commit comments