Browse Source

webhook deploy install

bach 3 years ago
parent
commit
0af76cdd60
3 changed files with 112 additions and 0 deletions
  1. 7 0
      assets/webhook-deploy.sh
  2. 10 0
      assets/webhook.service
  3. 95 0
      bin/webhook.sh

+ 7 - 0
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

+ 10 - 0
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

+ 95 - 0
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"