Skip to content

Instantly share code, notes, and snippets.

@CroneKorkN
Last active August 29, 2015 14:25
Show Gist options
  • Save CroneKorkN/9ec8cad9cabc98273d66 to your computer and use it in GitHub Desktop.
Save CroneKorkN/9ec8cad9cabc98273d66 to your computer and use it in GitHub Desktop.
autodeploy a git repo on commit
#!/bin/bash
# ==============
# git autodeploy
# ==============
#
# makes a bare git repo deploying a specific branch on every commit.
# creates repo if cwd is not the root of a bare repo. Requirements:
# - `git`
# - `ssh-copy-id`
# - `ssh`
# - `sshfs`
# - ssh key pair for git-user or `ssh-keygen` to create it
#
# by ckn
# dev vars
#PROJECT_NAME="test1"
#LIVE_USER="test"
#LIVE_PATH="/var/home/test"
clear
APP="#########################\n# git-autodeploy by ckn #\n#########################\n"
clear; echo -e $APP
# --------------------------------
# programm mode: update or create?
# --------------------------------
# cwd is a git repo?
if [ -d hooks ]; then
CREATE_REPO=false
APP="$APP# mode B: install autodeploy-hook into this repo\n";
else
CREATE_REPO=true
APP="$APP# mode A: create repo and install autodeploy-hook\n";
fi;
clear; echo -e $APP
# ----------
# user input
# ----------
# repo-name if neccessary
if [ true = $CREATE_REPO ]; then
echo "project name?"
while [ "" = "$PROJECT_NAME" ]; do
read -e PROJECT_NAME
done
APP="$APP# project name: $PROJECT_NAME\n"; clear; echo -e $APP
fi
# ssh user
echo "live ssh user?"
while [ "" = "$LIVE_USER" ]; do
read -e -i "test" LIVE_USER
done
APP="$APP# live user: $LIVE_USER\n"; clear; echo -e $APP
# ssh host
echo "live ssh host of $LIVE_USER? (default: localhost)"
read -e -i "localhost" LIVE_HOST
APP="$APP# live host: $LIVE_HOST\n"; clear; echo -e $APP
# live files path on remote machine
echo "path to live files on remote machine?"
while [ "" = "$LIVE_PATH" ]; do
read -e -i "/home/test" LIVE_PATH
done
APP="$APP# live path: $LIVE_PATH\n"; clear; echo -e $APP
# mas
echo "branch to deploy on update? (default: master)"
read -e -i "master" LIVE_BRANCH
APP="$APP# branch: $LIVE_BRANCH\n"; clear; echo -e $APP
# ----------
# yield vars
# ----------
# mode dependant vars
if [ false = $CREATE_REPO ]; then
GIT_PATH=$(realpath ..)
PROJECT_NAME=${PWD##*/}
PROJECT_NAME=${PROJECT_NAME::-4}
else
GIT_PATH=$(pwd)
fi;
# common vars
REPO_PATH="$GIT_PATH/$PROJECT_NAME.git"
LIVE_MOUNTPATH="$REPO_PATH/live"
HOOK_LOCATION="$REPO_PATH/hooks/post-receive"
HOOK_TEMP_LOCATION="$REPO_PATH/hooks/post-receive.tmp"
SCRIPT_LOCATION="$REPO_PATH/hooks/autodeploy"
SCRIPT_CONTENT="#!/bin/bash
# loop through riceiving branches
while read oldrev newrev refname
do
# extract this branch name
branch=\$(git rev-parse --symbolic --abbrev-ref \$refname)
# check if branch is master
if [ \"$LIVE_BRANCH\" == \"\$branch\" ]; then
# ensure live folder is unoccupied
fusermount -u \"$LIVE_MOUNTPATH\"
# make live-folder available to write updates
sshfs $LIVE_USER@$LIVE_HOST:$LIVE_PATH $LIVE_MOUNTPATH
# checkout files
git --work-tree=\"$LIVE_MOUNTPATH\" --git-dir=\"$REPO_PATH\" checkout -f $LIVE_BRANCH
# unoccupy live folder
fusermount -u \"$LIVE_MOUNTPATH\"
fi
done"
# log
APP="$APP# git path: $GIT_PATH
\n# project $PROJECT_NAME path: $REPO_PATH
\n# live files mount point: $LIVE_MOUNTPATH
\n# git-hook file: $HOOK_LOCATION\n#########################\n"; clear; echo -e $APP
# -------
# execute
# -------
# create repo if neccessary
if [ true = $CREATE_REPO ]; then
mkdir -p "$REPO_PATH"
git init --bare "$REPO_PATH"
echo "# created repository"
fi
# create folder for mounting live-fs
if ! [ -d "$LIVE_MOUNTPATH" ]; then
mkdir $LIVE_MOUNTPATH
echo "# created folder to mount live-fs"
else
echo "# live-fs mount-folder already exists"
fi
# ensure hook file exists
if ! [ -f "$HOOK_LOCATION" ]; then
echo "#!/bin/sh" > $HOOK_LOCATION
echo "# created hook file"
else
# avoid duplicate hooks
grep -v "autodeploy activator" $HOOK_LOCATION > $HOOK_TEMP_LOCATION
mv $HOOK_TEMP_LOCATION $HOOK_LOCATION
echo "# cleaned hook file"
fi
# install git hook and permit file-execution
echo "$SCRIPT_LOCATION # autodeploy activator" >> "$HOOK_LOCATION"
echo -e "$SCRIPT_CONTENT" > "$SCRIPT_LOCATION"
chmod +x "$HOOK_LOCATION"
chmod +x "$SCRIPT_LOCATION"
echo "# installed hook"
# ssh keys exist?
while ! [[ -f ~/.ssh/id_rsa && -f ~/.ssh/id_rsa.pub ]]; do
ssh-keygen -t rsa -b 4096
done
# authorize to live host
echo $LIVE_USER@$LIVE_HOST
ssh-copy-id $LIVE_USER@$LIVE_HOST
echo "# authorized to live user\n#########################\n# done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment