From caff79166ce796b50decf209d29fbd4f0fa00204 Mon Sep 17 00:00:00 2001 From: Michal Fojtik Date: Tue, 4 Feb 2014 15:41:12 +0100 Subject: [PATCH] Added support for GIT repository to be the source_dir --- sti/cmd/builder.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sti/cmd/builder.py b/sti/cmd/builder.py index 5d4b17981..f45b9d9db 100644 --- a/sti/cmd/builder.py +++ b/sti/cmd/builder.py @@ -9,6 +9,8 @@ import tempfile import shutil import time import logging +import subprocess +import re """STI is a tool for building reproducable Docker images. STI produces ready-to-run images by injecting a user source into a docker image and preparing a new Docker image which incorporates @@ -27,7 +29,7 @@ class Builder(object): Arguments: IMAGE_NAME Source image name. STI will pull this image if not available locally. - SOURCE_DIR Directory containing your application sources. + SOURCE_DIR Directory or GIT repository containing your application sources. Options: --incremental=PREV_BUILD Perform an incremental build. PREV_BUILD specified the previous built image. @@ -122,7 +124,17 @@ class Builder(object): time.sleep(1) self.docker_client.remove_container(container_id) build_context_source = os.path.join(tmp_dir, 'src') - shutil.copytree(source_dir, build_context_source) + + if re.match('^(http(s?)|git|file)://', source_dir): + git_clone_cmd = "git clone --quiet %s %s" %(source_dir, build_context_source) + try: + self.logger.debug("Fetching %s", source_dir) + subprocess.check_output(git_clone_cmd, stderr=subprocess.STDOUT, shell=True) + except subprocess.CalledProcessError as e: + self.logger.critical("%s command failed (%i)", git_clone_cmd, e.returncode) + return False + else: + shutil.copytree(source_dir, build_context_source) with open(os.path.join(tmp_dir, 'Dockerfile'), 'w+') as docker_file: docker_file.write("FROM %s\n" % image_name)