109 lines
3.2 KiB
Bash
Executable File
109 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
########################################
|
|
# Config (edit these)
|
|
########################################
|
|
REGISTRY="git.sneakygeek.net"
|
|
NAMESPACE="sneakygeek"
|
|
IMAGE_NAME="sneakymon"
|
|
|
|
# A helpful pointer/reference for your repo URL. Not used by docker commands,
|
|
# but kept for clarity and future automation if you want to clone/build elsewhere.
|
|
GIT_REPO_URL="https://git.sneakygeek.net/sneakygeek/sneakymon.git"
|
|
|
|
# If you prefer to override the tag manually sometimes:
|
|
# export DOCKER_TAG=my-custom-tag
|
|
########################################
|
|
|
|
# Colors
|
|
BOLD='\033[1m'; DIM='\033[2m'; GREEN='\033[32m'; YELLOW='\033[33m'; RED='\033[31m'; NC='\033[0m'
|
|
|
|
# Helpers
|
|
confirm() {
|
|
# Usage: confirm "Question?" default_yes|default_no
|
|
local prompt response default
|
|
if [[ "${2:-default_no}" == "default_yes" ]]; then
|
|
prompt=" [Y/n] "
|
|
default="y"
|
|
else
|
|
prompt=" [y/N] "
|
|
default="n"
|
|
fi
|
|
read -r -p "$1$prompt" response || true
|
|
response="${response:-$default}"
|
|
[[ "$response" =~ ^[Yy]$ ]]
|
|
}
|
|
|
|
docker_tag_sanitize() {
|
|
# Docker tag must be <=128 chars, allowed: [A-Za-z0-9_.-], usually lowercase for sanity
|
|
local raw="$1"
|
|
local lower="${raw,,}"
|
|
local safe
|
|
safe="$(printf '%s' "$lower" | tr -c 'a-z0-9_.-' '-')"
|
|
printf '%.128s' "$safe"
|
|
}
|
|
|
|
detect_git_tag() {
|
|
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
# Branch name or fallback to describe if detached
|
|
local ref
|
|
ref="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
|
|
if [[ "$ref" == "HEAD" || -z "$ref" ]]; then
|
|
# detached HEAD → use nearest tag or short commit
|
|
ref="$(git describe --tags --always --dirty 2>/dev/null || git rev-parse --short HEAD 2>/dev/null || echo 'detached')"
|
|
fi
|
|
docker_tag_sanitize "$ref"
|
|
else
|
|
# Not a git repo → default to "latest" unless DOCKER_TAG is set
|
|
echo "latest"
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
local image_repo="${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}"
|
|
local tag latest_tag push_it push_latest
|
|
|
|
# Determine tag
|
|
if [[ -n "${DOCKER_TAG:-}" ]]; then
|
|
tag="$(docker_tag_sanitize "$DOCKER_TAG")"
|
|
else
|
|
tag="$(detect_git_tag)"
|
|
fi
|
|
|
|
echo -e "${BOLD}Image:${NC} ${image_repo}"
|
|
echo -e "${BOLD}Tag: ${NC} ${GREEN}${tag}${NC}"
|
|
echo -e "${DIM}(git: $(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'n/a'), repo: ${GIT_REPO_URL})${NC}"
|
|
|
|
# Build
|
|
echo -e "\n${BOLD}==> Building ${image_repo}:${tag}${NC}"
|
|
docker build -t "${image_repo}:${tag}" .
|
|
|
|
# Ask about :latest
|
|
if confirm "Also tag as :latest?" default_no; then
|
|
latest_tag="latest"
|
|
echo -e "${BOLD}Tagging:${NC} ${image_repo}:${latest_tag}"
|
|
docker tag "${image_repo}:${tag}" "${image_repo}:${latest_tag}"
|
|
push_latest="yes"
|
|
else
|
|
push_latest="no"
|
|
fi
|
|
|
|
# Ask about pushing
|
|
if confirm "Push the built image(s) now?" default_yes; then
|
|
echo -e "\n${BOLD}==> Pushing ${image_repo}:${tag}${NC}"
|
|
docker push "${image_repo}:${tag}"
|
|
if [[ "$push_latest" == "yes" ]]; then
|
|
echo -e "${BOLD}==> Pushing ${image_repo}:latest${NC}"
|
|
docker push "${image_repo}:latest"
|
|
fi
|
|
echo -e "${GREEN}Done.${NC}"
|
|
else
|
|
echo -e "${YELLOW}Skipping push. Local images created:${NC}"
|
|
echo " - ${image_repo}:${tag}"
|
|
[[ "$push_latest" == "yes" ]] && echo " - ${image_repo}:latest"
|
|
fi
|
|
}
|
|
|
|
main "$@"
|