Skip to content

Commit 2800800

Browse files
authored
Cache container build CI (#231)
Building an empty cargo project with the dependencies included but no git/code included allows the dependencies to be cached and re-used.
1 parent fb53cca commit 2800800

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

.github/workflows/publish.yaml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,24 @@ jobs:
3333
if: ${{ github.ref_type == 'tag' }}
3434
run: sed -i 's/^version = ".*"$/version = "${{ steps.meta.outputs.version }}"/' Cargo.toml
3535

36+
- name: Login to GHCR
37+
uses: docker/login-action@v3
38+
with:
39+
registry: ghcr.io/diamondlightsource
40+
username: ${{ github.actor }}
41+
password: ${{ secrets.GITHUB_TOKEN }}
42+
3643
- name: Build image
3744
uses: docker/build-push-action@v6
3845
env:
3946
DOCKER_BUILD_RECORD_UPLOAD: false
4047
with:
4148
context: .
42-
cache-from: type=gha
43-
cache-to: type=gha
44-
tags: ${{ steps.meta.outputs.tags }}
49+
cache-from: type=registry,ref=ghcr.io/diamondlightsource/numtracker:buildcache
50+
cache-to: type=registry,ref=ghcr.io/diamondlightsource/numtracker:buildcache,mode=max
51+
tags: numtracker-build-cache
4552
load: true
4653

47-
- name: Login to GHCR
48-
if: ${{ github.event_name == 'push' && github.ref_type == 'tag' }}
49-
uses: docker/login-action@v3
50-
with:
51-
registry: ghcr.io/${{ github.repository_owner }}
52-
username: ${{ github.actor }}
53-
password: ${{ secrets.GITHUB_TOKEN }}
54-
5554
- name: Publish image
5655
uses: docker/build-push-action@v6
5756
env:

Dockerfile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@ RUN rustup target add x86_64-unknown-linux-musl && \
77

88
WORKDIR /build
99

10+
# Jump through hoops here to improve the caching performance.
11+
# As each command creates a new layer that can be re-used as long as nothing
12+
# has changed, maximise the amount of work done before the frequently updated
13+
# files (.git and src) are included
14+
15+
# Create an empty project
16+
RUN cargo init --name empty-build-init
17+
18+
# Copy the cargo config but no source
1019
COPY ./Cargo.toml ./Cargo.toml
1120
COPY ./Cargo.lock ./Cargo.lock
21+
22+
# Build the empty project with all the dependencies
23+
RUN cargo build --release --target x86_64-unknown-linux-musl
24+
25+
# Copy the rest of the project in and build again
1226
COPY ./build.rs ./build.rs
1327
COPY ./.env ./.env
14-
COPY ./src ./src
1528
COPY ./.sqlx ./.sqlx
1629
COPY ./migrations ./migrations
1730
COPY ./queries ./queries
1831
COPY ./static ./static
32+
COPY ./src ./src
1933
# Copy the git directory purely so that the commit information can be included
2034
# build details provided by the 'built' library. See #99
2135
COPY ./.git ./.git

0 commit comments

Comments
 (0)