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
This commit is contained in:
Kwitsch 2024-03-15 22:22:26 +01:00 committed by GitHub
parent ca7497897e
commit 7eef4bf6e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 10 deletions

View File

@ -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

View File

@ -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 \