Skip to content

Commit

Permalink
Tune docker build cache
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Rabot <[email protected]>
  • Loading branch information
sylr committed Jul 7, 2024
1 parent 4e7055e commit b4902cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
33 changes: 20 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@ ARG GO_VERSION=1.22

FROM --platform=$BUILDPLATFORM golang:$GO_VERSION AS builder

RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y build-essential git
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get dist-upgrade -y && apt-get install -y build-essential git

WORKDIR $GOPATH/src/sylr.dev/yage

COPY go.mod go.sum ./

RUN go mod download

COPY . .
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download

ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

# Switch shell to bash
SHELL ["bash", "-c"]

# Run a git command otherwise git describe in the Makefile could report a dirty git dir
RUN git diff --exit-code || true

RUN make build GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOARM=${TARGETVARIANT/v/} GO_BUILD_TARGET=dist/${TARGETPLATFORM}/yage
SHELL ["bash", "-c"]

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,target=. \
git diff --exit-code || true; \
make build \
GOOS=${TARGETOS} \
GOARCH=${TARGETARCH} \
GOARM=${TARGETVARIANT/v/} \
GO_BUILD_TARGET=/tmp/dist/${TARGETPLATFORM}/yage \
GO_BUILD_DIR=/tmp/dist/${TARGETPLATFORM} \
GO_BUILD_FLAGS_TARGET=/tmp/.go-build-flags

# -----------------------------------------------------------------------------

Expand All @@ -35,6 +42,6 @@ ARG TARGETPLATFORM

WORKDIR /usr/local/bin

COPY --from=builder "/go/src/sylr.dev/yage/dist/$TARGETPLATFORM/yage" .
COPY --from=builder "/tmp/dist/$TARGETPLATFORM/yage" .

CMD ["/usr/local/bin/yage"]
47 changes: 24 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GO ?= $(shell which go)
GIT_UPDATE_INDEX := $(shell git update-index --refresh)
GIT_UPDATE_INDEX ?= $(shell git update-index --refresh)
GIT_REVISION ?= $(shell git rev-parse HEAD)
GIT_VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)

Expand All @@ -15,17 +15,18 @@ GO_BUILD_TAGS := static
GO_BUILD_TARGET_DEPS :=
GO_BUILD_FLAGS := -trimpath
GO_BUILD_LDFLAGS_OPTIMS :=
GO_BUILD_DIR ?= dist

ifeq ($(GOOS)/$(GOARCH),$(GOENV_GOOS)/$(GOENV_GOARCH))
GO_BUILD_TARGET ?= dist/yage
GO_BUILD_VERSION_TARGET ?= dist/yage-$(GIT_VERSION)
GO_BUILD_TARGET ?= $(GO_BUILD_DIR)/yage
GO_BUILD_VERSION_TARGET ?= $(GO_BUILD_DIR)/yage-$(GIT_VERSION)
else
ifeq ($(GOARCH),arm)
GO_BUILD_TARGET ?= dist/yage-$(GOOS)-$(GOARCH)v$(GOARM)
GO_BUILD_VERSION_TARGET := dist/yage-$(GIT_VERSION)-$(GOOS)-$(GOARCH)v$(GOARM)
GO_BUILD_TARGET ?= $(GO_BUILD_DIR)/yage-$(GOOS)-$(GOARCH)v$(GOARM)
GO_BUILD_VERSION_TARGET := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-$(GOOS)-$(GOARCH)v$(GOARM)
else
GO_BUILD_TARGET ?= dist/yage-$(GOOS)-$(GOARCH)
GO_BUILD_VERSION_TARGET := dist/yage-$(GIT_VERSION)-$(GOOS)-$(GOARCH)
GO_BUILD_TARGET ?= $(GO_BUILD_DIR)/yage-$(GOOS)-$(GOARCH)
GO_BUILD_VERSION_TARGET := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-$(GOOS)-$(GOARCH)
endif # ($(GOARCH),arm)
endif # ($(GOOS)/$(GOARCH),$(GOENV_GOOS)/$(GOENV_GOARCH))

Expand All @@ -35,7 +36,7 @@ else
GO_BUILD_LDFLAGS_OPTIMS += -s -w
endif # $(DEBUG)

