Skip to content

Commit

Permalink
JENKINS-66171: Get built docker image ID using --iidfile (#82)
Browse files Browse the repository at this point in the history
Previously we were capturing `docker build` output, which led to some problems:
1. Other programs could print similar strings, which would match but did not
contain the image ID. This was fixed by taking the last capture as image ID.
2. `docker build` output has changed in version 23, so now we'd have to match
the output twice. First pass for docker < 23, and another for >= 23

Docker has had the `--iidfile` option for some time now, so switch to using it.
It is a much less fragile way to capture the image ID.

Tested with Docker 20.10.23 and 23.0.1

Co-authored-by: Jon Hermansen <[email protected]>
  • Loading branch information
RealCLanger and jonhermansen authored Jul 30, 2023
1 parent c3da4fa commit 01d447e
Showing 1 changed file with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <a href="mailto:[email protected]">Nicolas De Loof</a>
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}

Expand Down

0 comments on commit 01d447e

Please sign in to comment.