include python package changes when setting image, chart versions
path_changed, last_git_changed accept lists of paths
include Python package files in git consideration for both image
specify BINDERHUB_VERSION in image via `--build-arg`
@@ -7,55 +7,78 @@ from ruamel.yaml import YAML
yaml = YAML()
yaml.indent(offset=2)
-BASEPATH = os.path.dirname(__file__)
+BASEPATH = os.path.abspath(os.path.dirname(__file__))
CHARTPATH = os.path.join(BASEPATH, 'binderhub')
+ROOTPATH = os.path.dirname(BASEPATH)
NAME = 'binderhub'
-
-def last_git_modified(path):
+PYPKGPATH = os.path.join(ROOTPATH, NAME)
+SETUP_PY = os.path.join(ROOTPATH, 'setup.py')
+
+IMAGE_PATH = os.path.join(BASEPATH, 'images', NAME)
+# IMAGE_FILES should be all paths that contribute to the binderhub image
+# namely, the Python package itself and the image directory
+IMAGE_FILES = [SETUP_PY, PYPKGPATH, IMAGE_PATH]
+
+# CHART_FILES should be all files that contribute to the chart
+# namely, all image files plus the chart itself
+CHART_FILES = IMAGE_FILES + [CHARTPATH]
+
+def last_git_modified(paths):
+ """Return the short hash of the last commit on one or more paths"""
+ if isinstance(paths, str):
+ paths = [paths]
return subprocess.check_output([
'git',
'log',
'-n', '1',
'--pretty=format:%h',
- path
- ]).decode('utf-8')
+ '--',
+ ] + list(paths)).decode('utf-8')
+
-def image_touched(image, commit_range):
+def path_changed(paths, commit_range):
+ """Have the path(s) changed during the given commit range?"""
+ if isinstance(paths, str):
+ paths = [paths]
return subprocess.check_output([
- 'git', 'diff', '--name-only', commit_range, os.path.join(BASEPATH, 'images', image)
- ]).decode('utf-8').strip() != ''
+ 'git', 'diff', '--name-only', commit_range, '--',
+ ] + paths).decode('utf-8').strip() != ''
+
def build_images(prefix, images, commit_range=None, push=False):
for image in images:
+ image_path = os.path.join(BASEPATH, 'images', image)
if commit_range:
- if not image_touched(image, commit_range):
+ if path_changed(IMAGE_FILES, commit_range):
print("Skipping {}, not touched in {}".format(image, commit_range))
continue
- image_path = os.path.join(BASEPATH, 'images', image)
- tag = last_git_modified(image_path)
+ tag = last_git_modified(IMAGE_FILES)
image_spec = '{}{}:{}'.format(prefix, image, tag)
subprocess.check_call([
- 'docker', 'build', '-t', image_spec, image_path
+ 'docker', 'build', '-t', image_spec,
+ '--build-arg', 'BINDERHUB_VERSION=%s' % tag,
+ image_path
])
if push:
subprocess.check_call([
'docker', 'push', image_spec
])
+
def build_values(prefix):
with open(os.path.join(CHARTPATH, 'values.yaml')) as f:
values = yaml.load(f)
values['image']['name'] = prefix + NAME
- values['image']['tag'] = last_git_modified(os.path.join(BASEPATH, 'images/', NAME))
+ values['image']['tag'] = last_git_modified(IMAGE_FILES)
with open(os.path.join(CHARTPATH, 'values.yaml'), 'w') as f:
yaml.dump(values, f)
def build_chart():
- version = last_git_modified('.')
+ version = last_git_modified([BASEPATH] + IMAGE_FILES)
with open(os.path.join(CHARTPATH, 'Chart.yaml')) as f:
chart = yaml.load(f)
