diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f02fc15910555dc17fcadb2d30590e788bd66f90..7a622af4acb78fa32aba75c930397592c089add9 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 3f35b94b77d23d16cc0ce12b67efb8177c46e359..82d67357b0c09fa82889e32e34428f0559eb1c06 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__':