mirror of
https://github.com/selfhosters-cc/container-census.git
synced 2026-05-24 08:18:45 -05:00
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:
+2
-2
@@ -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
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 \
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user