From a02f0a4d67255671946427c3f3576d5fce11c6a6 Mon Sep 17 00:00:00 2001 From: sn0w Date: Thu, 10 Jan 2019 18:24:29 +0100 Subject: [PATCH] Implement smart build caches --- Dockerfile | 4 ++-- pleroma | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index ea9f50d..16c5150 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,8 +53,8 @@ WORKDIR /home/pleroma/pleroma # used version/branch/tag/commitish/... which originates in the script. # If the host doesn't have the required tool for "smart version detection" # we'll just use the current timestamp here which forces a rebuild every time. -ARG __BUST_CACHE -ENV __BUST_CACHE $__BUST_CACHE +ARG __CACHE_TAG +ENV __CACHE_TAG $__CACHE_TAG # Fetch changes, checkout ARG PLEROMA_VERSION diff --git a/pleroma b/pleroma index fa4f39d..2cb4d60 100755 --- a/pleroma +++ b/pleroma @@ -13,6 +13,7 @@ set -o pipefail readonly GITLAB_URI="https://git.pleroma.social" readonly PREFIX_API="api/v4/projects/pleroma%2Fpleroma/repository" +readonly ENDPOINT_REPO="pleroma/pleroma.git" readonly ENDPOINT_FILE="pleroma/pleroma/raw" readonly ENDPOINT_LIST="pleroma/pleroma/files" readonly ENDPOINT_TAG="$PREFIX_API/tags" @@ -110,7 +111,40 @@ request_file_content() { # $1: source ######################################################### action__build() { - docker_compose build --build-arg __BUST_CACHE="$(date +%s)" server + local cacheTag="" + + # Alternative 1: Get tags or branches from git (if installed) + if [[ -z "$cacheTag" ]] && has_command git; then + set +o pipefail + local resolvedHash + resolvedHash="$(git ls-remote $GITLAB_URI/$ENDPOINT_REPO | grep "/$PLEROMA_VERSION" | awk '{ print $1 }')" + set -o pipefail + + if [[ -n "$resolvedHash" ]]; then + cacheTag="$resolvedHash" + fi + fi + + # Alternative 2: Current time with date or awk + if [[ -z "$cacheTag" ]] && has_command date; then + cacheTag="$(date '+%s')" + fi + + if [[ -z "$cacheTag" ]] && has_command awk; then + cacheTag="$(awk 'BEGIN { srand(); print srand() }')" + fi + + # Last resort: Constant value, user will need to run `docker system prune` + if [[ -z "$cacheTag" ]]; then + echo "" + echo "Warning: Cannot set a reliably unique cache tag." + echo " You may be running outdated code!" + echo "" + + cacheTag="broken-host-env" + fi + + docker_compose build --build-arg __CACHE_TAG="$cacheTag" server } action__dump() {