fix(codebuild): create build working directory before running phases (#1577)

The build container was started with a fire-and-forget entrypoint
("mkdir -p /codebuild/output/src/src && tail -f /dev/null"). createAndStart
returns as soon as the container is running, which can be before that mkdir
completes. The runner then immediately execs the build phases (which chdir
into the directory) and copies artifacts out of it, so on a slow container
start the directory does not exist yet: the BUILD exec fails and the artifact
copy 404s, and the build is reported as FAILED.

Drop the mkdir from the entrypoint and create the working directory with an
explicit, awaited "mkdir -p" exec (run from "/", which always exists) before
the source copy and the phases. This removes the race.

This was surfacing as the intermittent failure of
CodeBuildTest.batchGetBuilds_eventuallySucceeds in the sdk-test-java
compatibility suite.
This commit is contained in:
Alex Banna authored and GitHub committed 2026-06-25 18:50:17 -05:00
1 parent 21a7939e46
commit bd75d4428f
1 file changed
+19 -8
@@ -190,12 +190,10 @@ public class CodeBuildRunner {
List<String> envList = buildEnvList(region, build, project, buildspec, logStream);
// Create the working directory inside the container as part of startup,
// then keep the container alive. No bind mount needed — source and
// artifacts are transferred with docker cp.
// Keep the container alive so each phase can be run with docker exec.
// No bind mount needed — source and artifacts are transferred with docker cp.
ContainerSpec spec = containerBuilder.newContainer(image)
.withCmd(List.of("sh", "-c",
"mkdir -p /codebuild/output/src/src && tail -f /dev/null"))
.withCmd(List.of("sh", "-c", "tail -f /dev/null"))
.withEnv(envList)
.withDockerNetwork(config.services().codebuild().dockerNetwork())
.withEmbeddedDns()
@@ -210,13 +208,26 @@ public class CodeBuildRunner {
logHandle = logStreamer.attach(containerId, logGroup, logStream, region, "codebuild:" + buildId);
// Copy downloaded source files into the container (no-op for NO_SOURCE builds)
copySourceToContainer(containerId, workspace, "/codebuild/output/src/src");
String containerSrcDir = "/codebuild/output/src/src";
int timeoutMinutes = build.getTimeoutInMinutes() != null ? build.getTimeoutInMinutes() : 60;
boolean buildFailed = false;
// createAndStart returns as soon as the container's entrypoint process is
// running, which can be before any startup command would finish. Create the
// working directory with an explicit, awaited exec (run from "/", which always
// exists) so the source copy, the phase execs that chdir into it, and the final
// artifact copy can never race against container startup.
PhaseResult workDirResult = runPhase(containerId, "/", envList,
List.of("mkdir -p " + containerSrcDir), timeoutMinutes, stopFlag);
if (workDirResult.stopped()) { finishStopped(build); return; }
if (workDirResult.failed()) {
throw new IllegalStateException("Could not create build working directory "
+ containerSrcDir + ": " + workDirResult.errorMessage());
}
// Copy downloaded source files into the container (no-op for NO_SOURCE builds)
copySourceToContainer(containerId, workspace, containerSrcDir);
// INSTALL
if (stopFlag.get()) { finishStopped(build); return; }
beginPhase(build, "INSTALL");