Skip to content

Commit

Permalink
feat: rust actions cache for faster builds
Browse files Browse the repository at this point in the history
  • Loading branch information
0PandaDEV committed Nov 11, 2024
1 parent 6337a3b commit c7c24a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
GITHUB_IMAGE_NAME: ${{ github.repository_owner }}/backend
GITHUB_REGISTRY: ghcr.io
CARGO_TERM_COLOR: always

jobs:
build-and-push:
Expand All @@ -29,6 +30,17 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Cache Rust dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

Expand Down
16 changes: 12 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
FROM rust:1.82.0 AS builder

RUN USER=root cargo new --bin backend
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git \
USER=root cargo new --bin backend
WORKDIR /backend

COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml

RUN cargo build --release
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git \
--mount=type=cache,target=/backend/target \
cargo build --release
RUN rm src/*.rs

COPY ./src ./src

RUN rm ./target/release/deps/backend*
RUN cargo build --release
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git \
--mount=type=cache,target=/backend/target \
rm ./target/release/deps/backend* && \
cargo build --release

FROM debian:bookworm-slim

Expand Down

0 comments on commit c7c24a5

Please sign in to comment.