From 7eef4bf6e2d585ee267df6424ce90cfd7e8ea12a Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Fri, 15 Mar 2024 22:22:26 +0100 Subject: [PATCH] Build Cache Optimization (#1402) * 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 --- .github/workflows/makefile.yml | 4 ++++ Dockerfile | 22 ++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml index 8c29e654..a053d1d8 100644 --- a/.github/workflows/makefile.yml +++ b/.github/workflows/makefile.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index 7c34ef91..5cf1adb1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ @@ -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 " \ @@ -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 \