From b8d8a0784ad3b2fc46e606caa6b3e659e98d5f76 Mon Sep 17 00:00:00 2001 From: Ben Cressey Date: Mon, 17 Aug 2020 22:18:37 +0000 Subject: [PATCH] build: use per-checkout cache directories The cache directory is primarily used by cargo, and having two cargo commands writing build artifacts to the same location will lead to unpredictable results. Signed-off-by: Ben Cressey --- Dockerfile | 6 ++++-- tools/buildsys/src/builder.rs | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 626274c4882..c7e5aaed4ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,10 +11,11 @@ FROM ${SDK} as sdk FROM scratch AS cache ARG PACKAGE ARG ARCH +ARG TOKEN # We can't create directories via RUN in a scratch container, so take an existing one. COPY --chown=1000:1000 --from=sdk /tmp /cache # Ensure the ARG variables are used in the layer to prevent reuse by other builds. -COPY --chown=1000:1000 .dockerignore /cache/.${PACKAGE}.${ARCH} +COPY --chown=1000:1000 .dockerignore /cache/.${PACKAGE}.${ARCH}.${TOKEN} # Some builds need to modify files in the source directory, for example Rust # software using build.rs to generate code. The source directory is mounted in @@ -32,10 +33,11 @@ FROM scratch AS variantcache ARG PACKAGE ARG ARCH ARG VARIANT +ARG TOKEN # We can't create directories via RUN in a scratch container, so take an existing one. COPY --chown=1000:1000 --from=sdk /tmp /variantcache # Ensure the ARG variables are used in the layer to prevent reuse by other builds. -COPY --chown=1000:1000 .dockerignore /variantcache/.${PACKAGE}.${ARCH}.${VARIANT} +COPY --chown=1000:1000 .dockerignore /variantcache/.${PACKAGE}.${ARCH}.${VARIANT}.${TOKEN} FROM sdk AS rpmbuild ARG PACKAGE diff --git a/tools/buildsys/src/builder.rs b/tools/buildsys/src/builder.rs index fdceec15993..5778af43396 100644 --- a/tools/buildsys/src/builder.rs +++ b/tools/buildsys/src/builder.rs @@ -123,8 +123,8 @@ fn build(target: &str, build_args: &str, tag: &str, output: &str) -> Result<()> let mut d = Sha512::new(); d.update(&root); let digest = hex::encode(d.finalize()); - let suffix = &digest[..12]; - let tag = format!("{}-{}", tag, suffix); + let token = &digest[..12]; + let tag = format!("{}-{}", tag, token); // Our SDK image is picked by the external `cargo make` invocation. let sdk = getenv("BUILDSYS_SDK_IMAGE")?; @@ -134,6 +134,9 @@ fn build(target: &str, build_args: &str, tag: &str, output: &str) -> Result<()> let nocache = rand::thread_rng().gen::(); let nocache_args = format!("--build-arg NOCACHE={}", nocache); + // Avoid using a cached layer from a concurrent build in another checkout. + let token_args = format!("--build-arg TOKEN={}", token); + let build = args(format!( "build . \ --network none \ @@ -141,11 +144,13 @@ fn build(target: &str, build_args: &str, tag: &str, output: &str) -> Result<()> {build_args} \ {sdk_args} \ {nocache_args} \ + {token_args} \ --tag {tag}", target = target, build_args = build_args, sdk_args = sdk_args, nocache_args = nocache_args, + token_args = token_args, tag = tag, ));