From 9178dcf1def6c1b9652bca5af9e2b6ee638ab7e5 Mon Sep 17 00:00:00 2001 From: Andre Challier Date: Wed, 27 Mar 2024 16:30:35 +0100 Subject: [PATCH] added init scripts for assets and programm --- webhooks-git/Dockerfile | 4 ++++ webhooks-git/assets-hook.sh | 1 + webhooks-git/assets-init.sh | 38 +++++++++++++++++++++++++++++++ webhooks-git/entrypoint.sh | 20 ++++++++++++++++ webhooks-git/programm-hook.sh | 0 webhooks-git/programm-init.sh | 43 +++++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 webhooks-git/assets-hook.sh create mode 100644 webhooks-git/assets-init.sh create mode 100644 webhooks-git/programm-hook.sh create mode 100644 webhooks-git/programm-init.sh diff --git a/webhooks-git/Dockerfile b/webhooks-git/Dockerfile index 3cf4afc..7e0b7e8 100644 --- a/webhooks-git/Dockerfile +++ b/webhooks-git/Dockerfile @@ -5,6 +5,10 @@ RUN apk --no-cache add git openssh-client bash nano gettext COPY --from=build /usr/local/bin/webhook /usr/local/bin/webhook COPY hooks.json.tmpl /etc/webhook/hooks.json.tmpl COPY entrypoint.sh /etc/webhook/entrypoint.sh +COPY assets-hook.sh /etc/webhook/assets-hook.sh +COPY assets-init.sh /etc/webhook/assets-init.sh +COPY programm-hook.sh /etc/webhook/programm-hook.sh +COPY programm-init.sh /etc/webhook/programm-init.sh RUN chmod +x /etc/webhook/entrypoint.sh WORKDIR /etc/webhook EXPOSE 9000 diff --git a/webhooks-git/assets-hook.sh b/webhooks-git/assets-hook.sh new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/webhooks-git/assets-hook.sh @@ -0,0 +1 @@ + diff --git a/webhooks-git/assets-init.sh b/webhooks-git/assets-init.sh new file mode 100644 index 0000000..52b77f2 --- /dev/null +++ b/webhooks-git/assets-init.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# +# Initialize assets folder from repository +# +# - delete existing assets folder +# - fetch assets/main from repository +# + +BRANCHES_FOLDER="/html/branches" +ASSETS_FOLDER="/html/branches/assets" +ASSETS_REPOSITORY="git_assets:AG-IT/assets.git" +ASSETS_BRANCH="main" + +# +# Remove assets folder if exists +# + +if [ -d "$ASSETS_FOLDER" ]; +then + echo "Removing existing assets folder: $ASSETS_FOLDER" + rm -R "$ASSETS_FOLDER" +fi + +# +# Create branches folder if not exist +# + +if [ ! -d "$BRANCHES_FOLDER" ]; +then + echo "Create branches folder: $BRANCES_FOLDER" + mkdir -p "$BRANCHES_FOLDER" +fi + +# +# Clone assets folder from repository +# +git clone --depth 1 --branch "$ASSETS_BRANCH" --single-branch "$ASSETS_REPOSITORY" "$ASSETS_FOLDER" \ No newline at end of file diff --git a/webhooks-git/entrypoint.sh b/webhooks-git/entrypoint.sh index bb19ad0..a5c87dd 100644 --- a/webhooks-git/entrypoint.sh +++ b/webhooks-git/entrypoint.sh @@ -81,6 +81,26 @@ fi # Create or append known_hosts ssh-keyscan -H "${GIT_SERVER}" >> "${path_ssh}/known_hosts" +echo "git and ssh configured." +echo "Programm webhook public key (deploy key):" +echo "|------------------------------------------------------------------|" +cat "${path_ssh}/programm-webhook.deploy.25519.pub" +echo "|------------------------------------------------------------------|" +echo "Assets webhook public key (deploy key):" +echo "|------------------------------------------------------------------|" +cat "${path_ssh}/assets-webhook.deploy.25519.pub" +echo "|------------------------------------------------------------------|" + +# Initialize assets + +echo "Initialize assets directory" +./assets-init.sh + +# Initialize branches + +echo "Initialize branches of programm" +./programm-init.sh + # # Start webhooks # diff --git a/webhooks-git/programm-hook.sh b/webhooks-git/programm-hook.sh new file mode 100644 index 0000000..e69de29 diff --git a/webhooks-git/programm-init.sh b/webhooks-git/programm-init.sh new file mode 100644 index 0000000..4a9424d --- /dev/null +++ b/webhooks-git/programm-init.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# +# Initialize assets folder from repository +# +# - delete existing assets folder +# - fetch assets/main from repository +# + +HTML_HOME="/html" +BRANCHES_FOLDER="/html/branches" +PROGRAMM_REPOSITORY="git_programm:AG-Programm/parteiprogramm.git" + +# +# Remove all branch folders but assets +# + +for dir in "$BRANCHES_FOLDER"/*/; +do + dir_name=$(basename "$dir") + + if [ "$dir_name" != "assets" ]; + then + echo "Deleting directory: $dir" + rm -rf "$dir" + fi +done + +# +# Fetch all branch names +# +git ls-remote --heads "$PROGRAMM_REPOSITORY" | + # Extract branch names + awk '{print $2}' | + # Remove "refs/heads/" prefix to get only branch names + sed 's#refs/heads/##' | + # Store branch names in an array + mapfile -t branches + +for branch in "${branches[@]}"; +do + git clone --depth 1 --branch "$branch" --single-branch "$PROGRAMM_REPOSITORY" "$BRANCHES_FOLDER"/"$branch" +done \ No newline at end of file