Skip to content

Commit 18381bc

Browse files
mkolesnikskitt
authored andcommitted
Revert "Drop all caching logic from image building"
This reverts commit a685e56. It seems that cache was useful to speed up CI jobs after all, reverting the change. Signed-off-by: Mike Kolesnik <[email protected]>
1 parent a1d5788 commit 18381bc

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

gh-actions/release-images/action.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ runs:
1818
- name: Build new images
1919
# This needs to be kept separate so that the release stage runs using the new Shipyard base image
2020
shell: bash
21+
env:
22+
USE_CACHE: false
2123
run: |
2224
echo "::group::Build new images"
2325
make images multiarch-images

scripts/shared/build_image.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -e
44

55
### Variables ###
66

7+
[[ -n "${USE_CACHE}" ]] || USE_CACHE='true'
78
[[ $# == 1 ]] || { echo "Exactly one image to build must be specified!"; exit 1; }
89
[[ -n "${DOCKERFILE}" ]] || { echo "The DOCKERFILE to build from must be specified!"; exit 1; }
910
[[ -n "${HASHFILE}" ]] || { echo "The HASHFILE to write the hash to must be specified!"; exit 1; }
@@ -21,6 +22,28 @@ source "${SCRIPTS_DIR}/lib/debug_functions"
2122
local_image="${REPO}/${1}:${DEV_VERSION}"
2223
cache_image="${REPO}/${1}:${CUTTING_EDGE}"
2324

25+
# When using cache pull latest image from the repo, so that its layers may be reused.
26+
declare -a cache_flags
27+
if [[ "${USE_CACHE}" = true ]]; then
28+
cache_flags+=(--cache-from "${cache_image}")
29+
if [[ -z "$(docker image ls -q "${cache_image}")" ]]; then
30+
docker pull "${cache_image}" || :
31+
fi
32+
# The shellcheck linting tool recommends piping to a while read loop, but that doesn't work for us
33+
# because the while loop ends up in a subshell
34+
# shellcheck disable=SC2013
35+
for parent in $(awk '/FROM/ {
36+
for (i = 2; i <= NF; i++) {
37+
if ($i == "AS") next;
38+
if (!($i ~ /^--platform/ || $i ~ /scratch/))
39+
print gensub("\\${BASE_BRANCH}", ENVIRON["BASE_BRANCH"], "g", $i)
40+
}
41+
}' "${DOCKERFILE}"); do
42+
cache_flags+=(--cache-from "${parent}")
43+
docker pull "${parent}" || :
44+
done
45+
fi
46+
2447
output_flag=--load
2548
[[ -z "${OCIFILE}" ]] || output_flag="--output=type=oci,dest=${OCIFILE}"
2649

@@ -37,13 +60,13 @@ fi
3760
buildargs_flags=(--build-arg BUILDKIT_INLINE_CACHE=1 --build-arg "BASE_BRANCH=${BASE_BRANCH}")
3861
if [[ "${PLATFORM}" != "${default_platform}" ]] && docker buildx version > /dev/null 2>&1; then
3962
docker buildx use buildx_builder || docker buildx create --name buildx_builder --use
40-
docker buildx build "${output_flag}" -t "${local_image}" -f "${DOCKERFILE}" --iidfile "${HASHFILE}" --platform "${PLATFORM}" "${buildargs_flags[@]}" .
63+
docker buildx build "${output_flag}" -t "${local_image}" "${cache_flags[@]}" -f "${DOCKERFILE}" --iidfile "${HASHFILE}" --platform "${PLATFORM}" "${buildargs_flags[@]}" .
4164
else
4265
# Fall back to plain BuildKit
4366
if [[ "${PLATFORM}" != "${default_platform}" ]]; then
4467
echo "WARNING: buildx isn't available, cross-arch builds won't work as expected"
4568
fi
46-
DOCKER_BUILDKIT=1 docker build -t "${local_image}" -f "${DOCKERFILE}" --iidfile "${HASHFILE}" "${buildargs_flags[@]}" .
69+
DOCKER_BUILDKIT=1 docker build -t "${local_image}" "${cache_flags[@]}" -f "${DOCKERFILE}" --iidfile "${HASHFILE}" "${buildargs_flags[@]}" .
4770
fi
4871

4972
# We can only tag the image in non-OCI mode

0 commit comments

Comments
 (0)