Skip to content

Instantly share code, notes, and snippets.

@psyao
Last active December 30, 2015 15:19
Show Gist options
  • Save psyao/7847968 to your computer and use it in GitHub Desktop.
Save psyao/7847968 to your computer and use it in GitHub Desktop.
#Config file for deployment
documentroot=/Users/steve/Desktop/bash-scripts/test/remote/domain.com
giturl=ssh://user@domain.com/var/www/example.com
remotename=production
localpath=/Users/steve/Desktop/bash-scripts/test/local
user=steve
group=staff
#!/bin/bash
# May need to run this as sudo!
# sudo chmod o+x git-deploy-local.sh
# sudo ./git-deploy-local.sh
function show_usage {
cat <<- _EOF_
Add a remote git repository to the local repository
-h Help - Show this menu
-n Name of the remote repository - i.e. production or live
-p Path to the local git repository - i.e. ~/Sites/projects/domain.com
-u Url of the git repo - i.e. ssh://user@domain.com/var/www/example.com
Usage ./git-deploy-local.sh -p "~/Sites/projects/domain.com" -u "ssh://user@domain.com/var/www/example.com" -n production
_EOF_
exit 1
}
echo ''
echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo ' GIT deployment shell script'
echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo ''
#Sanity Check
if [ $# -ne 6 ]; then
show_usage
fi
#Parse flags
while getopts "n:p:u:" OPTION; do
case $OPTION in
n) remotename="$OPTARG";;
p) localpath="$OPTARG";;
u) giturl="$OPTARG";;
*) show_usage;;
esac
done
cd $localpath
if [ ! -d $localpath/.git ]; then
git init
fi
git remote add $remotename $giturl
echo " "
echo "Done!"
echo " "
echo "For the first push to your server, run the following command:"
echo "git push production master"
echo " "
echo "Then to deploy changes you've made locally, simply run the following command:"
echo "git push"
#!/bin/bash
# May need to run this as sudo!
# sudo chmod o+x git-deploy-remote.sh
# sudo ./git-deploy-remote.sh
function show_usage {
cat <<- _EOF_
Initialize a new git repository
-d Document root - i.e. /var/www/example.com or /var/www/sub.example.com
-g Group to add user to - i.e. staff
-h Help - Show this menu.
-u User to give permission to - i.e. steve
Usage ./git-deploy-remote.sh -d /var/www/example.com -u steve -g staff
_EOF_
exit 1
}
function create_post_receive_hook {
cat <<- _EOF_
#!/bin/bash
git checkout --force master
_EOF_
}
echo ''
echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo ' GIT deployment shell script'
echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echo ''
#Sanity Check
if [ $# -ne 6 ]; then
show_usage
fi
#Parse flags
while getopts "d:g:u:" OPTION; do
case $OPTION in
d) documentroot="$OPTARG";;
g) group="$OPTARG";;
u) uuser="$OPTARG";;
*) show_usage;;
esac
done
#Folder creation
if [ ! -d $documentroot ]; then
mkdir -p $documentroot
chown $uuser:$group $documentroot
echo " - directory created..."
fi
cd $documentroot
#Git initialisation and configuration
if [ -d $documentroot/.git ]; then
echo 'ERROR: Git folder already exists. Aborting'
exit 1
fi
git init --quiet
echo " - git repository initialized..."
git config core.worktree $documentroot
git config receive.denycurrentbranch ignore
echo " - git repository configured..."
create_post_receive_hook > $documentroot/.git/hooks/post-receive
echo " - post-receive hook added..."
echo " "
echo "Done!"
echo " "
#!/bin/bash
source deploy.cfg
function git-deploy-remote {
if [ ! -f git-deploy-remote.sh ]; then
wget -q https://gist.github.com/psyao/7847968/raw/git-deploy-remote.sh
sudo chmod 755 git-deploy-remote.sh
fi
./git-deploy-remote.sh -d "$documentroot" -u $user -g $group
boot
}
function git-deploy-local {
if [ ! -f git-deploy-local.sh ]; then
wget -q https://gist.github.com/psyao/7847968/raw/git-deploy-local.sh
sudo chmod 755 git-deploy-local.sh
fi
./git-deploy-local.sh -p "$localpath" -u "$giturl" -n $remotename
boot
}
#TODO: delete files
function boot {
echo "What do you want to do ?"
echo " a) Install git deployment on remote server"
echo " b) Install git deployment on local computer"
echo " q) Quit"
read -e action
case $action in
a) git-deploy-remote;;
b) git-deploy-local;;
q) exit 1;;
*) boot;;
esac
}
boot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment