Skip to content

Commit 59c1343

Browse files
authored
refactor: build binaries on the host to improve build/module cache reuse (#68)
1 parent eb1b396 commit 59c1343

4 files changed

Lines changed: 16 additions & 35 deletions

File tree

.dockerignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# Build artifacts
2-
dist/
3-
**/dist/
1+
# Build artifacts (dist/ is allowed for pre-built binaries)
42

53
# Git
64
.git/

Justfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ fmt:
3939
build GOOS=(GOOS) GOARCH=(GOARCH):
4040
#!/usr/bin/env bash
4141
mkdir -p {{ RELEASE }}
42-
go build -trimpath -o {{ RELEASE }}/cachewd-{{ GOOS }}-{{ GOARCH }} \
42+
CGO_ENABLED=0 GOOS={{ GOOS }} GOARCH={{ GOARCH }} \
43+
go build -trimpath -o {{ RELEASE }}/cachewd-{{ GOOS }}-{{ GOARCH }} \
4344
-ldflags "-s -w -X main.version={{ VERSION }} -X main.gitCommit={{ GIT_COMMIT }}" \
4445
./cmd/cachewd
4546
test "{{ GOOS }}-{{ GOARCH }}" = "$(go env GOOS)-$(go env GOARCH)" && (cd {{ RELEASE }} && ln -sf cachewd-{{ GOOS }}-{{ GOARCH }} cachewd)

docker/Dockerfile

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,27 @@
1-
# Build stage
2-
FROM golang:1.25.5-alpine AS builder
1+
# Runtime image - binaries are pre-built on the host
2+
FROM alpine:3.21
33

4-
ARG VERSION=dev
5-
ARG GIT_COMMIT=unknown
6-
ARG TARGETOS
74
ARG TARGETARCH
85

9-
WORKDIR /build
10-
11-
# Install build dependencies
12-
RUN apk add --no-cache git ca-certificates tzdata
13-
14-
# Copy go mod files first for better caching
15-
COPY go.mod go.sum ./
16-
RUN go mod download
17-
18-
# Copy source code
19-
COPY . .
20-
21-
# Build the binary
22-
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
23-
go build -trimpath -o cachewd \
24-
-ldflags "-s -w -X main.version=${VERSION} -X main.gitCommit=${GIT_COMMIT}" \
25-
./cmd/cachewd
26-
27-
# Runtime stage
28-
FROM alpine:3.21
29-
306
SHELL ["/bin/sh", "-o", "pipefail", "-c"]
317

328
# Install runtime dependencies for git operations and TLS
339
RUN apk add --no-cache ca-certificates git tzdata && \
34-
addgroup -g 1000 cachew && \
35-
adduser -D -u 1000 -G cachew cachew
10+
addgroup -g 1000 cachew && \
11+
adduser -D -u 1000 -G cachew cachew
3612

3713
# Set working directory (config uses relative paths like ./state/cache)
3814
WORKDIR /app
3915

40-
# Copy binary from builder
41-
COPY --from=builder /build/cachewd /usr/local/bin/cachewd
16+
# Copy pre-built binary from host (built by Justfile)
17+
COPY dist/cachewd-linux-${TARGETARCH} /usr/local/bin/cachewd
4218

4319
# Copy default configuration file
4420
COPY cachew.hcl /app/cachew.hcl
4521

4622
# Create state directory with proper permissions
4723
RUN mkdir -p /app/state/cache && \
48-
chown -R cachew:cachew /app
24+
chown -R cachew:cachew /app
4925

5026
# Switch to non-root user
5127
USER cachew

docker/Justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ TAG := `git rev-parse --short HEAD 2>/dev/null || echo "dev"`
77
VERSION := `git describe --tags --always --dirty 2>/dev/null || echo "dev"`
88
GIT_COMMIT := `git rev-parse HEAD 2>/dev/null || echo "unknown"`
99
DOCKERFILE := ROOT + "/docker/Dockerfile"
10+
RELEASE := ROOT + "/dist"
1011

1112
# Build Docker image for local development
1213
build:
14+
@echo "Building Linux binary for current architecture..."
15+
@just -f {{ ROOT }}/Justfile build linux
1316
@echo "Building Docker image..."
1417
@docker build -f {{ DOCKERFILE }} \
1518
--build-arg VERSION={{ VERSION }} \
@@ -20,6 +23,9 @@ build:
2023

2124
# Build multi-arch Docker image (builds for amd64 + arm64, stays in cache)
2225
build-multi:
26+
@echo "Building Linux binaries for amd64 and arm64..."
27+
@just -f {{ ROOT }}/Justfile build linux amd64
28+
@just -f {{ ROOT }}/Justfile build linux arm64
2329
@docker buildx create --use --driver docker-container --driver-opt image=moby/buildkit:v0.17.3 --name multi-arch-cachew 2>/dev/null || docker buildx use multi-arch-cachew
2430
@echo "Building multi-arch image..."
2531
@docker buildx build \

0 commit comments

Comments
 (0)