Skip to content
Snippets Groups Projects
publish_images.py 1.83 KiB
Newer Older
Tamas Gal's avatar
Tamas Gal committed
#!/usr/bin/env python
from glob import glob
import subprocess as sp
import sys
import time

REGISTRY_URL = 'docker.km3net.de'
Tamas Gal's avatar
Tamas Gal committed
IS_TEST_BRANCH = '-t' in sys.argv
Tamas Gal's avatar
Tamas Gal committed
def get_changed_dockerfiles(vs_master=False):
    """Checks the last Git commit and returns a list of changed Dockefiles"""
Tamas Gal's avatar
Tamas Gal committed
    if vs_master:
Tamas Gal's avatar
Tamas Gal committed
        diff_cmd = "git --no-pager diff --name-only origin/master"
Tamas Gal's avatar
Tamas Gal committed
    else:
        diff_cmd = "git diff-tree --no-commit-id --name-only -r HEAD"
    child = sp.Popen(diff_cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
    out, _ = child.communicate()

    changed_files = [l.decode().rstrip() for l in out.split(b'\n')]
    changed_dockerfiles = [f for f in changed_files if f.startswith("base/")]
    return changed_dockerfiles


Tamas Gal's avatar
Tamas Gal committed
def main():
Tamas Gal's avatar
Tamas Gal committed
    changed_dockerfiles = get_changed_dockerfiles(vs_master=IS_TEST_BRANCH)
    n_files = len(changed_dockerfiles)

    if n_files == 0:
        print("Nothing to do!")
        exit(0)

Tamas Gal's avatar
Tamas Gal committed
    print("Processing {} dockerfile{}...".format(n_files,
Tamas Gal's avatar
Tamas Gal committed
                                                 's' if n_files > 1 else ''))
    for idx, fname in enumerate(changed_dockerfiles):
Tamas Gal's avatar
Tamas Gal committed
        progress = "({}/{})".format(idx + 1, n_files)

        steps = [("Building",
Tamas Gal's avatar
Tamas Gal committed
                  "docker build --pull -t {0}/{1} -f {1} . ".format(
Tamas Gal's avatar
Tamas Gal committed
                      REGISTRY_URL, fname))]
Tamas Gal's avatar
Tamas Gal committed
        if not IS_TEST_BRANCH:
            steps.append(
                ("Pushing", "docker push {0}/{1}".format(REGISTRY_URL, fname)))
Tamas Gal's avatar
Tamas Gal committed
        else:
            print("Skiping push...")
Tamas Gal's avatar
Tamas Gal committed
        print('-' * 79)

        for step, cmd in steps:
            print("{} {} '{}'".format(progress, step, fname))
Tamas Gal's avatar
Tamas Gal committed
            child = sp.Popen(cmd,
                             shell=True,
                             stdout=sys.stdout,
                             stderr=sys.stderr)
            child.communicate()
Tamas Gal's avatar
Tamas Gal committed

Tamas Gal's avatar
Tamas Gal committed
if __name__ == '__main__':
    main()