Skip to content

Commit e56d171

Browse files
authored
Cache intermediate layers of container build (#52)
And rearrange Dockerfile to improve cache hits in CI builds.
1 parent 8ef178d commit e56d171

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

.github/workflows/_container.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ jobs:
2121
type=semver,pattern={{major}}.{{minor}}
2222
type=semver,pattern={{major}}
2323
24+
- name: Setup buildx
25+
uses: docker/setup-buildx-action@v3
26+
2427
- name: Build and export to Docker local cache
2528
uses: docker/build-push-action@v6
2629
env:
2730
DOCKER_BUILD_RECORD_UPLOAD: false
2831
with:
2932
# Need load and tags so we can test it below
33+
context: .
3034
load: true
3135
tags: test_tag
36+
cache-from: type=gha
37+
cache-to: type=gha,mode=max
3238

3339
- name: Test cli works in cached runtime image
3440
run: docker run --rm test_tag --version
@@ -47,6 +53,7 @@ jobs:
4753
env:
4854
DOCKER_BUILD_RECORD_UPLOAD: false
4955
with:
56+
context: .
5057
push: ${{ github.event_name == 'push' && github.ref_type == 'tag' }}
5158
tags: ${{ steps.meta.outputs.tags }}
5259
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ RUN rustup target add x86_64-unknown-linux-musl && \
66
apt-get install -y musl-tools musl-dev && \
77
update-ca-certificates
88

9+
# Build an empty project with only the Cargo files to improve the cache
10+
# performance of the container build. The src directory is expected to change
11+
# most frequently invalidating later caches.
12+
# This downloads and builds the dependencies early allowing built dependencies
13+
# to be cached.
14+
RUN mkdir src && echo 'fn main() {}' > src/main.rs
915
COPY ./Cargo.toml ./Cargo.toml
1016
COPY ./Cargo.lock ./Cargo.lock
17+
RUN cargo build --release --target x86_64-unknown-linux-musl
18+
1119
COPY ./src ./src
1220

1321
RUN cargo build --release --target x86_64-unknown-linux-musl

0 commit comments

Comments
 (0)