From 2aa0becce2644f212de5d031a340bd6006fb3461 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 10 Jul 2015 15:26:48 -0400 Subject: [PATCH 1/3] fix for path handling when the context path is not the same as the Dockerfile path. Add missing dependency for unit tests to run --- pom.xml | 6 +++ .../DockerfileImageSelector.java | 37 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index b21db04..1bb2de7 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,12 @@ 1.0 true + + org.mortbay.jetty + jetty-util + 6.1.26 + test + diff --git a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java index aa94121..040aa6c 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java @@ -33,18 +33,47 @@ public DockerfileImageSelector(String contextPath, String dockerfile) { this.dockerfile = dockerfile; } + private FilePath getDockerFilePath(FilePath ctxPath, Docker docker, AbstractBuild build, TaskListener listener) throws IOException, InterruptedException { + + FilePath dfPath = null; + + if (dockerfile != null) { + + String expandedDockerFile = build.getEnvironment(listener).expand(dockerfile); + dfPath = build.getWorkspace().child(expandedDockerFile); + + } else { + + dfPath = ctxPath.child("DockerFile"); + + } + + return dfPath; + } + + private String getAbsPath(FilePath fp) throws IOException, InterruptedException { + + File f = new File(fp.toURI()); + + return f.getAbsolutePath(); + } + @Override public String prepareDockerImage(Docker docker, AbstractBuild build, TaskListener listener) throws IOException, InterruptedException { String expandedContextPath = build.getEnvironment(listener).expand(contextPath); - FilePath filePath = build.getWorkspace().child(expandedContextPath); + FilePath ctxPath = build.getWorkspace().child(expandedContextPath); + + FilePath dfPath = getDockerFilePath(ctxPath, docker, build, listener); + + String dfAbsPath = getAbsPath(dfPath); - String hash = filePath.act(new ComputeDockerfileChecksum()); + String hash = dfPath.getParent().act(new ComputeDockerfileChecksum()); // search for a tagged image with this hash ID if (!docker.hasImage(hash)) { - listener.getLogger().println("Build Docker image from "+expandedContextPath+"/Dockerfile ..."); - docker.buildImage(filePath, dockerfile, hash); + listener.getLogger().println("Build Docker image from "+dfAbsPath+" ..."); + docker.buildImage(ctxPath, dfAbsPath, hash); } return hash; From 7c1bc199d9265e3f2660ffabc9ebfeb08f02d94e Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 22 Jul 2015 15:04:30 -0400 Subject: [PATCH 2/3] dont use file hash cache --- .../plugins/docker_build_env/DockerfileImageSelector.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java index 040aa6c..10116cd 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerfileImageSelector.java @@ -71,10 +71,14 @@ public String prepareDockerImage(Docker docker, AbstractBuild build, TaskListene String hash = dfPath.getParent().act(new ComputeDockerfileChecksum()); // search for a tagged image with this hash ID - if (!docker.hasImage(hash)) { + //MR: we don't want to do this check + //MR: The docker build will already cheerfully cache each line item int he docker file + //MR: but if you use ADD or COPY to bring ina resource, this check will miss and ignore any + //MR: changes to that file. + //if (!docker.hasImage(hash)) { listener.getLogger().println("Build Docker image from "+dfAbsPath+" ..."); docker.buildImage(ctxPath, dfAbsPath, hash); - } + //} return hash; } From 35f7e32ad9680747c6936ad7d6dbae95dfdbf2b1 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 22 Jul 2015 15:51:17 -0400 Subject: [PATCH 3/3] more updates to docker command handling --- .../plugins/docker_build_env/Docker.java | 42 +++++++++++++++++++ .../docker_build_env/DockerBuildWrapper.java | 4 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java index 994839d..d75cb17 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java @@ -173,6 +173,48 @@ public String runDetached(String image, String workdir, Map volu return out.toString("UTF-8").trim(); } + public String runAttached(String image, String workdir, Map volumes, Map ports, Map links, EnvVars environment, String user, String... command) throws IOException, InterruptedException { + + ArgumentListBuilder args = dockerCommand() + .add("run"); + if (privileged) { + args.add( "--privileged"); + } + args.add("--workdir", workdir); + for (Map.Entry volume : volumes.entrySet()) { + args.add("--volume", volume.getKey() + ":" + volume.getValue() + ":rw" ); + } + for (Map.Entry port : ports.entrySet()) { + args.add("--publish", port.getKey() + ":" + port.getValue()); + } + for (Map.Entry link : links.entrySet()) { + args.add("--link", link.getKey() + ":" + link.getValue()); + } + for (Map.Entry e : environment.entrySet()) { + if ("HOSTNAME".equals(e.getKey())) { + continue; + } + args.add("--env"); + args.addMasked(e.getKey()+"="+e.getValue()); + } + + if (command != null) { + args.add(image).add(command); + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + int status = launcher.launch() + .envs(dockerEnv.env()) + .cmds(args) + .stdout(out).quiet(!verbose).stderr(listener.getLogger()).join(); + + if (status != 0) { + throw new RuntimeException("Failed to run docker image"); + } + return out.toString("UTF-8").trim(); + } + public void executeIn(String container, Launcher.ProcStarter starter) { List originalCmds = starter.cmds(); diff --git a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java index e73cd2d..ab9e374 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java @@ -154,9 +154,9 @@ private String startBuildContainer(BuiltInContainer runInContainer, AbstractBuil Map links = new HashMap(); - return runInContainer.getDocker().runDetached(runInContainer.image, workdir, + return runInContainer.getDocker().runAttached(runInContainer.image, workdir, runInContainer.getVolumesMap(), runInContainer.getPortsMap(), links, environment, userId, - "/bin/cat"); // Command expected to hung until killed + (String)null); // Command expected to hung until killed } catch (InterruptedException e) { throw new RuntimeException("Interrupted");