From eaa947923cd7ba17d9991aedad5f3c029cbd9c73 Mon Sep 17 00:00:00 2001
From: Tamas Gal <tgal@km3net.de>
Date: Tue, 13 Nov 2018 23:32:47 +0100
Subject: [PATCH] Use git to determine changed files

---
 .gitlab-ci.yml    |  1 +
 publish_images.py | 50 +++++++++++++++++++++++------------------------
 2 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f02fc15..7a622af 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -9,6 +9,7 @@ variables:
 
 before_script:
     - apk --no-cache add python
+    - pip install gitpython
 
 
 build:
diff --git a/publish_images.py b/publish_images.py
index 3f35b94..82d6735 100755
--- a/publish_images.py
+++ b/publish_images.py
@@ -4,39 +4,39 @@ import subprocess as sp
 import sys
 import time
 
+import git
+
 REGISTRY_URL = 'docker.km3net.de'
 
 
 def main():
-    dockerfiles = glob('base/*:*')
-    n_files = len(dockerfiles)
-    print("Processing {} dockerfiles...".format(n_files))
-    for idx, fname in enumerate(dockerfiles):
+    changed_dockerfiles = [f.a_path for f in git.Repo('.').index.diff("HEAD~1")
+        if f.a_path.startswith("base/")]
+    n_files = len(changed_dockerfiles)
+
+    if n_files == 0:
+        print("Nothing to do!")
+        exit(0)
+
+    print("Processing {} dockerfile{}..."
+          .format(n_files, 's' if n_files else ''))
+    for idx, fname in enumerate(changed_dockerfiles):
         progress = "({}/{})".format(idx + 1, n_files)
 
-        print('-' * 79)
-        print("{} Checking for existing '{}'".format(progress, fname))
-        pull_cmd = ("docker pull {0}/{1}".format(REGISTRY_URL, fname))
-        child = sp.Popen(
-            pull_cmd, shell=True, stdout=sys.stdout, stderr=sp.PIPE)
-        child.communicate()
-        if child.returncode > 0:
-            print("  -> no image found for '{}', starting from scratch.".
-                  format(fname))
+        steps = [("Building",
+                    "docker build --pull -t {0}/{1} -f {1} . "
+                    .format(REGISTRY_URL, fname)),
+                 ("Pushing",
+                    "docker push {0}/{1}"
+                    .format(REGISTRY_URL, fname))]
 
         print('-' * 79)
-        print("{} Building '{}'".format(progress, fname))
-        build_cmd = ("docker build --pull -t {0}/{1} -f {1} . ".format(
-            REGISTRY_URL, fname))
-        child = sp.Popen(
-            build_cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
-        child.communicate()
-
-        print("{} Publishing '{}'".format(progress, fname))
-        push_cmd = ("docker push {0}/{1}".format(REGISTRY_URL, fname))
-        child = sp.Popen(
-            push_cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
-        child.communicate()
+
+        for step, cmd in steps:
+            print("{} {} '{}'".format(progress, step, fname))
+            child = sp.Popen(
+                cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
+            child.communicate()
 
 
 if __name__ == '__main__':
-- 
GitLab