From 0af76cdd60c26b3efaddfe89539395a746b46ea6 Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 18 May 2021 13:58:50 +0200 Subject: [PATCH] webhook deploy install --- assets/webhook-deploy.sh | 7 +++ assets/webhook.service | 10 +++++ bin/webhook.sh | 95 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 assets/webhook-deploy.sh create mode 100644 assets/webhook.service create mode 100644 bin/webhook.sh diff --git a/assets/webhook-deploy.sh b/assets/webhook-deploy.sh new file mode 100644 index 0000000..1d79802 --- /dev/null +++ b/assets/webhook-deploy.sh @@ -0,0 +1,7 @@ +#!/bin/bach + +# update bare repos +git --git-dir=git-repositories/DOMAIN.git fetch origin prod:prod +# deploy prod +cd www/DOMAIN/app +./deploy.sh diff --git a/assets/webhook.service b/assets/webhook.service new file mode 100644 index 0000000..5a4db03 --- /dev/null +++ b/assets/webhook.service @@ -0,0 +1,10 @@ +[Unit] +Description=Small server for creating HTTP endpoints (hooks) +Documentation=https://github.com/adnanh/webhook/ + +[Service] +ExecStart=webhook -hooks /etc/webhooks.conf -verbose -nopanic -hotreload +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/bin/webhook.sh b/bin/webhook.sh new file mode 100644 index 0000000..b30a804 --- /dev/null +++ b/bin/webhook.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +# bachir soussi chiadmi + +# get the current position +_cwd="$(pwd)" + +echo -e '\033[35m + __ __ _ _ _ _ + \ \ / /__| |__| || |___ ___| |__ + \ \/\/ / -_) `_ \ __ / _ \/ _ \ / / + \_/\_/\___|_.__/_||_\___/\___/_\_\ +\033[0m' + +# check for assets folder +_assets="$_cwd/assets" +if [ ! -d "$_assets" ]; then + _assets="$_cwd/../assets" + if [ ! -d "$_assets" ]; then + echo "!! can't find assets directory !!" + exit + fi +fi + +while [ "$user" = "" ] +do + read -p "enter an existing user name ? " user + if [ "$user" != "" ]; then + # check if user already exists + if id "$user" >/dev/null 2>&1; then + read -p "is user name $user correcte [y|n] " validated + if [ "$validated" = "y" ]; then + break + else + user="" + fi + else + echo "user $user doesn't exists, you must provide an existing user" + user="" + fi + fi +done + +while [ "$_domain" = "" ] +do +read -p "enter a domain name ? " _domain +if [ "$_domain" != "" ]; then + read -p "is domain $_domain correcte [y|n] " validated + if [ "$validated" = "y" ]; then + break + else + _domain="" + fi +fi +done + +# TODO check for /home/"$user"/www/"$_domain" +if [ ! -d /home/"$user"/www/"$_domain" ]; then + echo "/home/$user/www/$_domain does not exists !" + exit +fi + +# TODO check for /home/"$user"/git-repositories/"$_domain.git" +if [ ! -d /home/"$user"/git-repositories/"$_domain.git" ]; then + echo "/home/$user/git-repositories/$_domain.git does not exists !" + exit +fi + +apt-get install webhook + +# hook deploy script +cp "$_assets"/webhook-deploy.sh /"$user"/webhook-deploy-"$_domain".sh +sed -i -r "s/DOMAIN/$_domain/g" /"$user"/webhook-deploy-"$_domain".sh +chmod +x /"$user"/webhook-deploy-"$_domain".sh + +# remove git bare repos hook +mv /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive /home/"$user"/git-repositories/"$_domain".git/hooks/post-receive.back + +# webhook conf +touch /etc/webhook.conf +echo " +- id: deploy-app-$_domain + execute-command: /$user/webhook-deploy-$_domain.sh + command-working-directory: /home/$user/ +" >> /etc/webhook.conf + +# webhook service +cp "$_assets"/webhook.service /etc/systemd/system/webhook.service +systemctl enable webhook +systemctl start webhook +systemctl reload webhook + +ufw allow 9000 + +echo "http://$_domain:9000/hooks/deploy-app-$_domain"