[igt-dev] [PATCH v2 2/3] gitlab-ci: rebuild images only on Dockerfile changes

Oleg Vasilev oleg.vasilev at intel.com
Tue Aug 20 13:19:35 UTC 2019


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.

Signed-off-by: Oleg Vasilev <oleg.vasilev at intel.com>
---
 .gitlab-ci.yml             | 39 +++++------------------------
 scripts/pull-or-rebuild.sh | 50 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 33 deletions(-)
 create mode 100755 scripts/pull-or-rebuild.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d47c9514..c9f8ca35 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -22,11 +22,6 @@ stages:
 containers:igt-debian:
   stage: containers
   image: docker:stable
-  only:
-    changes:
-      - Dockerfile.debian-minimal
-      - Dockerfile.debian
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -34,18 +29,12 @@ containers:igt-debian:
     DOCKER_DRIVER: overlay2
   script:
     - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-    - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-minimal -t igt-debian-minimal -f Dockerfile.debian-minimal .
-    - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian -f Dockerfile.debian .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-minimal
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian
+    - scripts/pull-or-rebuild.sh Dockerfile.debian-minimal igt-debian-minimal
+    - scripts/pull-or-rebuild.sh Dockerfile.debian igt-debian
 
 containers:igt-debian-armhf:
   stage: containers
   image: docker:stable
-  only:
-    changes:
-      - Dockerfile.debian-armhf
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -53,16 +42,11 @@ containers:igt-debian-armhf:
     DOCKER_DRIVER: overlay2
   script:
     - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-    - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-armhf -f Dockerfile.debian-armhf .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-armhf
+    - scripts/pull-or-rebuild.sh Dockerfile.debian-armhf igt-debian-armhf
 
 containers:igt-debian-arm64:
   stage: containers
   image: docker:stable
-  only:
-    changes:
-      - Dockerfile.debian-arm64
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -70,16 +54,11 @@ containers:igt-debian-arm64:
     DOCKER_DRIVER: overlay2
   script:
     - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-    - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-arm64 -f Dockerfile.debian-arm64 .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-arm64
+    - scripts/pull-or-rebuild.sh Dockerfile.debian-arm64 igt-debian-arm64
 
 containers:igt-debian-mips:
   stage: containers
   image: docker:stable
-  only:
-    changes:
-      - Dockerfile.debian-mips
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -87,16 +66,11 @@ containers:igt-debian-mips:
     DOCKER_DRIVER: overlay2
   script:
     - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-    - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-mips -f Dockerfile.debian-mips .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/igt-debian-mips
+    - scripts/pull-or-rebuild.sh Dockerfile.debian-mips igt-debian-mips
 
 containers:igt-fedora:
   stage: containers
   image: docker:stable
-  only:
-    changes:
-      - Dockerfile.fedora
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -104,8 +78,7 @@ containers:igt-fedora:
     DOCKER_DRIVER: overlay2
   script:
     - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-    - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH/igt-fedora -f Dockerfile.fedora .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/igt-fedora
+    - scripts/pull-or-rebuild.sh Dockerfile.fedora igt-fedora
 
 #################### BUILD #########################
 
diff --git a/scripts/pull-or-rebuild.sh b/scripts/pull-or-rebuild.sh
new file mode 100755
index 00000000..8ad32696
--- /dev/null
+++ b/scripts/pull-or-rebuild.sh
@@ -0,0 +1,50 @@
+#!/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
+TAG=${3:-${CI_COMMIT_REF_NAME:-latest}}
+
+TAG=$(echo $TAG | tr / - )
+if [ $TAG = "master" ]; then
+	TAG=latest
+fi
+
+CHECKSUM=$(sha1sum $DOCKERFILE | cut -d ' ' -f1)
+FULLNAME=$CI_REGISTRY/$CI_PROJECT_PATH/$NAME:$TAG
+CHECKNAME=$CI_REGISTRY/$CI_PROJECT_PATH/$NAME:$CHECKSUM
+
+docker pull $CHECKNAME
+IMAGE_PRESENT=$?
+
+set -e
+if [ $IMAGE_PRESENT -eq 0 ] && [ ${FORCE_REBUILD:-0} -eq 0 ] ; then
+	echo "Skipping $NAME:$TAG, already built"
+	docker tag $CHECKNAME $FULLNAME
+	docker tag $CHECKNAME $NAME
+else
+	echo "Building $NAME:$TAG"
+	docker build -t $CHECKNAME -t $FULLNAME -t $NAME -f $DOCKERFILE .
+	docker push $CHECKNAME
+fi
+docker push $FULLNAME
-- 
2.22.0



More information about the igt-dev mailing list