Skip to content

Commit

Permalink
fix: force cache bust when --no-cache used (#4157)
Browse files Browse the repository at this point in the history
fixes #2593

Signed-off-by: Alex Couture-Beil <[email protected]>
  • Loading branch information
alexcb authored May 28, 2024
1 parent 74c20db commit e019418
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions earthfile2llb/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/earthly/earthly/util/vertexmeta"
"github.com/earthly/earthly/variables"
"github.com/earthly/earthly/variables/reserved"
"github.com/google/uuid"
"github.com/moby/buildkit/client/llb"
dockerimage "github.com/moby/buildkit/exporter/containerimage/image"
"github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb"
Expand Down Expand Up @@ -2306,6 +2307,10 @@ func (c *Converter) internalRun(ctx context.Context, opts ConvertRunOpts) (pllb.
// Shell and debugger wrap.
prependDebugger := !opts.Locally
finalArgs = opts.shellWrap(finalArgs, extraEnvVars, opts.WithShell, prependDebugger, isInteractive)
if opts.NoCache {
// llb.IgnoreCache is not always enough; we will force a different cache key as a work-around
finalArgs = append(finalArgs, "#"+uuid.NewString())
}
if opts.Locally {
// buildkit-hack in order to run locally, we prepend the command with a magic UUID.
finalArgs = append(
Expand Down
22 changes: 21 additions & 1 deletion tests/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ ga-no-qemu-group12:
BUILD --pass-args ./warn-if-not-logged-in+test
BUILD --pass-args ./with-docker-validate-labels+all
BUILD --pass-args +test-aws-oidc

BUILD --pass-args +run-no-cache-save-artifact

ga-no-qemu-slow:
BUILD +server
Expand Down Expand Up @@ -1061,6 +1061,26 @@ run-no-cache:
DO +RUN_EARTHLY --earthfile=run-no-cache.earth --use_tmpfs=false --target=+test-from \
--grep_flags="-v" --output_contains="\\\*cached\\\* --> .*motd2 \\\.\\\//"

run-no-cache-save-artifact:
# based on https://github.com/earthly/earthly/issues/2593#issuecomment-1777657111
COPY run-no-cache-save-artifact.earth Earthfile
RUN touch some-file
LET numruns=10
RUN echo "#!/bin/sh
set -ex
for x in \$(seq 1 $numruns); do
earthly --config \$earthly_config --artifact '+build/*' test/\$x/
done
" >/tmp/test-earthly-script && chmod +x /tmp/test-earthly-script
ENV EARTHLY_EXEC_CMD="/tmp/test-earthly-script"
RUN --privileged \
--entrypoint \
--mount=type=tmpfs,target=/tmp/earthly \
-- -P
# test that all output files are different
RUN cat test/*/dep1
RUN acbtest "$(cat test/*/dep1 | sort | uniq | wc -l)" = "$numruns"

save-artifact-after-push:
# test that save after push only outputs files before the RUN --push
DO +RUN_EARTHLY --earthfile=save-artifact-after-push.earth --target=+test
Expand Down
20 changes: 20 additions & 0 deletions tests/run-no-cache-save-artifact.earth
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
VERSION 0.8

FROM alpine:3.18
WORKDIR /work-der-work

build:
COPY +dep2/out dep2
COPY +dep1/out dep1
RUN rm dep2
SAVE ARTIFACT dep1

dep1:
COPY some-file . # required to trigger bug
COPY +dep2/out dep2
RUN cp dep2 out
SAVE ARTIFACT out

dep2:
RUN --no-cache head -c 20 /dev/urandom | base64 > out
SAVE ARTIFACT out

0 comments on commit e019418

Please sign in to comment.