public inbox for igt-dev@lists.freedesktop.org
 help / color / mirror / Atom feed
From: Oleg Vasilev <oleg.vasilev@intel.com>
To: igt-dev@lists.freedesktop.org
Subject: [igt-dev] [PATCH v4 4/6] gitlab-ci: rebuild images only on Dockerfile changes
Date: Tue, 27 Aug 2019 17:02:00 +0300	[thread overview]
Message-ID: <20190827140202.3347-4-oleg.vasilev@intel.com> (raw)
In-Reply-To: <20190827140202.3347-1-oleg.vasilev@intel.com>

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

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

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3433ca08..0d40b493 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -22,11 +22,6 @@ stages:
 build-containers:build-debian:
   stage: build-containers
   image: docker:stable
-  only:
-    changes:
-      - Dockefile.build-debian-minimal
-      - Dockefile.build-debian
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -34,18 +29,12 @@ build-containers:build-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/build-debian-minimal -t build-debian-minimal -f Dockefile.build-debian-minimal .
-    - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH/build-debian -f Dockefile.build-debian .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/build-debian-minimal
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/build-debian
+    - .gitlab-ci/pull-or-rebuild.sh Dockerfile.build-debian-minimal build-debian-minimal
+    - .gitlab-ci/pull-or-rebuild.sh Dockerfile.build-debian build-debian
 
 build-containers:build-debian-armhf:
   stage: build-containers
   image: docker:stable
-  only:
-    changes:
-      - Dockefile.build-debian-armhf
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -53,16 +42,11 @@ build-containers:build-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/build-debian-armhf -f Dockefile.build-debian-armhf .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/build-debian-armhf
+    - .gitlab-ci/pull-or-rebuild.sh Dockerfile.build-debian-armhf build-debian-armhf
 
 build-containers:build-debian-arm64:
   stage: build-containers
   image: docker:stable
-  only:
-    changes:
-      - Dockefile.build-debian-arm64
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -70,16 +54,11 @@ build-containers:build-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/build-debian-arm64 -f Dockefile.build-debian-arm64 .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/build-debian-arm64
+    - .gitlab-ci/pull-or-rebuild.sh Dockerfile.build-debian-arm64 build-debian-arm64
 
 build-containers:build-debian-mips:
   stage: build-containers
   image: docker:stable
-  only:
-    changes:
-      - Dockefile.build-debian-mips
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -87,16 +66,11 @@ build-containers:build-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/build-debian-mips -f Dockefile.build-debian-mips .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/build-debian-mips
+    - .gitlab-ci/pull-or-rebuild.sh Dockerfile.build-debian-mips build-debian-mips
 
 build-containers:build-fedora:
   stage: build-containers
   image: docker:stable
-  only:
-    changes:
-      - Dockefile.build-fedora
-      - .gitlab-ci.yml
   services:
     - docker:dind
   variables:
@@ -104,8 +78,7 @@ build-containers:build-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/build-fedora -f Dockefile.build-fedora .
-    - docker push $CI_REGISTRY/$CI_PROJECT_PATH/build-fedora
+    - .gitlab-ci/pull-or-rebuild.sh Dockerfile.build-fedora build-fedora
 
 #################### BUILD #########################
 
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
-- 
2.23.0

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

  parent reply	other threads:[~2019-08-27 14:02 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-27 14:01 [igt-dev] [PATCH v4 1/6] gitlab-ci: move containers section to the top Oleg Vasilev
2019-08-27 14:01 ` [igt-dev] [PATCH v4 2/6] gitlab-ci: rename igt-* container to build-* Oleg Vasilev
2019-08-27 14:01 ` [igt-dev] [PATCH v4 3/6] gitlab-ci: rename Dockerfiles Oleg Vasilev
2019-08-27 14:02 ` Oleg Vasilev [this message]
2019-08-27 14:02 ` [igt-dev] [PATCH v4 5/6] gitlab-ci: use images tagged with git ref instead of latest Oleg Vasilev
2019-08-27 14:02 ` [igt-dev] [PATCH v4 6/6] gitlab-ci: introduce containers with IGT inside Oleg Vasilev
2019-08-28  6:03   ` Arkadiusz Hiler
2019-08-28 10:13     ` Vasilev, Oleg
2019-08-27 14:29 ` [igt-dev] ✓ Fi.CI.BAT: success for series starting with [v4,1/6] gitlab-ci: move containers section to the top Patchwork
2019-08-28 12:58 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190827140202.3347-4-oleg.vasilev@intel.com \
    --to=oleg.vasilev@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox