use caching for docker builds (#1340)

* try pulling for cached layers

* use cache-from and cache-to

* empty commit

* add readme

* use specific cache tags

* also cache from main
This commit is contained in:
Robert Brennan
2024-04-24 15:57:46 -04:00
committed by GitHub
parent d357fcff36
commit 8828d9836d
3 changed files with 22 additions and 3 deletions

View File

@@ -9,7 +9,9 @@ if [[ $3 == "--push" ]]; then
fi
echo -e "Building: $image_name"
tags=(latest)
tags=()
cache_tag_base="buildcache"
cache_tag="$cache_tag_base"
if [[ -n $GITHUB_REF_NAME ]]; then
# check if ref name is a version number
if [[ $GITHUB_REF_NAME =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
@@ -18,6 +20,7 @@ if [[ -n $GITHUB_REF_NAME ]]; then
tags+=($major_version $minor_version)
fi
sanitized=$(echo $GITHUB_REF_NAME | sed 's/[^a-zA-Z0-9.-]\+/-/g')
cache_tag+="-${sanitized}"
tags+=($sanitized)
fi
echo "Tags: ${tags[@]}"
@@ -38,7 +41,7 @@ fi
DOCKER_REPOSITORY=$DOCKER_REGISTRY/$DOCKER_ORG/$DOCKER_IMAGE
echo "Repo: $DOCKER_REPOSITORY"
echo "Base dir: $DOCKER_BASE_DIR"
#docker pull $DOCKER_REPOSITORY:main || true # try to get any cached layers
args=""
for tag in ${tags[@]}; do
args+=" -t $DOCKER_REPOSITORY:$tag"
@@ -49,6 +52,9 @@ fi
docker buildx build \
$args \
--cache-to=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag,mode=max \
--cache-from=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag \
--cache-from=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag_base-main \
--platform linux/amd64,linux/arm64 \
--provenance=false \
-f $dir/Dockerfile $DOCKER_BASE_DIR