GO_BUILD_FLAGS_TARGET := .go-build-flags
GO_BUILD_FLAGS_TARGET ?= .go-build-flags
GO_CROSSBUILD_WINDOWS_PLATFORMS := windows/386 windows/amd64 windows/arm windows/arm64
GO_CROSSBUILD_PLATFORMS ?= linux/386 linux/amd64 linux/arm linux/arm64 linux/arm/v7 linux/arm/v6 \
linux/mips linux/mips/softfloat linux/mips64 linux/mips64le linux/mipsle \
Expand All @@ -62,19 +63,19 @@ GO_CROSSBUILD_RISCV64_PLATFORMS := $(filter %/riscv64,$(GO_CROSSBUILD_PLAT
GO_CROSSBUILD_S390X_PLATFORMS := $(filter %/s390x,$(GO_CROSSBUILD_PLATFORMS))
GO_CROSSBUILD_WINDOWS_PLATFORMS := $(filter windows/%,$(GO_CROSSBUILD_WINDOWS_PLATFORMS))

GO_CROSSBUILD_386_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-386
GO_CROSSBUILD_AMD64_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-amd64
GO_CROSSBUILD_ARM_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-arm
GO_CROSSBUILD_ARM64_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-arm64
GO_CROSSBUILD_ARMV6_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-armv6
GO_CROSSBUILD_ARMV7_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-armv7
GO_CROSSBUILD_MIPS_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-mips
GO_CROSSBUILD_MIPSLE_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-mipsle
GO_CROSSBUILD_MIPS64_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-mips64
GO_CROSSBUILD_MIPS64LE_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-mips64le
GO_CROSSBUILD_RISCV64_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-riscv64
GO_CROSSBUILD_S390X_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-%-s390x
GO_CROSSBUILD_WINDOWS_TARGET_PATTERN := dist/yage-$(GIT_VERSION)-windows-%.exe
GO_CROSSBUILD_386_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-386
GO_CROSSBUILD_AMD64_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-amd64
GO_CROSSBUILD_ARM_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-arm
GO_CROSSBUILD_ARM64_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-arm64
GO_CROSSBUILD_ARMV6_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-armv6
GO_CROSSBUILD_ARMV7_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-armv7
GO_CROSSBUILD_MIPS_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-mips
GO_CROSSBUILD_MIPSLE_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-mipsle
GO_CROSSBUILD_MIPS64_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-mips64
GO_CROSSBUILD_MIPS64LE_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-mips64le
GO_CROSSBUILD_RISCV64_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-riscv64
GO_CROSSBUILD_S390X_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-%-s390x
GO_CROSSBUILD_WINDOWS_TARGET_PATTERN := $(GO_BUILD_DIR)/yage-$(GIT_VERSION)-windows-%.exe

GO_CROSSBUILD_TARGETS := $(patsubst %/386,$(GO_CROSSBUILD_386_TARGET_PATTERN),$(GO_CROSSBUILD_386_PLATFORMS))
GO_CROSSBUILD_TARGETS += $(patsubst %/amd64,$(GO_CROSSBUILD_AMD64_TARGET_PATTERN),$(GO_CROSSBUILD_AMD64_PLATFORMS))
Expand Down Expand Up @@ -203,9 +204,9 @@ $(GO_CROSSBUILD_RISCV64_TARGET_PATTERN): $(GO_BUILD_SRC) $(GO_BUILD_FLAGS_TARGET
$(GO_CROSSBUILD_S390X_TARGET_PATTERN): $(GO_BUILD_SRC) $(GO_BUILD_FLAGS_TARGET)
CGO_ENABLED=0 GOOS=$* GOARCH=s390x $(GO) build -tags $(GO_BUILD_TAGS),crossbuild $(GO_BUILD_FLAGS) $(GO_BUILD_LDFLAGS) -o $@

crossbuild-checksums: dist/checksums
crossbuild-checksums: $(GO_BUILD_DIR)/checksums

dist/checksums : $(GO_CROSSBUILD_TARGETS)
$(GO_BUILD_DIR)/checksums : $(GO_CROSSBUILD_TARGETS)
cd dist && shasum -a 256 yage-*-* > checksums

# -- go mod --------------------------------------------------------------------
Expand Down

0 comments on commit b4902cc

Please sign in to comment.