Skip to content

Commit

Permalink
Add go cache for improvement docker build performance (#2297)
Browse files Browse the repository at this point in the history
* 🐳 Add go cache for improvement docker build performance

Signed-off-by: vankichi <[email protected]>

* 🐳 Fix

Signed-off-by: vankichi <[email protected]>

* 🐳 Fix

Signed-off-by: vankichi <[email protected]>

* 🐳 Add skip clean deps flag

Signed-off-by: vankichi <[email protected]>

* 🐳 Fix

Signed-off-by: vankichi <[email protected]>

* 🐳 Fix

Signed-off-by: vankichi <[email protected]>

* 🐳 remove

Signed-off-by: vankichi <[email protected]>

* 🐳 convert /tmp to /home/vankichi

Signed-off-by: vankichi <[email protected]>

---------

Signed-off-by: vankichi <[email protected]>
  • Loading branch information
vankichi authored Jan 23, 2024
1 parent 2ca7187 commit f68adfa
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 44 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ GOARCH := $(eval GOARCH := $(shell go env GOARCH))$(GOARCH)
GOBIN := $(eval GOBIN := $(or $(shell go env GOBIN),$(GOPATH)/bin))$(GOBIN)
GOCACHE := $(eval GOCACHE := $(shell go env GOCACHE))$(GOCACHE)
GOOS := $(eval GOOS := $(shell go env GOOS))$(GOOS)
GO_CLEAN_DEPS := true
GOTEST_TIMEOUT = 30m
TEST_NOT_IMPL_PLACEHOLDER = NOT IMPLEMENTED BELOW

Expand Down
29 changes: 16 additions & 13 deletions Makefile.d/dependencies.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ go/download:
.PHONY: go/deps
## install Go package dependencies
go/deps:
rm -rf $(ROOTDIR)/vendor \
/go/pkg \
$(GOCACHE) \
$(ROOTDIR)/go.sum \
$(ROOTDIR)/go.mod
cp $(ROOTDIR)/hack/go.mod.default $(ROOTDIR)/go.mod
GOPRIVATE=$(GOPRIVATE) go mod tidy
go clean -cache -modcache -testcache -i -r
rm -rf $(ROOTDIR)/vendor \
/go/pkg \
$(GOCACHE) \
$(ROOTDIR)/go.sum \
$(ROOTDIR)/go.mod
if $(GO_CLEAN_DEPS); then \
rm -rf $(ROOTDIR)/vendor \
/go/pkg \
$(GOCACHE) \
$(ROOTDIR)/go.sum \
$(ROOTDIR)/go.mod ; \
cp $(ROOTDIR)/hack/go.mod.default $(ROOTDIR)/go.mod ; \
GOPRIVATE=$(GOPRIVATE) go mod tidy ; \
go clean -cache -modcache -testcache -i -r ; \
rm -rf $(ROOTDIR)/vendor \
/go/pkg \
$(GOCACHE) \
$(ROOTDIR)/go.sum \
$(ROOTDIR)/go.mod ; \
cp $(ROOTDIR)/hack/go.mod.default $(ROOTDIR)/go.mod ; \
fi
cp $(ROOTDIR)/hack/go.mod.default $(ROOTDIR)/go.mod
GOPRIVATE=$(GOPRIVATE) go mod tidy
go get -u all 2>/dev/null || true
Expand Down
11 changes: 9 additions & 2 deletions dockers/agent/core/ngt/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang

FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -65,7 +68,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -87,7 +92,9 @@ COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make ngt/install
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/cmd/${PKG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/agent/sidecar/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang

FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV GOPATH /go
ENV GOROOT /opt/go
Expand Down Expand Up @@ -58,7 +61,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -79,7 +84,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/versions
COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

FROM --platform=${BUILDPLATFORM} ${DISTROLESS_IMAGE}:${DISTROLESS_IMAGE_TAG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/ci/base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang
FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder
LABEL maintainer="${MAINTAINER}"

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -71,9 +74,13 @@ COPY apis/proto apis/proto
COPY versions versions
COPY hack/go.mod.default hack/go.mod.default

RUN make deps ROOTDIR=$ROOTDIR
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make deps ROOTDIR=$ROOTDIR GO_CLEAN_DEPS=false

RUN make ngt/install \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make ngt/install \
&& make helm/install \
&& make helm-docs/install \
&& make kind/install \
Expand Down
12 changes: 10 additions & 2 deletions dockers/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ ARG MAINTAINER="vdaas.org vald team <[email protected]>"
FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/vscode/devcontainers/go:1 AS base
LABEL maintainer="${MAINTAINER}"

ARG TARGETARCH

ENV ARCH=${TARGETARCH}

# skipcq: DOK-DL3008
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
Expand Down Expand Up @@ -51,7 +55,9 @@ COPY versions versions
COPY hack/go.mod.default hack/go.mod.default

# basic deps
RUN make deps \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make deps GO_CLEAN_DEPS=false \
&& make ngt/install \
&& make helm/install \
&& make helm-docs/install \
Expand All @@ -62,7 +68,9 @@ RUN make deps \
&& make kubectl/install

# additional deps
RUN make k3d/install \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make k3d/install \
&& make buf/install \
&& make k9s/install \
&& make minikube/install
11 changes: 9 additions & 2 deletions dockers/discoverer/k8s/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang

FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -58,7 +61,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -76,7 +81,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/versions
COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/cmd/${PKG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/gateway/filter/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang

FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -57,7 +60,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -75,7 +80,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/versions
COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/cmd/${PKG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/gateway/lb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang

FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -57,7 +60,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -75,7 +80,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/versions
COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/cmd/${PKG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/gateway/mirror/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ FROM golang:${GO_VERSION} AS golang

FROM ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -53,7 +56,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
COPY go.mod .
COPY go.sum .

RUN go mod download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
go mod download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -77,7 +82,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
COPY Makefile .
COPY .git .

RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

FROM ${DISTROLESS_IMAGE}:${DISTROLESS_IMAGE_TAG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/index/job/correction/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang

FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -57,7 +60,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -75,7 +80,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/versions
COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/cmd/${PKG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/index/job/creation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION} AS golang

FROM --platform=${BUILDPLATFORM} ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -57,7 +60,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -75,7 +80,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/versions
COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/cmd/${PKG}
Expand Down
11 changes: 9 additions & 2 deletions dockers/index/job/readreplica/rotate/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ FROM golang:${GO_VERSION} AS golang

FROM ubuntu:devel AS builder

ARG TARGETARCH

ENV ARCH=${TARGETARCH}
ENV GO111MODULE on
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
Expand Down Expand Up @@ -57,7 +60,9 @@ COPY .git .
COPY go.mod .
COPY go.sum .

RUN make go/download
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make go/download

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/internal
COPY internal .
Expand All @@ -75,7 +80,9 @@ WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/versions
COPY versions .

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}
RUN make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
RUN --mount=type=cache,target="${GOPATH}/pkg",id="go-build-${ARCH}" \
--mount=type=cache,target="${HOME}/.cache/go-build",id="go-build-${ARCH}" \
make REPO=${ORG} NAME=${REPO} cmd/${PKG}/${APP_NAME} \
&& mv "cmd/${PKG}/${APP_NAME}" "/usr/bin/${APP_NAME}"

WORKDIR ${GOPATH}/src/github.com/${ORG}/${REPO}/cmd/${PKG}
Expand Down
Loading

0 comments on commit f68adfa

Please sign in to comment.