diff --git a/scripts/image-builder.sh b/scripts/image-builder.sh index f893e22..18a12b3 100755 --- a/scripts/image-builder.sh +++ b/scripts/image-builder.sh @@ -643,109 +643,103 @@ registry_login() { echo "$password" | docker login "$url" -u "$username" --password-stdin } -# Image bauen +# Docker-Image bauen build_image() { local project=$1 local config_file="$PROJECT_CONFIG_DIR/$project/config-file" load_project_config "$config_file" - mkdir -p "$LOGS_DIR" local timestamp timestamp=$(date +"%Y%m%d_%H%M%S") local logfile="$LOGS_DIR/${project}_${timestamp}.log" - echo "==== Build gestartet: $(date) ====" >> "$logfile" - echo "Projekt: $project" >> "$logfile" - echo "Registries: $registry" >> "$logfile" - echo "Image: $image_name" >> "$logfile" - echo "Architekturen: $architectures" >> "$logfile" - echo "Push: $push" >> "$logfile" - echo "Version: $version" >> "$logfile" - echo "Latest: $latest" >> "$logfile" - echo "====================================" >> "$logfile" + echo "==== Build gestartet: $(date) ====" | tee -a "$logfile" + echo "Projekt: $project" | tee -a "$logfile" + echo "Registries: $registry" | tee -a "$logfile" + echo "Image: $image_name" | tee -a "$logfile" + echo "Architekturen: $architectures" | tee -a "$logfile" + echo "Push: $push" | tee -a "$logfile" + echo "Version: $version" | tee -a "$logfile" + echo "Latest: $latest" | tee -a "$logfile" + echo "====================================" | tee -a "$logfile" - # Plattformen zusammenstellen - # Architekturen in Docker-Platform-Syntax übersetzen -platforms="" -for arch in $architectures; do - case "$arch" in - amd64) platforms+="linux/amd64," ;; - arm64) platforms+="linux/arm64," ;; - armhf) platforms+="linux/arm/v7," ;; - x86) platforms+="linux/386," ;; - *) echo "WARNUNG: Unbekannte Architektur '$arch' wird ignoriert." >> "$logfile" ;; - esac -done + # Architekturen-Array vorbereiten + arch_array=($architectures) + platforms="" + for arch in "${arch_array[@]}"; do + case "$arch" in + amd64) platforms+="linux/amd64," ;; + arm64) platforms+="linux/arm64," ;; + armhf) platforms+="linux/arm/v7," ;; + x86) platforms+="linux/386," ;; + *) echo "WARNUNG: Unbekannte Architektur '$arch' wird ignoriert." | tee -a "$logfile" ;; + esac + done + platforms="${platforms%,}" -# Letztes Komma abschneiden -platforms="${platforms%,}" -echo "DEBUG architectures='$architectures'" >> "$logfile" + if [[ -z "$platforms" ]]; then + echo "Keine Architektur ausgewählt. Build abgebrochen." | tee -a "$logfile" + return 1 + fi -if [[ -z "$platforms" ]]; then - echo "Keine Architektur ausgewählt. Build abgebrochen." | tee -a "$logfile" - return 1 -fi + echo "DEBUG platforms='$platforms'" | tee -a "$logfile" - # Tags für alle registries zusammenstellen - local tags=() - for reg in $registry; do - local reg_url - reg_url=$(get_registry_url "$reg") - if [[ -z "$reg_url" ]]; then - whiptail --msgbox "Registry '$reg' konnte nicht aufgelöst werden. Build abgebrochen." 10 60 - return + # Tags für alle Registries vorbereiten + IFS=',' read -ra reg_array <<< "$registry" + tags=() + for reg in "${reg_array[@]}"; do + reg=$(echo "$reg" | xargs) # trim + [[ -z "$reg" ]] && continue + + # Docker-Login für Registry (falls Config vorhanden) + reg_config="$CONFIG_DIR/registries/$reg/config-file" + if [[ -f "$reg_config" ]]; then + reg_user=$(grep "^username=" "$reg_config" | cut -d= -f2) + reg_pass=$(grep "^password=" "$reg_config" | cut -d= -f2) + if [[ -n "$reg_user" && -n "$reg_pass" ]]; then + echo "$reg_pass" | docker login "$reg" -u "$reg_user" --password-stdin 2>>"$logfile" || { + echo "WARNUNG: Login bei $reg fehlgeschlagen!" | tee -a "$logfile" + } + fi fi - tags+=("-t" "$reg_url/$image_name:$version") + + tags+=("-t" "$reg/$image_name:$version") if [[ "$latest" == "yes" ]]; then - tags+=("-t" "$reg_url/$image_name:latest") + tags+=("-t" "$reg/$image_name:latest") fi done if [[ ${#tags[@]} -eq 0 ]]; then - whiptail --msgbox "Keine gültigen Registries ausgewählt. Build abgebrochen." 10 60 - return + echo "Keine Registry-Tags definiert. Build abgebrochen." | tee -a "$logfile" + return 1 fi - # Build-Befehl zusammenstellen - local build_cmd - build_cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "./projects/$project") + # Build-Befehl zusammenbauen if [[ "$push" == "yes" ]]; then - build_cmd+=(--push) + cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "$PROJECTS_DIR/$project" --push) else - build_cmd+=(--load) + cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "$PROJECTS_DIR/$project" --load) fi - # Docker Login für jede Registry durchführen - for reg in $registry; do - if ! registry_login "$reg" >> "$logfile" 2>&1; then - echo "Login bei Registry '$reg' fehlgeschlagen!" >> "$logfile" - whiptail --msgbox "Login bei Registry '$reg' fehlgeschlagen! Build abgebrochen." 10 70 - return + echo "${cmd[*]}" | tee -a "$logfile" + + if "${cmd[@]}" >>"$logfile" 2>&1; then + echo "==== Build beendet: $(date) ====" | tee -a "$logfile" + + # Auto-Subversion + if [[ "$auto_subversion" == "yes" ]]; then + local major minor + major=$(echo "$version" | cut -d. -f1) + minor=$(echo "$version" | cut -d. -f2) + minor=$((minor + 1)) + new_version="${major}.${minor}" + sed -i "s/^version=.*/version=$new_version/" "$config_file" + echo "Subversion automatisch auf $new_version erhöht." | tee -a "$logfile" fi - done - - # Log-Ausgabe - echo "${build_cmd[*]}" >> "$logfile" - - # Build starten - if ! "${build_cmd[@]}" >> "$logfile" 2>&1; then - echo "==== Build fehlgeschlagen: $(date) ====" >> "$logfile" - whiptail --msgbox "Build fehlgeschlagen! Log-Datei: $logfile" 10 70 - return + else + echo "==== Build fehlgeschlagen: $(date) ====" | tee -a "$logfile" + return 1 fi - - echo "==== Build beendet: $(date) ====" >> "$logfile" - - # Subversion automatisch erhöhen - if grep -q "^auto_subversion=yes" "$config_file"; then - IFS='.' read -r main sub <<< "$version" - sub=$((sub + 1)) - new_version="$main.$sub" - sed -i "s|^version=.*|version=$new_version|" "$config_file" - echo "Subversion automatisch auf $new_version erhöht." >> "$logfile" - fi - - whiptail --msgbox "Build erfolgreich abgeschlossen!\nLog-Datei: $logfile" 10 70 } # Logs ansehen (optional gefiltert nach Projekt)