From 13b2f88c8ff8ac367f2c3e5a1dd32c9c25935328 Mon Sep 17 00:00:00 2001 From: libmartinito Date: Fri, 28 Jun 2024 23:45:17 +0800 Subject: [PATCH] CC-1333 Add repository aware caching --- dockerfiles/go-1.19.Dockerfile | 7 ++++++- dockerfiles/python-3.11.Dockerfile | 9 ++++++--- dockerfiles/rust-1.70.Dockerfile | 8 ++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/dockerfiles/go-1.19.Dockerfile b/dockerfiles/go-1.19.Dockerfile index cad38a4..3d9d240 100644 --- a/dockerfiles/go-1.19.Dockerfile +++ b/dockerfiles/go-1.19.Dockerfile @@ -1,10 +1,15 @@ +# syntax=docker/dockerfile:1.7-labs FROM golang:1.19-alpine ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="go.mod,go.sum" WORKDIR /app -COPY go.mod go.sum ./ +# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses +COPY --exclude=.git --exclude=README.md . /app RUN go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs -r go get + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/python-3.11.Dockerfile b/dockerfiles/python-3.11.Dockerfile index 5b6c513..20d7899 100644 --- a/dockerfiles/python-3.11.Dockerfile +++ b/dockerfiles/python-3.11.Dockerfile @@ -1,11 +1,12 @@ +# syntax=docker/dockerfile:1.7-labs FROM python:3.11-alpine RUN pip install pipenv -COPY Pipfile /app/Pipfile -COPY Pipfile.lock /app/Pipfile.lock WORKDIR /app +# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses +COPY --exclude=.git --exclude=README.md . /app ENV WORKON_HOME=/venvs @@ -14,4 +15,6 @@ RUN pipenv install # Force environment creation RUN pipenv run python3 -c "1+1" -ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Pipfile,Pipfile.lock" \ No newline at end of file +ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Pipfile,Pipfile.lock" +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/rust-1.70.Dockerfile b/dockerfiles/rust-1.70.Dockerfile index 28e4d8b..e1ccca1 100644 --- a/dockerfiles/rust-1.70.Dockerfile +++ b/dockerfiles/rust-1.70.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM rust:1.70-buster -COPY Cargo.toml /app/Cargo.toml -COPY Cargo.lock /app/Cargo.lock RUN mkdir /app/src RUN echo 'fn main() { println!("Hello World!"); }' > /app/src/main.rs WORKDIR /app +# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses +COPY --exclude=.git --exclude=README.md . /app RUN cargo build --release --target-dir=/tmp/codecrafters-grep-target RUN cargo clean -p grep-starter-rust --release --target-dir=/tmp/codecrafters-grep-target @@ -18,3 +19,6 @@ RUN chmod +x /codecrafters-precompile.sh ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Cargo.toml,Cargo.lock" + +# Once the heave steps are done, we can copy all files back +COPY . /app