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 dd842c3..dc52572 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 @@ -8,7 +8,6 @@ import hudson.model.TaskListener; import hudson.util.ArgumentListBuilder; import org.apache.commons.io.LineIterator; -import org.apache.commons.io.output.TeeOutputStream; import org.apache.commons.lang.StringUtils; import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint; @@ -29,8 +28,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * @author Nicolas De Loof @@ -110,9 +107,11 @@ public boolean pullImage(String image) throws IOException, InterruptedException public String buildImage(FilePath workspace, String dockerfile, boolean forcePull, boolean noCache) throws IOException, InterruptedException { + FilePath tempImageIdFile = workspace.createTempFile("dcbep-", ".iid"); ArgumentListBuilder args = dockerCommand() - .add("build"); + .add("build") + .add("--iidfile", tempImageIdFile.getRemote()); if (forcePull) args.add("--pull"); @@ -126,36 +125,25 @@ public String buildImage(FilePath workspace, String dockerfile, boolean forcePul args.add("--label", "jenkins-project=" + this.build.getProject().getName()); args.add("--label", "jenkins-build-number=" + this.build.getNumber()); - OutputStream logOutputStream = listener.getLogger(); + OutputStream out = listener.getLogger(); OutputStream err = listener.getLogger(); - - ByteArrayOutputStream resultOutputStream = new ByteArrayOutputStream(); - TeeOutputStream teeOutputStream = new TeeOutputStream(logOutputStream, resultOutputStream); - int status = launcher.launch() - .envs(getEnvVars()) - .cmds(args) - .stdout(teeOutputStream).stderr(err).join(); + .envs(getEnvVars()) + .cmds(args) + .stdout(out).stderr(err).join(); if (status != 0) { throw new RuntimeException("Failed to build docker image from project Dockerfile"); } - Pattern pattern = Pattern.compile("Successfully built (.*)"); - Matcher matcher = pattern.matcher(resultOutputStream.toString("UTF-8")); - if (!matcher.find()) - { - throw new RuntimeException("Failed to lookup the docker build ImageID."); - } - - // find the last occurrence of "Successfully built" - String imageId; - do { - imageId = matcher.group(matcher.groupCount()); - } while (matcher.find()); + String imageId = tempImageIdFile.readToString(); if (imageId.equals("")) { - throw new RuntimeException("Failed to lookup the docker build ImageID."); + throw new RuntimeException("Failed to lookup the docker build ImageID. ID cannot be empty."); } + + listener.getLogger().println("ID of built image: \"" + imageId + "\""); + tempImageIdFile.delete(); + return imageId; }