summaryrefslogtreecommitdiff
path: root/.gitlab-ci
diff options
context:
space:
mode:
authorOleg Vasilev <oleg.vasilev@intel.com>2019-08-28 15:48:39 +0300
committerArkadiusz Hiler <arkadiusz.hiler@intel.com>2019-08-29 13:18:35 +0300
commit1a6c9018e76d4e57f88ee96fd3d1ba1d0a668006 (patch)
treeeacb30eab6594e2d32e79996e29b225ff8ce5aef /.gitlab-ci
parent27d1e768e59a85c67458dca09a7f790ad6e9445d (diff)
gitlab-ci: rebuild images only on Dockerfile changes
Base images rarely change, there is not much sense in rebuilding it on every commit. GitLab already has mechanism for detecting such changes. However, it is only able to prevent rebuilding whenever there is no changes within the same ref. Since in our CI system, git tag is created on every series, the mechanism doesn't work. One possible way to workaround that is to compute a checksum of a Dockerfile, and rebuilding only if there was no image built with the same checksum. V2: - Rename gitlab-ci to .gitlab-ci (Arek) - Add commit-$CI_COMMIT_SHA Reviewed-by: Arkadiusz Hiler <arkadiusz.hiler@intel.com> Signed-off-by: Oleg Vasilev <oleg.vasilev@intel.com>
Diffstat (limited to '.gitlab-ci')
-rwxr-xr-x.gitlab-ci/pull-or-rebuild.sh56
1 files changed, 56 insertions, 0 deletions
diff --git a/.gitlab-ci/pull-or-rebuild.sh b/.gitlab-ci/pull-or-rebuild.sh
new file mode 100755
index 00000000..182c8d49
--- /dev/null
+++ b/.gitlab-ci/pull-or-rebuild.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+#
+# Copyright © 2019 Intel Corporation
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+
+DOCKERFILE=$1
+NAME=$2
+REF=${3:-${CI_COMMIT_REF_NAME:-latest}}
+
+REF=$(echo $REF | tr / - )
+IMAGENAME=$CI_REGISTRY/$CI_PROJECT_PATH/$NAME
+DOCKERFILE_CHECKSUM=$(sha1sum $DOCKERFILE | cut -d ' ' -f1)
+
+REFNAME=$IMAGENAME:$REF
+DOCKERNAME=$IMAGENAME:dockerfile-$DOCKERFILE_CHECKSUM
+COMMITNAME=$IMAGENAME:commit-$CI_COMMIT_SHA
+
+docker pull $DOCKERNAME
+IMAGE_PRESENT=$?
+
+set -e
+if [ $IMAGE_PRESENT -eq 0 ] && [ ${FORCE_REBUILD:-0} -eq 0 ] ; then
+ echo "Skipping $NAME:$TAG, already built"
+ docker tag $DOCKERNAME $NAME
+ docker tag $DOCKERNAME $REFNAME
+ docker tag $DOCKERNAME $COMMITNAME
+else
+ echo "Building $NAME:$TAG"
+ docker build -t $DOCKERNAME -t $NAME \
+ -t $REFNAME -t $COMMITNAME -f $DOCKERFILE .
+ docker push $DOCKERNAME
+fi
+docker push $REFNAME
+docker push $COMMITNAME
+if [ $TAG = "master" ]; then
+ docker tag $CHECKNAME $IMAGENAME
+ docker push $IMAGENAME
+fi