Files
gravl/scripts/build-check.sh

107 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# Gravl Build Status Checker
#
# Purpose:
# Verifies that deployed containers match the current git HEAD.
# Warns if containers are stale (built from older commits).
# Helps you catch situations where code was updated but not redeployed.
#
# How it works:
# 1. Gets current local git commit (HEAD)
# 2. Queries each container's build labels
# 3. Compares container label commit vs local HEAD
# 4. Reports status: "OK", "STALE", or "WARNING"
#
# Exit codes:
# 0 = All checks completed (see output for individual status)
# (Warnings don't cause non-zero exit)
#
# Usage:
# ./scripts/build-check.sh
#
# Example output:
# Local HEAD: abc1234 (abc1234567890abcdef...)
#
# [gravl-backend] Built: abc1234 on 2026-03-03T18:21:00Z
# [gravl-backend] OK: up to date
# [gravl-frontend] Built: abc1234 on 2026-03-03T18:21:00Z
# [gravl-frontend] OK: up to date
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")"
cd "$REPO_DIR"
# Get the current local git commit (what's checked out locally)
LOCAL_COMMIT=$(git rev-parse HEAD)
echo "Local HEAD: $(git rev-parse --short HEAD) ($LOCAL_COMMIT)"
echo ""
# ============================================================================
# check() helper function
# ============================================================================
# Queries a container's build labels and compares against local HEAD.
#
# Parameters:
# $1 = Container name (e.g., "gravl-backend")
#
# Label fields used:
# org.opencontainers.image.revision = commit hash when image was built
# Format: 40-character SHA (same as git rev-parse HEAD)
# Set by: scripts/deploy.sh -> docker compose build args
#
# org.opencontainers.image.created = RFC3339 timestamp when image was built
# Format: 2026-03-03T18:21:00Z
# Set by: scripts/deploy.sh -> docker compose build args
# Purpose: Shows humans when the image was built (for diagnostics)
#
# Status outcomes:
# - "Not running": Container doesn't exist or isn't running
# - "WARNING": Container exists but has no revision label
# Fix: Re-deploy with scripts/deploy.sh
# - "OK": Container label commit = local HEAD (up to date)
# - "STALE": Container label commit != local HEAD
# Fix: Run scripts/deploy.sh to update container
check() {
local name="$1"
# Check if container exists and is running
if ! docker inspect "$name" &>/dev/null; then
echo "[$name] Not running"
return
fi
# Extract build labels from container config
# These labels are set in the docker-compose.yml build args,
# and the Dockerfile COPYs them into image labels.
local commit date
commit=$(docker inspect "$name" --format '{{index .Config.Labels "org.opencontainers.image.revision"}}' 2>/dev/null)
date=$(docker inspect "$name" --format '{{index .Config.Labels "org.opencontainers.image.created"}}' 2>/dev/null)
# Check if revision label exists
if [ -z "$commit" ] || [ "$commit" = "unknown" ]; then
echo "[$name] WARNING: no build label found — redeploy with scripts/deploy.sh to add tracking"
return
fi
# Display when this container's image was built
echo "[$name] Built: ${commit:0:7} on ${date:-unknown}"
# Compare container's commit against local HEAD
# If they match, container is up to date.
# If they differ, code has changed locally but container hasn't been redeployed.
if [ "$commit" = "$LOCAL_COMMIT" ]; then
echo "[$name] ✓ OK: up to date"
else
echo "[$name] ⚠ STALE: container is behind local code — run scripts/deploy.sh"
fi
}
# ============================================================================
# Check Each Service
# ============================================================================
# These are the service names defined in docker-compose.yml.
# Adjust if you rename services.
check "gravl-backend"
check "gravl-frontend"