Refactor: Enhance container image management with intelligent auto-discovery and multi-architecture support

This commit is contained in:
sHa
2025-09-28 19:22:40 +03:00
parent 38cca103b1
commit 5262d042f8
4 changed files with 717 additions and 135 deletions

134
core.just
View File

@@ -5,6 +5,127 @@ _discover_project:
#!/usr/bin/env bash
echo "$(basename $(pwd))"
# Enhanced project name detection with priority order
# Priority: PROJECT_NAME env var > auto-detection > folder name
_get_project_name:
#!/usr/bin/env bash
set -euo pipefail
# 1. Check PROJECT_NAME environment variable (highest priority)
if [ -n "${PROJECT_NAME:-}" ]; then
echo "$PROJECT_NAME"
exit 0
fi
# 2. Auto-detection logic
project_name=""
# Check if we're in root with single Containerfile
if [ -f "Containerfile" ] || [ -f "Dockerfile" ] || [ -f "docker/Containerfile" ] || [ -f "docker/Dockerfile" ]; then
# Check for multiple projects (subdirectories with Containerfiles)
subproject_count=0
for dir in */; do
if [ -d "$dir" ] && ([ -f "${dir}Containerfile" ] || [ -f "${dir}Dockerfile" ]); then
subproject_count=$((subproject_count + 1))
fi
done
if [ $subproject_count -eq 0 ]; then
# Single project in root
project_name="$(basename $(pwd))"
else
# Multiple subprojects detected - this should be handled per-project
project_name="$(basename $(pwd))"
fi
else
# No Containerfile in root, use current directory name
project_name="$(basename $(pwd))"
fi
# 3. Fallback to current folder basename
if [ -z "$project_name" ]; then
project_name="$(basename $(pwd))"
fi
echo "$project_name"
# Get full image name with priority logic
# Priority: IMAGE_NAME env var > built from components
_get_image_name tag="":
#!/usr/bin/env bash
set -euo pipefail
tag="{{tag}}"
# 1. Check IMAGE_NAME environment variable (highest priority)
if [ -n "${IMAGE_NAME:-}" ]; then
if [ -n "$tag" ]; then
echo "${IMAGE_NAME}:$tag"
else
echo "$IMAGE_NAME"
fi
exit 0
fi
# 2. Build from components
registry="${REGISTRY:-ghcr.io}"
namespace="${REGISTRY_NAMESPACE:-${GITHUB_USERNAME:-}}"
project_name=$(just _get_project_name)
if [ -z "$namespace" ]; then
echo "Error: REGISTRY_NAMESPACE or GITHUB_USERNAME must be set" >&2
echo "Set REGISTRY_NAMESPACE in .env file for registry operations" >&2
exit 1
fi
if [ -n "$tag" ]; then
echo "$registry/$namespace/$project_name:$tag"
else
echo "$registry/$namespace/$project_name"
fi
# Generate default tag based on git or timestamp
_generate_default_tag:
#!/usr/bin/env bash
set -euo pipefail
if git rev-parse --git-dir >/dev/null 2>&1; then
echo "commit-$(git rev-parse --short HEAD)"
else
echo "build-$(date '+%Y%m%d-%H%M%S')"
fi
# Detect platforms from Containerfile
_detect_platforms containerfile="":
#!/usr/bin/env bash
set -euo pipefail
containerfile="{{containerfile}}"
default_platforms="${DEFAULT_PLATFORMS:-linux/amd64,linux/arm64}"
if [ -z "$containerfile" ]; then
echo "$default_platforms"
exit 0
fi
if [ ! -f "$containerfile" ]; then
echo "$default_platforms"
exit 0
fi
# Check for --platform specification in FROM statements
if grep -q "FROM.*--platform" "$containerfile"; then
# Extract platform from first FROM statement with --platform
platform=$(grep "FROM.*--platform" "$containerfile" | head -1 | sed -n 's/.*--platform=\([^ ]*\).*/\1/p')
if [ -n "$platform" ]; then
echo "$platform"
else
echo "$default_platforms"
fi
else
echo "$default_platforms"
fi
# Auto-discover containerfile and return path and project info
# Usage: _discover_containerfile [directory]
# Returns: containerfile_path project_name build_context
@@ -49,6 +170,19 @@ _discover_containerfile:
# Detect container runtime (Docker or Podman)
_detect_runtime:
#!/usr/bin/env bash
# Check if DOCKER environment variable is set (highest priority)
if [ -n "${DOCKER:-}" ]; then
if command -v "$DOCKER" >/dev/null 2>&1; then
echo "$DOCKER"
exit 0
else
echo "Error: Specified runtime '$DOCKER' not found" >&2
exit 1
fi
fi
# Auto-detect available runtime
if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then
echo "docker"
elif command -v podman >/dev/null 2>&1; then