From 2bf4aba41b69c3d1cc12e9d3b0d28df7e8a02122 Mon Sep 17 00:00:00 2001 From: Davlet Panech Date: Wed, 20 Apr 2022 11:38:00 -0400 Subject: [PATCH] build-docker-images: allow docker cache Add an option to docker image build scripts to enable docker cache. This allows us to build images much more quickly when troubleshooting build scripts. Story: 2009897 Task: 45121 TESTS ======== Build base image and one of the docker apps with and without the "--cache" option and make sure it works as expected Signed-off-by: Davlet Panech Change-Id: I0a1e94ebfe8a263c6ddd293d0da40b5eb8550e80 --- .../build-docker-images/build-stx-base.sh | 14 +++++++++-- .../build-docker-images/build-stx-images.sh | 25 ++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/build-tools/build-docker-images/build-stx-base.sh b/build-tools/build-docker-images/build-stx-base.sh index cf8f1d28..eca7fb75 100755 --- a/build-tools/build-docker-images/build-stx-base.sh +++ b/build-tools/build-docker-images/build-stx-base.sh @@ -36,6 +36,7 @@ CLEAN=no TAG_LATEST=no LATEST_TAG=latest HOST=${HOSTNAME} +USE_DOCKER_CACHE=no declare -i MAX_ATTEMPTS=1 function usage { @@ -65,6 +66,9 @@ Options: --attempts: Max attempts, in case of failure (default: 1) --config-file:Specify a path to a config file which will specify additional arguments to be passed into the command + --cache: Allow docker to use cached filesystem layers when building + CAUTION: this option may ignore locally-generated packages + and is meant for debugging the build scripts. EOF } @@ -114,7 +118,7 @@ function get_args_from_file { done <"$1" } -OPTS=$(getopt -o h -l help,os:,os-version:,version:,stream:,release:,repo:,push,proxy:,latest,latest-tag:,user:,registry:,local,clean,hostname:,attempts:,config-file: -- "$@") +OPTS=$(getopt -o h -l help,os:,os-version:,version:,stream:,release:,repo:,push,proxy:,latest,latest-tag:,user:,registry:,local,clean,cache,hostname:,attempts:,config-file: -- "$@") if [ $? -ne 0 ]; then usage exit 1 @@ -186,6 +190,10 @@ while true; do CLEAN=yes shift ;; + --cache) + USE_DOCKER_CACHE=yes + shift + ;; --hostname) HOST=$2 shift 2 @@ -358,7 +366,9 @@ if [ ! -z "$PROXY" ]; then fi # Don't use docker cache -BUILD_ARGS+=("--no-cache") +if [[ "$USE_DOCKER_CACHE" != "yes" ]] ; then + BUILD_ARGS+=("--no-cache") +fi BUILD_ARGS+=(--tag ${IMAGE_NAME} ${BUILDDIR}) diff --git a/build-tools/build-docker-images/build-stx-images.sh b/build-tools/build-docker-images/build-stx-images.sh index 549d7d57..0d912750 100755 --- a/build-tools/build-docker-images/build-stx-images.sh +++ b/build-tools/build-docker-images/build-stx-images.sh @@ -36,6 +36,7 @@ BASE= WHEELS= WHEELS_PY2= CLEAN=no +export USE_DOCKER_CACHE=no TAG_LATEST=no TAG_LIST_FILE= TAG_LIST_LATEST_FILE= @@ -80,6 +81,11 @@ Options: multiple --skip arguments. --attempts: Max attempts, in case of failure (default: 1) + --cache: Allow docker to use filesystem cache when building + CAUTION: this option may ignore locally-generated + packages and is meant for debugging the build + scripts. + EOF } @@ -540,9 +546,14 @@ function build_image_loci { # Use patched docker file BUILD_ARGS+=(--file "${WORKDIR}/loci/Dockerfile.stx") + # Disable build cache + if [[ "$USE_DOCKER_CACHE" != "yes" ]] ; then + BUILD_ARGS+=("--no-cache") + fi + local build_image_name="${USER}/${LABEL}:${IMAGE_TAG_BUILD}" - with_retries ${MAX_ATTEMPTS} docker build ${WORKDIR}/loci --no-cache \ + with_retries ${MAX_ATTEMPTS} docker build ${WORKDIR}/loci \ "${BUILD_ARGS[@]}" \ --tag ${build_image_name} 2>&1 | tee ${WORKDIR}/docker-${LABEL}-${OS}-${BUILD_STREAM}.log if [ ${PIPESTATUS[0]} -ne 0 ]; then @@ -655,7 +666,7 @@ function build_image_docker { local build_image_name="${USER}/${LABEL}:${IMAGE_TAG_BUILD}" local -a BASE_BUILD_ARGS - BASE_BUILD_ARGS+=(${real_docker_context} --no-cache) + BASE_BUILD_ARGS+=(${real_docker_context}) BASE_BUILD_ARGS+=(--file ${real_docker_file}) BASE_BUILD_ARGS+=(--build-arg "BASE=${BASE}") if [ ! -z "$HTTP_PROXY" ]; then @@ -670,6 +681,10 @@ function build_image_docker { BASE_BUILD_ARGS+=(--build-arg no_proxy=$NO_PROXY) fi + if [[ "$USE_DOCKER_CACHE" != "yes" ]] ; then + BASE_BUILD_ARGS+=("--no-cache") + fi + BASE_BUILD_ARGS+=(--tag ${build_image_name}) with_retries ${MAX_ATTEMPTS} docker build ${BASE_BUILD_ARGS[@]} 2>&1 | tee ${WORKDIR}/docker-${LABEL}-${OS}-${BUILD_STREAM}.log @@ -784,7 +799,7 @@ function build_image { esac } -OPTS=$(getopt -o hN -l help,os:,version:,release:,stream:,push,http_proxy:,https_proxy:,no_proxy:,user:,registry:,base:,wheels:,wheels-alternate:,wheels-py2:,only:,skip:,prefix:,latest,latest-prefix:,clean,attempts:,no-pull-base -- "$@") +OPTS=$(getopt -o hN -l help,os:,version:,release:,stream:,push,http_proxy:,https_proxy:,no_proxy:,user:,registry:,base:,wheels:,wheels-alternate:,wheels-py2:,only:,skip:,prefix:,latest,latest-prefix:,clean,cache,attempts:,no-pull-base -- "$@") if [ $? -ne 0 ]; then usage exit 1 @@ -864,6 +879,10 @@ while true; do CLEAN=yes shift ;; + --cache) + USE_DOCKER_CACHE=yes + shift + ;; --only) # Read comma-separated values into array ONLY+=(${2//,/ })