From d7d7fa97185c8b49639b25b663ef6adf8ef60582 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Mon, 3 Jun 2024 20:09:00 +0100 Subject: [PATCH] Investigate cargo-chef to cache things --- .github/workflows/build.yml | 7 +---- Dockerfile.cache | 58 +++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 Dockerfile.cache diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 813af43..1011fee 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,17 +10,11 @@ jobs: runs-on: ubuntu-latest container: image: catthehacker/ubuntu:act-latest - env: - RUNNER_TOOL_CACHE: /toolcache steps: - name: Checkout code uses: actions/checkout@v2 -# Note to self: maybe look at this if we want to build outside docker? -# - name: Build with cache -# uses: Swatinem/rust-cache@v2 - - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 @@ -36,6 +30,7 @@ jobs: with: context: . push: true + file: ./Dockerfile.cache tags: git.joshuacoles.me/personal/monzo-ingestion:latest cache-from: type=registry,ref=user/app:latest cache-to: type=inline diff --git a/Dockerfile.cache b/Dockerfile.cache new file mode 100644 index 0000000..9bdf7e3 --- /dev/null +++ b/Dockerfile.cache @@ -0,0 +1,58 @@ +# Stage 1: Build +ARG RUST_VERSION=1.76.0 +FROM lukemathwalker/cargo-chef:latest-rust-${RUST_VERSION} as chef +WORKDIR /build/ +# hadolint ignore=DL3008 +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + lld \ + clang \ + libclang-dev \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +FROM chef as planner +COPY . . +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef as builder +COPY --from=planner /build/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release -p monzo-ingestion --recipe-path recipe.json +# Build application +COPY . . +RUN cargo build --release -p monzo-ingestion + +# Stage 2: Run +FROM debian:bullseye-slim AS final + +RUN set -ex; \ + apt-get update && \ + apt-get -y install --no-install-recommends \ + ca-certificates curl && \ + rm -rf /var/lib/apt/lists/* + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser + +# Copy the executable from the "build" stage. +COPY --from=builder /build/target/release/monzo-ingestion /bin/ + +# Expose the port that the application listens on. +EXPOSE 3000 + +HEALTHCHECK --interval=5s --timeout=3s --retries=3 \ + CMD curl -f http://localhost:3000/health || exit 1 + +# What the container should run when it is started. +CMD ["/bin/server"] diff --git a/src/main.rs b/src/main.rs index 5201e64..847be21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ enum Commands { #[arg(long)] down: bool, }, + Run { /// If we should perform migration on startup. #[clap(short, long, env, default_value_t = true)]