Skip to content

Commit 5933800

Browse files
committed
feat(docker): add local Docker build script for Tux
- Introduced a new script `docker-build.sh` to facilitate local Docker image builds with versioning options. - The script supports auto-detection of version from git tags, custom tags, and build targets (production/dev). - Includes detailed usage instructions and error handling for invalid inputs. - Implements version verification post-build to ensure consistency between the image and specified version.
1 parent 8e92732 commit 5933800

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

scripts/docker-build.sh

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/env bash
2+
# Local Docker build script for Tux
3+
# Builds Docker image with version baked in from git tags or manual override
4+
#
5+
# Usage:
6+
# ./scripts/docker-build.sh # Auto-detect version from git
7+
# ./scripts/docker-build.sh 1.2.3 # Use specific version
8+
# ./scripts/docker-build.sh --target dev # Build dev target
9+
# ./scripts/docker-build.sh --tag my-tag # Custom image tag
10+
11+
set -euo pipefail
12+
13+
# Default values
14+
TARGET="production"
15+
IMAGE_TAG="tux:latest"
16+
VERSION=""
17+
GIT_SHA=""
18+
BUILD_DATE=""
19+
20+
# Parse arguments
21+
while [[ $# -gt 0 ]]; do
22+
case $1 in
23+
--target)
24+
TARGET="$2"
25+
shift 2
26+
;;
27+
--tag)
28+
IMAGE_TAG="$2"
29+
shift 2
30+
;;
31+
--version)
32+
VERSION="$2"
33+
shift 2
34+
;;
35+
--help|-h)
36+
cat << EOF
37+
Local Docker build script for Tux
38+
39+
Usage:
40+
$0 [OPTIONS] [VERSION]
41+
42+
Arguments:
43+
VERSION Version to bake into image (default: auto-detect from git)
44+
45+
Options:
46+
--target TARGET Docker build target (default: production)
47+
Options: production, dev
48+
--tag TAG Docker image tag (default: tux:latest)
49+
--version VERSION Explicit version (overrides auto-detection)
50+
--help, -h Show this help message
51+
52+
Examples:
53+
$0 # Auto-detect version from git
54+
$0 1.2.3 # Use version 1.2.3
55+
$0 --target dev --tag tux:dev # Build dev target with custom tag
56+
$0 --version \$(git describe --tags) # Use git describe output
57+
58+
Version Detection Priority:
59+
1. --version flag or VERSION argument
60+
2. git describe --tags --always (removes 'v' prefix)
61+
3. git rev-parse --short HEAD (short commit SHA)
62+
4. "dev" (fallback)
63+
64+
EOF
65+
exit 0
66+
;;
67+
-*)
68+
echo "Error: Unknown option $1" >&2
69+
echo "Use --help for usage information" >&2
70+
exit 1
71+
;;
72+
*)
73+
if [ -z "$VERSION" ]; then
74+
VERSION="$1"
75+
else
76+
echo "Error: Multiple version arguments provided" >&2
77+
exit 1
78+
fi
79+
shift
80+
;;
81+
esac
82+
done
83+
84+
# Get script directory
85+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
86+
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
87+
88+
# Change to project root
89+
cd "$PROJECT_ROOT"
90+
91+
# Function to detect version from git
92+
detect_version() {
93+
local version=""
94+
95+
# Try git describe first (tags)
96+
if command -v git >/dev/null 2>&1 && [ -d .git ]; then
97+
if git describe --tags --exact-match >/dev/null 2>&1; then
98+
# Exact tag match
99+
version=$(git describe --tags --exact-match 2>/dev/null | sed 's/^v//')
100+
elif git describe --tags >/dev/null 2>&1; then
101+
# Nearest tag
102+
version=$(git describe --tags 2>/dev/null | sed 's/^v//')
103+
elif git rev-parse --short HEAD >/dev/null 2>&1; then
104+
# Fallback to short commit SHA
105+
version="dev-$(git rev-parse --short HEAD 2>/dev/null)"
106+
fi
107+
fi
108+
109+
# Final fallback
110+
echo "${version:-dev}"
111+
}
112+
113+
# Function to get git SHA
114+
get_git_sha() {
115+
if command -v git >/dev/null 2>&1 && [ -d .git ]; then
116+
git rev-parse HEAD 2>/dev/null || echo ""
117+
else
118+
echo ""
119+
fi
120+
}
121+
122+
# Function to generate build date
123+
get_build_date() {
124+
date -u +'%Y-%m-%dT%H:%M:%SZ'
125+
}
126+
127+
# Detect version if not provided
128+
if [ -z "$VERSION" ]; then
129+
echo "Detecting version from git..."
130+
VERSION=$(detect_version)
131+
fi
132+
133+
# Get git SHA
134+
GIT_SHA=$(get_git_sha)
135+
136+
# Get build date
137+
BUILD_DATE=$(get_build_date)
138+
139+
# Validate version
140+
if [ -z "$VERSION" ]; then
141+
echo "Error: Could not determine version" >&2
142+
exit 1
143+
fi
144+
145+
# Display build information
146+
echo "Building Tux Docker Image"
147+
echo "Version: $VERSION"
148+
echo "Target: $TARGET"
149+
echo "Tag: $IMAGE_TAG"
150+
if [ -n "$GIT_SHA" ]; then
151+
echo "Git SHA: ${GIT_SHA:0:7}"
152+
fi
153+
echo "Build Date: $BUILD_DATE"
154+
echo ""
155+
156+
# Build the image
157+
echo "Building Docker image..."
158+
docker build \
159+
--target "$TARGET" \
160+
--tag "$IMAGE_TAG" \
161+
--build-arg VERSION="$VERSION" \
162+
--build-arg GIT_SHA="$GIT_SHA" \
163+
--build-arg BUILD_DATE="$BUILD_DATE" \
164+
--file Containerfile \
165+
.
166+
167+
# Verify version was baked in
168+
echo ""
169+
echo "Verifying version in image..."
170+
IMAGE_VERSION=$(docker run --rm "$IMAGE_TAG" cat /app/VERSION 2>/dev/null || echo "")
171+
if [ -n "$IMAGE_VERSION" ]; then
172+
if [ "$IMAGE_VERSION" = "$VERSION" ]; then
173+
echo "Version verified: $IMAGE_VERSION"
174+
else
175+
echo "Warning: Version mismatch: expected '$VERSION', found '$IMAGE_VERSION'"
176+
fi
177+
else
178+
echo "Warning: Could not read version from image"
179+
fi
180+
181+
echo ""
182+
echo "Build complete!"
183+
echo "Image: $IMAGE_TAG"
184+
echo ""
185+
echo "To run with Docker Compose:"
186+
echo " TUX_IMAGE=${IMAGE_TAG%%:*} TUX_IMAGE_TAG=${IMAGE_TAG##*:} docker compose --profile production up -d"
187+
echo ""
188+
echo "To run directly:"
189+
echo " docker run --rm $IMAGE_TAG"

0 commit comments

Comments
 (0)