From bd75d4428f7bde7a510ebc88e32c13ddc5c64a7f Mon Sep 17 00:00:00 2001 From: Alex Banna Date: Thu, 25 Jun 2026 18:50:17 -0500 Subject: [PATCH] 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. --- .../services/codebuild/CodeBuildRunner.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildRunner.java b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildRunner.java index f7a94cd3dd..e75072ffdc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildRunner.java +++ b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildRunner.java @@ -190,12 +190,10 @@ public class CodeBuildRunner { List 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");