edit function build_image

This commit is contained in:
2025-09-28 12:23:43 +02:00
parent 47229f68f6
commit 159f2284e5

View File

@@ -643,109 +643,103 @@ registry_login() {
echo "$password" | docker login "$url" -u "$username" --password-stdin echo "$password" | docker login "$url" -u "$username" --password-stdin
} }
# Image bauen # Docker-Image bauen
build_image() { build_image() {
local project=$1 local project=$1
local config_file="$PROJECT_CONFIG_DIR/$project/config-file" local config_file="$PROJECT_CONFIG_DIR/$project/config-file"
load_project_config "$config_file" load_project_config "$config_file"
mkdir -p "$LOGS_DIR"
local timestamp local timestamp
timestamp=$(date +"%Y%m%d_%H%M%S") timestamp=$(date +"%Y%m%d_%H%M%S")
local logfile="$LOGS_DIR/${project}_${timestamp}.log" local logfile="$LOGS_DIR/${project}_${timestamp}.log"
echo "==== Build gestartet: $(date) ====" >> "$logfile" echo "==== Build gestartet: $(date) ====" | tee -a "$logfile"
echo "Projekt: $project" >> "$logfile" echo "Projekt: $project" | tee -a "$logfile"
echo "Registries: $registry" >> "$logfile" echo "Registries: $registry" | tee -a "$logfile"
echo "Image: $image_name" >> "$logfile" echo "Image: $image_name" | tee -a "$logfile"
echo "Architekturen: $architectures" >> "$logfile" echo "Architekturen: $architectures" | tee -a "$logfile"
echo "Push: $push" >> "$logfile" echo "Push: $push" | tee -a "$logfile"
echo "Version: $version" >> "$logfile" echo "Version: $version" | tee -a "$logfile"
echo "Latest: $latest" >> "$logfile" echo "Latest: $latest" | tee -a "$logfile"
echo "====================================" >> "$logfile" echo "====================================" | tee -a "$logfile"
# Plattformen zusammenstellen # Architekturen-Array vorbereiten
# Architekturen in Docker-Platform-Syntax übersetzen arch_array=($architectures)
platforms="" platforms=""
for arch in $architectures; do for arch in "${arch_array[@]}"; do
case "$arch" in case "$arch" in
amd64) platforms+="linux/amd64," ;; amd64) platforms+="linux/amd64," ;;
arm64) platforms+="linux/arm64," ;; arm64) platforms+="linux/arm64," ;;
armhf) platforms+="linux/arm/v7," ;; armhf) platforms+="linux/arm/v7," ;;
x86) platforms+="linux/386," ;; x86) platforms+="linux/386," ;;
*) echo "WARNUNG: Unbekannte Architektur '$arch' wird ignoriert." >> "$logfile" ;; *) echo "WARNUNG: Unbekannte Architektur '$arch' wird ignoriert." | tee -a "$logfile" ;;
esac esac
done done
platforms="${platforms%,}"
# Letztes Komma abschneiden if [[ -z "$platforms" ]]; then
platforms="${platforms%,}" echo "Keine Architektur ausgewählt. Build abgebrochen." | tee -a "$logfile"
echo "DEBUG architectures='$architectures'" >> "$logfile" return 1
fi
if [[ -z "$platforms" ]]; then echo "DEBUG platforms='$platforms'" | tee -a "$logfile"
echo "Keine Architektur ausgewählt. Build abgebrochen." | tee -a "$logfile"
return 1
fi
# Tags für alle registries zusammenstellen # Tags für alle Registries vorbereiten
local tags=() IFS=',' read -ra reg_array <<< "$registry"
for reg in $registry; do tags=()
local reg_url for reg in "${reg_array[@]}"; do
reg_url=$(get_registry_url "$reg") reg=$(echo "$reg" | xargs) # trim
if [[ -z "$reg_url" ]]; then [[ -z "$reg" ]] && continue
whiptail --msgbox "Registry '$reg' konnte nicht aufgelöst werden. Build abgebrochen." 10 60
return # 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 fi
tags+=("-t" "$reg_url/$image_name:$version")
tags+=("-t" "$reg/$image_name:$version")
if [[ "$latest" == "yes" ]]; then if [[ "$latest" == "yes" ]]; then
tags+=("-t" "$reg_url/$image_name:latest") tags+=("-t" "$reg/$image_name:latest")
fi fi
done done
if [[ ${#tags[@]} -eq 0 ]]; then if [[ ${#tags[@]} -eq 0 ]]; then
whiptail --msgbox "Keine gültigen Registries ausgewählt. Build abgebrochen." 10 60 echo "Keine Registry-Tags definiert. Build abgebrochen." | tee -a "$logfile"
return return 1
fi fi
# Build-Befehl zusammenstellen # Build-Befehl zusammenbauen
local build_cmd
build_cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "./projects/$project")
if [[ "$push" == "yes" ]]; then if [[ "$push" == "yes" ]]; then
build_cmd+=(--push) cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "$PROJECTS_DIR/$project" --push)
else else
build_cmd+=(--load) cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "$PROJECTS_DIR/$project" --load)
fi fi
# Docker Login für jede Registry durchführen echo "${cmd[*]}" | tee -a "$logfile"
for reg in $registry; do
if ! registry_login "$reg" >> "$logfile" 2>&1; then if "${cmd[@]}" >>"$logfile" 2>&1; then
echo "Login bei Registry '$reg' fehlgeschlagen!" >> "$logfile" echo "==== Build beendet: $(date) ====" | tee -a "$logfile"
whiptail --msgbox "Login bei Registry '$reg' fehlgeschlagen! Build abgebrochen." 10 70
return # 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 fi
done else
echo "==== Build fehlgeschlagen: $(date) ====" | tee -a "$logfile"
# Log-Ausgabe return 1
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
fi 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) # Logs ansehen (optional gefiltert nach Projekt)