dadi-staging-webhook/webhooks-git/dadi-staging-init.sh

91 lines
2.2 KiB
Bash

#!/bin/bash
#
# Initialize dadi staging folder from repository
#
# - delete existing dadi staging folder
#
HTML_HOME="/html"
BRANCHES_FOLDER="/html/branches"
DADI_STAGING_REPOSITORY="git_dadi-staging:AG-IT/KlDaDiSeite.git"
MAX_RETRY_ATTEMPTS=${INIT_RETRY_ATTEMPTS:--1}
RETRY_DELAY=${INIT_RETRY_DELAY:-2}
#
# Funktion: Warte bis Git-Repository erreichbar ist
#
wait_for_git_repository() {
local attempt=0
local unlimited=false
# Prüfe ob unbegrenzte Retries gewünscht sind
if [ $MAX_RETRY_ATTEMPTS -lt 0 ]; then
unlimited=true
echo "Checking Git repository availability (unlimited retries)..."
else
echo "Checking Git repository availability (max $MAX_RETRY_ATTEMPTS attempts)..."
fi
while true; do
if git ls-remote --heads "$DADI_STAGING_REPOSITORY" &>/dev/null; then
echo "✓ Git repository is reachable!"
return 0
fi
attempt=$((attempt + 1))
if [ "$unlimited" = true ]; then
echo "⏳ Waiting for Git repository... (attempt $attempt)"
else
echo "⏳ Waiting for Git repository... (attempt $attempt/$MAX_RETRY_ATTEMPTS)"
# Abbruch bei Erreichen des Limits
if [ $attempt -ge $MAX_RETRY_ATTEMPTS ]; then
echo "✗ Error: Could not reach Git repository after $MAX_RETRY_ATTEMPTS attempts"
return 1
fi
fi
sleep $RETRY_DELAY
done
}
#
# Warte auf Git-Repository
#
if ! wait_for_git_repository; then
echo "Initialization failed. Service will continue but branches won't be initialized."
echo "You may need to manually trigger initialization or restart the service."
exit 1
fi
#
# Remove all branch folders
#
for dir in "$BRANCHES_FOLDER"/*/; do
dir_name=$(basename "$dir")
echo "Deleting directory: $dir"
rm -rf "$dir"
done
#
# Fetch all branch names
#
branches=()
while read -r ref; do
# Extract branch name from the reference
branch_name=$(echo "$ref" | awk '{print $2}' | sed 's/refs\/heads\///')
# put into array
branches+=("$branch_name")
done < <(git ls-remote --heads "$DADI_STAGING_REPOSITORY")
for branch in "${branches[@]}"; do
safe_branch_name="${branch//\//-}"
echo "cloning $branch into $safe_branch_name"
git clone --depth 1 --branch "$branch" --single-branch "$DADI_STAGING_REPOSITORY" "$BRANCHES_FOLDER/$safe_branch_name"
done