#!/bin/bash set -e # ============================================================================= # Gitea Docker Deploy Script # Build multi-platform Docker image and push to Gitea container registry # ============================================================================= # --- Load config from file if exists --- CONFIG_FILE=".gitea-deploy" if [[ -f "$CONFIG_FILE" ]]; then source "$CONFIG_FILE" fi # --- Configuration --- # Priority: 1) environment variables, 2) .gitea-deploy file, 3) auto-detect DOMAIN="${GITEA_DOMAIN:-$DOMAIN}" REPO_PATH="${GITEA_REPO:-$REPO_PATH}" PLATFORMS="${DOCKER_PLATFORMS:-${PLATFORMS:-linux/amd64,linux/arm64}}" DOCKERFILE="${DOCKERFILE:-./Dockerfile}" # --- Output colors --- RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } # --- Auto-detect from git remote --- detect_gitea_info() { local remote_url local remote_name="${GIT_REMOTE:-}" # Priority: 1) GIT_REMOTE env, 2) remote "gitea", 3) remote "origin" if [[ -n "$remote_name" ]]; then remote_url=$(git remote get-url "$remote_name" 2>/dev/null || echo "") elif git remote get-url gitea &>/dev/null; then remote_name="gitea" remote_url=$(git remote get-url gitea) log_info "Using remote 'gitea'" else remote_name="origin" remote_url=$(git remote get-url origin 2>/dev/null || echo "") fi if [[ -z "$remote_url" ]]; then log_error "Git remote 'origin' not found" exit 1 fi # Parse URL: support multiple formats # SSH short: git@gitea.luulam.dev:luulam/9router.git # SSH full: ssh://git@192.168.1.10:2222/luulam/9router.git # HTTPS: https://gitea.luulam.dev/luulam/9router.git local detected_domain="" local detected_repo="" if [[ "$remote_url" =~ ^ssh:// ]]; then # SSH full format: ssh://git@host:port/path.git detected_domain=$(echo "$remote_url" | sed 's|ssh://[^@]*@\([^:/]*\).*|\1|') detected_repo=$(echo "$remote_url" | sed 's|ssh://[^/]*/\(.*\)\.git|\1|' | sed 's|\.git$||') elif [[ "$remote_url" =~ ^git@ ]]; then # SSH short format: git@host:path.git detected_domain=$(echo "$remote_url" | sed 's/git@\([^:]*\):.*/\1/') detected_repo=$(echo "$remote_url" | sed 's/git@[^:]*:\(.*\)\.git/\1/' | sed 's/\.git$//') elif [[ "$remote_url" =~ ^https?:// ]]; then # HTTPS format detected_domain=$(echo "$remote_url" | sed 's|https\?://\([^/]*\)/.*|\1|') detected_repo=$(echo "$remote_url" | sed 's|https\?://[^/]*/\(.*\)\.git|\1|' | sed 's/\.git$//') else log_error "Unrecognized git remote format: $remote_url" exit 1 fi # Use detected values if not configured REPO_PATH="${REPO_PATH:-$detected_repo}" # Check if domain is IP -> need to configure DOMAIN if [[ -z "$DOMAIN" ]]; then if [[ "$detected_domain" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then log_error "Git remote points to IP ($detected_domain), not domain." log_error "Please create .gitea-deploy file with:" echo "" echo " DOMAIN=\"gitea.luulam.dev\"" echo " REPO_PATH=\"$detected_repo\"" echo "" exit 1 fi DOMAIN="$detected_domain" fi } # --- Check prerequisites --- check_prerequisites() { log_info "Checking prerequisites..." # Dockerfile if [[ ! -f "$DOCKERFILE" ]]; then log_error "$DOCKERFILE not found" exit 1 fi # Docker daemon if ! docker info > /dev/null 2>&1; then log_error "Docker daemon not running. Please start Docker Desktop." exit 1 fi # Docker buildx if ! docker buildx version > /dev/null 2>&1; then log_error "Docker Buildx not installed." log_info "Install with: docker buildx install" exit 1 fi # Check if builder supports multi-platform local builder_platforms builder_platforms=$(docker buildx inspect --bootstrap 2>/dev/null | grep -i "platforms" || echo "") if [[ -z "$builder_platforms" ]]; then log_warn "Creating new multi-platform builder..." docker buildx create --use --name multiarch-builder --driver docker-container --bootstrap fi log_info "All prerequisites ready!" } # --- Build and Push --- build_and_push() { local git_rev git_rev=$(git rev-parse --short HEAD) local image_base="$DOMAIN/$REPO_PATH" local tag_rev="$image_base:$git_rev" local tag_latest="$image_base:latest" echo "" log_info "==========================================" log_info "Build Docker Image" log_info "==========================================" log_info "Registry: $DOMAIN" log_info "Repository: $REPO_PATH" log_info "Platforms: $PLATFORMS" log_info "Tags: $git_rev, latest" log_info "Dockerfile: $DOCKERFILE" echo "" # Build and push docker buildx build \ --platform "$PLATFORMS" \ --tag "$tag_rev" \ --tag "$tag_latest" \ --file "$DOCKERFILE" \ --push . if [[ $? -eq 0 ]]; then echo "" log_info "==========================================" log_info "Deploy successful!" log_info "==========================================" log_info "Image: $tag_rev" log_info "Image: $tag_latest" echo "" log_info "Pull image:" echo " docker pull $tag_latest" echo "" else log_error "Build failed!" exit 1 fi } # --- Main --- main() { echo "" log_info "Gitea Docker Deploy" echo "" # Detect info if not configured if [[ -z "$DOMAIN" ]] || [[ -z "$REPO_PATH" ]]; then detect_gitea_info fi check_prerequisites build_and_push } main "$@"