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

100 lines
2.8 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/sh
#
# Create hosts.json with by replacing environment variables in template file
#
# Define the template file path
template_file="/etc/webhook/hooks.json.tmpl"
# Define the output file path
output_file="/etc/webhook/hooks.json"
# Check if the template file exists
if [ ! -f "$template_file" ]; then
echo "Template file $template_file not found."
# exit 1
else
envsubst <"$template_file" >"$output_file"
echo "Generated $output_file"
fi
#
# Copy ssh keys and config
#
path_ssh="/root/.ssh"
path_dadi_staging_key="/data/ssh/dadi-staging-webhook.deploy.ed25519"
path_dadi_staging_pub="/data/ssh/dadi-staging-webhook.deploy.ed25519.pub"
path_ssh_config="/data/ssh/config"
# Create .ssh folder if not present
if [ ! -d "$path_ssh" ]; then
# Create the folder
mkdir -p "$path_ssh"
echo "Folder created: $path_ssh"
fi
# Copy dadi staging key
if [ ! -f "$path_dadi_staging_key" ]; then
echo "Error: repository key $path_dadi_staging_key does not exist"
else
cp "$path_dadi_staging_key" "${path_ssh}/"
fi
# Copy dadi staging public key
if [ ! -f "$path_dadi_staging_pub" ]; then
echo "Error: repository public key $path_dadi_staging_pub does not exist"
else
cp "$path_dadi_staging_pub" "${path_ssh}/"
fi
# Copy ssh config
if [ ! -f "$path_ssh_config" ]; then
echo "Error: ssh config $path_ssh_config does not exist"
else
cp "$path_ssh_config" "${path_ssh}/"
fi
# Create or append known_hosts
echo "Validating: $GIT_SERVER_DADI_STAGING"
# Wir wollen, dass der Befehl mindestens einmal erfolgreich ist.
# Wenn er scheitert, warten wir 10s und probieren es erneut.
while true; do
# Versuche, den HostKey zu holen und an known_hosts anzuhängen.
# Der Rückgabewert von ssh-keyscan ist 0, wenn mindestens ein
# Schlüssel empfangen wurde.
if ssh-keyscan -H "$GIT_SERVER_DADI_STAGING" >>"${path_ssh}/known_hosts"; then
echo "✅ Host key for '$GIT_SERVER_DADI_STAGING' added to known_hosts"
break # Erfolg → Schleife verlassen
else
echo "⚠️ ssh-keyscan failed retry in 10seconds"
sleep 10
fi
done
echo "git and ssh configured."
echo "|--- dadi staging webhook public key (deploy key): --------------------|"
cat "${path_ssh}"/dadi-staging-webhook.deploy.ed25519.pub
echo "|---- Config ------------------------------------------------------|"
cat "${path_ssh}"/config
echo "|---- known_hosts -------------------------------------------------|"
cat "${path_ssh}"/known_hosts
echo "|------------------------------------------------------------------|"
# Initialize branches
echo "Initialize branches of dadi staging"
./dadi-staging-init.sh
# Generate index.html
./generate_index_html.sh
#
# Start webhooks
#
echo "Starting webhooks service"
exec /usr/local/bin/webhook -verbose -hooks=/etc/webhook/hooks.json -hotreload