From e4d4bda4323947cae127f211286dfcd2eac97009 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/bun-1.1.Dockerfile | 8 ++++++-- dockerfiles/dotnet-6.0.Dockerfile | 8 ++++++-- dockerfiles/dotnet-8.0.Dockerfile | 8 ++++++-- dockerfiles/go-1.16.Dockerfile | 8 ++++++-- dockerfiles/go-1.19.Dockerfile | 8 ++++++-- dockerfiles/go-1.21.Dockerfile | 8 ++++++-- dockerfiles/go-1.22.Dockerfile | 8 ++++++-- dockerfiles/java-21.Dockerfile | 7 ++++++- dockerfiles/nodejs-21.Dockerfile | 8 ++++++-- dockerfiles/python-3.11.Dockerfile | 8 ++++++-- dockerfiles/python-3.12.Dockerfile | 8 ++++++-- dockerfiles/python-3.8.Dockerfile | 8 ++++++-- dockerfiles/ruby-3.3.Dockerfile | 7 ++++++- dockerfiles/rust-1.43.Dockerfile | 8 ++++++-- dockerfiles/rust-1.54.Dockerfile | 8 ++++++-- dockerfiles/rust-1.62.Dockerfile | 8 ++++++-- dockerfiles/rust-1.68.Dockerfile | 8 ++++++-- dockerfiles/rust-1.70.Dockerfile | 8 ++++++-- dockerfiles/rust-1.76.Dockerfile | 8 ++++++-- dockerfiles/rust-1.77.Dockerfile | 8 ++++++-- 20 files changed, 120 insertions(+), 38 deletions(-) diff --git a/dockerfiles/bun-1.1.Dockerfile b/dockerfiles/bun-1.1.Dockerfile index 4db1041..3622634 100644 --- a/dockerfiles/bun-1.1.Dockerfile +++ b/dockerfiles/bun-1.1.Dockerfile @@ -1,11 +1,12 @@ +# syntax=docker/dockerfile:1.7-labs FROM oven/bun:1.1.4-alpine ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="package.json,bun.lockb" WORKDIR /app -COPY package.json ./ -COPY bun.lockb ./ +# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses +COPY --exclude=.git --exclude=README.md . /app # For reproducible builds. # This will install the exact versions of each package specified in the lockfile. @@ -15,3 +16,6 @@ RUN bun install --frozen-lockfile RUN mkdir -p /app-cached # If the node_modules directory exists, move it to /app-cached RUN if [ -d "/app/node_modules" ]; then mv /app/node_modules /app-cached; fi + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/dotnet-6.0.Dockerfile b/dockerfiles/dotnet-6.0.Dockerfile index 5c8270c..b8c7884 100644 --- a/dockerfiles/dotnet-6.0.Dockerfile +++ b/dockerfiles/dotnet-6.0.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim -COPY codecrafters-sqlite.csproj /app/codecrafters-sqlite.csproj -COPY codecrafters-sqlite.sln /app/codecrafters-sqlite.sln RUN mkdir /app/src RUN (echo 'System.Console.WriteLine("If you are seeing this, there is something wrong with our caching mechanism! Please contact us at hello@codecrafters.io.");' > /app/src/Program.cs) > /dev/null 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 # This saves nuget packages to ~/.nuget RUN dotnet build --configuration Release . @@ -22,3 +23,6 @@ RUN chmod +x /codecrafters-precompile.sh ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="codecrafters-sqlite.csproj,codecrafters-sqlite.sln" + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/dotnet-8.0.Dockerfile b/dockerfiles/dotnet-8.0.Dockerfile index 3234d44..2a3f044 100644 --- a/dockerfiles/dotnet-8.0.Dockerfile +++ b/dockerfiles/dotnet-8.0.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine -COPY codecrafters-sqlite.csproj /app/codecrafters-sqlite.csproj -COPY codecrafters-sqlite.sln /app/codecrafters-sqlite.sln RUN mkdir /app/src RUN (echo 'System.Console.WriteLine("If you are seeing this, there is something wrong with our caching mechanism! Please contact us at hello@codecrafters.io.");' > /app/src/Program.cs) > /dev/null 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 # This saves nuget packages to ~/.nuget RUN dotnet build --configuration Release . @@ -22,3 +23,6 @@ RUN chmod +x /codecrafters-precompile.sh ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="codecrafters-sqlite.csproj,codecrafters-sqlite.sln" + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/go-1.16.Dockerfile b/dockerfiles/go-1.16.Dockerfile index 6721452..9add415 100644 --- a/dockerfiles/go-1.16.Dockerfile +++ b/dockerfiles/go-1.16.Dockerfile @@ -1,9 +1,10 @@ +# syntax=docker/dockerfile:1.7-labs FROM golang:1.16-alpine -COPY go.mod /app/go.mod -COPY go.sum /app/go.sum 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 go mod download @@ -13,3 +14,6 @@ RUN go mod download RUN ash -c "set -exo pipefail; go mod graph | awk '{if (\$1 !~ \"@\") {print \$2}}' | xargs -r go get" ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="go.mod,go.sum" + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/go-1.19.Dockerfile b/dockerfiles/go-1.19.Dockerfile index 6d10667..de734eb 100644 --- a/dockerfiles/go-1.19.Dockerfile +++ b/dockerfiles/go-1.19.Dockerfile @@ -1,9 +1,10 @@ +# syntax=docker/dockerfile:1.7-labs FROM golang:1.19-alpine -COPY go.mod /app/go.mod -COPY go.sum /app/go.sum 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 go mod download @@ -13,3 +14,6 @@ RUN go mod download RUN ash -c "set -exo pipefail; go mod graph | awk '{if (\$1 !~ \"@\") {print \$2}}' | xargs -r go get" ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="go.mod,go.sum" + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/go-1.21.Dockerfile b/dockerfiles/go-1.21.Dockerfile index 9b1f956..3530193 100644 --- a/dockerfiles/go-1.21.Dockerfile +++ b/dockerfiles/go-1.21.Dockerfile @@ -1,9 +1,10 @@ +# syntax=docker/dockerfile:1.7-labs FROM golang:1.19-alpine -COPY go.mod /app/go.mod -COPY go.sum /app/go.sum 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 go mod download @@ -17,3 +18,6 @@ RUN GODEBUG="installgoroot=all" go install std RUN ash -c "set -exo pipefail; go mod graph | awk '{if (\$1 !~ \"@\") {print \$2}}' | xargs -r go get" ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="go.mod,go.sum" + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/go-1.22.Dockerfile b/dockerfiles/go-1.22.Dockerfile index d007910..5078a9c 100644 --- a/dockerfiles/go-1.22.Dockerfile +++ b/dockerfiles/go-1.22.Dockerfile @@ -1,9 +1,10 @@ +# syntax=docker/dockerfile:1.7-labs FROM golang:1.22-alpine -COPY go.mod /app/go.mod -COPY go.sum /app/go.sum 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 go mod download @@ -17,3 +18,6 @@ RUN GODEBUG="installgoroot=all" go install std RUN ash -c "set -exo pipefail; go mod graph | awk '{if (\$1 !~ \"@\") {print \$2}}' | xargs -r go get" ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="go.mod,go.sum" + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/java-21.Dockerfile b/dockerfiles/java-21.Dockerfile index 256aaae..96b4dd7 100644 --- a/dockerfiles/java-21.Dockerfile +++ b/dockerfiles/java-21.Dockerfile @@ -1,8 +1,10 @@ +# syntax=docker/dockerfile:1.7-labs FROM maven:3.9.5-eclipse-temurin-21-alpine -COPY pom.xml /app/pom.xml 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 # Download the dependencies RUN mvn -B package -Ddir=/tmp/codecrafters-sqlite-target @@ -14,3 +16,6 @@ RUN mv /app/target /app-cached # Is this needed? # Pre-compile steps RUN printf "cd \${CODECRAFTERS_SUBMISSION_DIR} && mvn -B package -Ddir=/tmp/codecrafters-sqlite-target && sed -i 's/^\(mvn .*\)/#\1/' ./your_sqlite3.sh" > /codecrafters-precompile.sh RUN chmod +x /codecrafters-precompile.sh + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/nodejs-21.Dockerfile b/dockerfiles/nodejs-21.Dockerfile index 435b41e..8f634f5 100644 --- a/dockerfiles/nodejs-21.Dockerfile +++ b/dockerfiles/nodejs-21.Dockerfile @@ -1,11 +1,12 @@ +# syntax=docker/dockerfile:1.7-labs FROM node:21.7-alpine3.19 ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="package.json,package-lock.json" WORKDIR /app -COPY package.json ./ -COPY package-lock.json ./ +# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses +COPY --exclude=.git --exclude=README.md . /app # If dependencies in the package lock do not match those in package.json, instead of updating the package lock, npm ci will exit with an error. RUN npm ci @@ -13,3 +14,6 @@ RUN npm ci RUN mkdir -p /app-cached # If the node_modules directory exists, move it to /app-cached RUN if [ -d "/app/node_modules" ]; then mv /app/node_modules /app-cached; fi + +# 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 c449ed3..f47f222 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 --no-cache-dir "pipenv>=2023.12.1" -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 LANG="en_US.UTF-8" ENV PIPENV_VENV_IN_PROJECT=1 @@ -20,3 +21,6 @@ RUN mkdir -p /app-cached RUN mv /app/.venv /app-cached/.venv 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/python-3.12.Dockerfile b/dockerfiles/python-3.12.Dockerfile index 29d45b3..7e6b137 100644 --- a/dockerfiles/python-3.12.Dockerfile +++ b/dockerfiles/python-3.12.Dockerfile @@ -1,11 +1,12 @@ +# syntax=docker/dockerfile:1.7-labs FROM python:3.12-alpine RUN pip install --no-cache-dir "pipenv>=2023.12.1" -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 LANG="en_US.UTF-8" ENV PIPENV_VENV_IN_PROJECT=1 @@ -20,3 +21,6 @@ RUN mkdir -p /app-cached RUN mv /app/.venv /app-cached/.venv 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/python-3.8.Dockerfile b/dockerfiles/python-3.8.Dockerfile index 0012a06..c785d8f 100644 --- a/dockerfiles/python-3.8.Dockerfile +++ b/dockerfiles/python-3.8.Dockerfile @@ -1,11 +1,12 @@ +# syntax=docker/dockerfile:1.7-labs FROM jfloff/alpine-python:3.8 RUN pip install --no-cache-dir "pipenv>=2023.12.1" -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 LANG="en_US.UTF-8" ENV PIPENV_VENV_IN_PROJECT=1 @@ -20,3 +21,6 @@ RUN mkdir -p /app-cached RUN mv /app/.venv /app-cached/.venv 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/ruby-3.3.Dockerfile b/dockerfiles/ruby-3.3.Dockerfile index 2c92211..9accaf4 100644 --- a/dockerfiles/ruby-3.3.Dockerfile +++ b/dockerfiles/ruby-3.3.Dockerfile @@ -1,9 +1,14 @@ +# syntax=docker/dockerfile:1.7-labs FROM ruby:3.3-alpine ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Gemfile,Gemfile.lock" WORKDIR /app -COPY Gemfile Gemfile.lock ./ +# .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 bundle install --verbose + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/rust-1.43.Dockerfile b/dockerfiles/rust-1.43.Dockerfile index ea711ea..76c0048 100644 --- a/dockerfiles/rust-1.43.Dockerfile +++ b/dockerfiles/rust-1.43.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM rust:1.43-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-sqlite-target RUN rm /tmp/codecrafters-sqlite-target/release/sqlite-starter-rust @@ -22,3 +23,6 @@ RUN rm -rf /app/src ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Cargo.toml,Cargo.lock" + +# Once the heave steps are done, we can copy all files back +COPY . /app diff --git a/dockerfiles/rust-1.54.Dockerfile b/dockerfiles/rust-1.54.Dockerfile index 12d0d51..de26eed 100644 --- a/dockerfiles/rust-1.54.Dockerfile +++ b/dockerfiles/rust-1.54.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM rust:1.54-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-sqlite-target RUN rm /tmp/codecrafters-sqlite-target/release/sqlite-starter-rust @@ -25,3 +26,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 diff --git a/dockerfiles/rust-1.62.Dockerfile b/dockerfiles/rust-1.62.Dockerfile index b84f717..c265d41 100644 --- a/dockerfiles/rust-1.62.Dockerfile +++ b/dockerfiles/rust-1.62.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM rust:1.62-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-sqlite-target RUN rm /tmp/codecrafters-sqlite-target/release/sqlite-starter-rust @@ -25,3 +26,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 diff --git a/dockerfiles/rust-1.68.Dockerfile b/dockerfiles/rust-1.68.Dockerfile index 87a0d34..d94297c 100644 --- a/dockerfiles/rust-1.68.Dockerfile +++ b/dockerfiles/rust-1.68.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM rust:1.68-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-sqlite-target RUN rm /tmp/codecrafters-sqlite-target/release/sqlite-starter-rust @@ -25,3 +26,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 diff --git a/dockerfiles/rust-1.70.Dockerfile b/dockerfiles/rust-1.70.Dockerfile index 442d7e8..fa4cb1f 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-sqlite-target RUN rm /tmp/codecrafters-sqlite-target/release/sqlite-starter-rust @@ -25,3 +26,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 diff --git a/dockerfiles/rust-1.76.Dockerfile b/dockerfiles/rust-1.76.Dockerfile index 5c6488e..aaad3e1 100644 --- a/dockerfiles/rust-1.76.Dockerfile +++ b/dockerfiles/rust-1.76.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM rust:1.76-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-sqlite-target RUN rm /tmp/codecrafters-sqlite-target/release/sqlite-starter-rust @@ -25,3 +26,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 diff --git a/dockerfiles/rust-1.77.Dockerfile b/dockerfiles/rust-1.77.Dockerfile index b9f8b87..c7b8e13 100644 --- a/dockerfiles/rust-1.77.Dockerfile +++ b/dockerfiles/rust-1.77.Dockerfile @@ -1,12 +1,13 @@ +# syntax=docker/dockerfile:1.7-labs FROM rust:1.77-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-sqlite-target RUN rm /tmp/codecrafters-sqlite-target/release/sqlite-starter-rust @@ -25,3 +26,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