Pass BUILD_TIME as build arg to Dockerfiles for better caching

Changed Dockerfiles to accept BUILD_TIME as a build argument instead of
generating it inside the Docker build. This allows better layer caching
since the timestamp no longer invalidates the build cache. Updated
build-all-images.sh and collector-build.sh to pass the timestamp.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Self Hosters
2025-12-21 14:09:08 -05:00
parent f8c4214978
commit 77e35fed05
5 changed files with 25 additions and 7 deletions
+2 -2
View File
@@ -24,8 +24,8 @@ COPY . .
RUN go mod tidy -e
# Build the binary with proper tags for Alpine and inject build timestamp
RUN BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") && \
CGO_ENABLED=1 GOOS=linux go build \
ARG BUILD_TIME
RUN CGO_ENABLED=1 GOOS=linux go build \
-buildvcs=false \
-tags "sqlite_omit_load_extension" \
-ldflags "-X github.com/selfhosters-cc/container-census/internal/version.BuildTime=${BUILD_TIME}" \
+2 -2
View File
@@ -24,8 +24,8 @@ COPY . .
RUN go mod tidy -e
# Build the agent binary with build time
RUN BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") && \
CGO_ENABLED=0 GOOS=linux go build -buildvcs=false \
ARG BUILD_TIME
RUN CGO_ENABLED=0 GOOS=linux go build -buildvcs=false \
-ldflags="-w -s -X github.com/selfhosters-cc/container-census/internal/version.BuildTime=${BUILD_TIME}" \
-o census-agent ./cmd/agent
+5 -2
View File
@@ -22,8 +22,11 @@ COPY . .
# Tidy if needed (rarely changes cache)
RUN go mod tidy -e
# Build the telemetry collector binary
RUN CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -ldflags="-w -s" -o telemetry-collector ./cmd/telemetry-collector
# Build the telemetry collector binary with build time injection
ARG BUILD_TIME
RUN CGO_ENABLED=0 GOOS=linux go build -buildvcs=false \
-ldflags="-w -s -X github.com/selfhosters-cc/container-census/internal/version.BuildTime=${BUILD_TIME}" \
-o telemetry-collector ./cmd/telemetry-collector
# Stage 2: Create minimal runtime image
FROM alpine:3.21
+8
View File
@@ -131,6 +131,10 @@ build_image() {
build_args="--build-arg DOCKER_GID=999"
fi
# Add BUILD_TIME for all images
local build_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
build_args="$build_args --build-arg BUILD_TIME=$build_time"
# Determine if this is a multi-platform build
local platform_count=$(echo "$platforms" | tr ',' '\n' | wc -l)
local load_flag=""
@@ -194,6 +198,10 @@ build_and_push() {
build_args="--build-arg DOCKER_GID=999"
fi
# Add BUILD_TIME for all images
local build_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
build_args="$build_args --build-arg BUILD_TIME=$build_time"
docker buildx build \
--platform "$platforms" \
$build_args \
+8 -1
View File
@@ -6,7 +6,14 @@ set -e
cd "$(dirname "$0")/.."
# Get build timestamp in RFC3339 format
BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "Building telemetry collector..."
CGO_ENABLED=1 go build -o /tmp/telemetry-collector ./cmd/telemetry-collector
CGO_ENABLED=1 go build \
-ldflags "-X github.com/selfhosters-cc/container-census/internal/version.BuildTime=${BUILD_TIME}" \
-o /tmp/telemetry-collector \
./cmd/telemetry-collector
echo "Built: /tmp/telemetry-collector"
ls -lh /tmp/telemetry-collector