Skip to content

Instantly share code, notes, and snippets.

@thomseddon
Last active December 29, 2015 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomseddon/7655422 to your computer and use it in GitHub Desktop.
Save thomseddon/7655422 to your computer and use it in GitHub Desktop.
Create a git deployment folder on a remote machine
#!/bin/bash
# Read vars
if [ "$1" == "" ]; then
echo "Usage: ./git_deploy <folder> <user?> <post-receive?>"
exit 1
fi
folder=$1
if [ -d "$folder" ]; then
echo "$folder already exists"
exit 1
fi
user=$2
if [ "$2" == "" ]; then
user=$USER
fi
post_receive=$3
# Create folder
mkdir $folder
cd $folder
dir=$(pwd)
# Config git
git init
git config core.worktree $dir
git config receive.denycurrentbranch ignore
cat > .git/hooks/post-receive <<EOL
#!/bin/sh
git checkout -f
$post_receive
EOL
chmod +x .git/hooks/post-receive
# Fix ownership
cd ../
chown $user:$user -R $folder
echo
echo "Run: git remote add prod ssh://$user@$HOSTNAME$dir"
echo "Run: git push prod master"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment