Commit dc7a246b authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add build cache support to CI builds

parent aade1156
Loading
Loading
Loading
Loading
Loading

.gitlab-ci.d/funcs.sh

0 → 100644
+30 −0
Original line number Diff line number Diff line
# Copyright (c) 2020 Dominik Sekotill <dom.sekotill@kodo.org.uk>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

values() {
	CACHE_IMG=${CI_REGISTRY_IMAGE}/${TARGET}/cache
	RW_CACHE1=${CACHE_IMG}:latest
	RW_CACHE2=${CACHE_IMG}:${CI_COMMIT_REF_NAME}
	RW_CACHE3=${CACHE_IMG}:${CI_COMMIT_SHA}
	RO_CACHE1=${CACHE_IMG}:${CI_COMMIT_BEFORE_SHA}
}

cache_arg() {
	values
	arg="--cache-from=${RW_CACHE1},${RW_CACHE2},${RW_CACHE3},${RO_CACHE1}"
	sed -n 's/FROM \([^ ]*:[^ ]*\).*/\1/p' Dockerfile |
		while read image; do arg+=",$image"; done
	echo "$arg"
}

push_cache() (
	values
	for tag in ${RW_CACHE1} ${RW_CACHE2} ${RW_CACHE3}; do
		docker tag $1 ${tag}
		docker push ${tag}
	done
)
+4 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ services:
before_script:
- docker info
- docker login -u gitlab-ci-token -p "$CI_JOB_TOKEN" "$CI_REGISTRY"

- . .gitlab-ci.d/funcs.sh

.build: &build
  stage: build
@@ -23,10 +23,13 @@ before_script:
    --pull=true
    --tag=${BUILD_TAG}
    --target=${TARGET}
    --build-arg BUILDKIT_INLINE_CACHE=1
    $(cache_arg)
    ${NGINX_VERSION:+--build-arg=nginx_version=$NGINX_VERSION}
    ${PHP_VERSION:+--build-arg=php_version=$PHP_VERSION}
    ${WORDPRESS_VERSION:+--build-arg=wp_version=$WORDPRESS_VERSION}
  - docker push ${BUILD_TAG}
  - push_cache ${BUILD_TAG}

.changes: &only-changes
  only: &change-files