Skip to content

Commit

Permalink
Build Cache Optimization (#1402)
Browse files Browse the repository at this point in the history
* don't copy if we mount the files anyway

* use newer alpine version

* cache apk pkgs

* changeing workdir is not needed in ziggoimg

* cache some more

* preload go modules
  • Loading branch information
kwitsch authored Mar 15, 2024
1 parent ca74978 commit 7eef4bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ jobs:
with:
go-version-file: go.mod

- name: Download dependencies
run: go mod download
if: matrix.go == true

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
if: matrix.docker == true
Expand Down
22 changes: 12 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

# ----------- stage: ca-certs
# get newest certificates in seperate stage for caching
FROM --platform=$BUILDPLATFORM alpine:3.16 AS ca-certs
RUN apk add --no-cache ca-certificates
FROM --platform=$BUILDPLATFORM alpine:3 AS ca-certs
RUN --mount=type=cache,target=/var/cache/apk \
apk update && \
apk add ca-certificates

# update certificates and use the apk ones if update fails
RUN --mount=type=cache,target=/etc/ssl/certs \
Expand All @@ -16,17 +18,15 @@ FROM --platform=$BUILDPLATFORM ghcr.io/kwitsch/ziggoimg AS build
ARG VERSION
ARG BUILD_TIME

# set working directory
WORKDIR /go/src

# download packages
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg \
# bind mount go.mod and go.sum
# use cache for go packages
RUN --mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
go mod download

# add source
COPY . .

# setup go
ENV GO_SKIP_GENERATE=1\
GO_BUILD_FLAGS="-tags static -v " \
Expand All @@ -35,6 +35,8 @@ ENV GO_SKIP_GENERATE=1\
BIN_OUT_DIR="/bin"

# build binary
# bind mount source code
# use cache for go packages
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
Expand Down

0 comments on commit 7eef4bf

Please sign in to comment.