Instea of uploading to a middle man like github then doing a pull on your production or staging server you can use this trick to push directly to the production or staging server over SSH
These instructions will show how to use a git hook( in this case post-receive ) to do whatever is needed on the server(receiving) side.
This allows for high security(no middle men) and high flexibility as you can use Bash scripts to do whatever is required.
- Know how to use GIT, Terminal etc.
- Have a local working-working copy ready
- Have SSH access to your server using private/public key
- Create a folder to deploy to on production server (i.e. your httpds folder)
- Use git to add a bare repository on the productions(remote) server
- Add the post-receive hook script to the bare repository (and make it executable)
- Add the remote-repository resided on the production server to your local repository
- Push to the production server, relax.
This can be any branch, not only master.
ssh into your remote server:
ssh -p 2222 user@server.com
mkdir ~/deployment-folder
Create a bare repository, this will essentially be the .git folder contents of a normal repo.(doesnt need to end in .git)
git init --bare ~/production.git
vim ~/production.git/hooks/post-receive
#!/bin/bash
TARGET="/home/webuser/deployment-folder"
GIT_DIR="/home/webuser/production.git"
BRANCH="master"
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [ "$ref" = "refs/heads/$BRANCH" ];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to ${TARGET}..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
chmod +x post-receive
Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.
cd ~/path/to/working-copy/
git remote add production user@yourserver.com:production.git
if you need to use an alternative port you can do...
git remote add production ssh://user@yourserver.com:2222/home/webuser/production.git
Make sure "production.git" coresponds to the name you gave in step 3.
Now you can push the master branch to the production server:
git push production master
#!/bin/bash
TARGET_PRODUCTION="/home/myUser/www/myWebsite-production"
TARGET_DEVELOPMENT="/home/myUser/www/myWebsite-development"
GIT_DIR="/home/myUser/www/myWebsite.git"
BRANCH_PRODUCTION="master"
BRANCH_DEVELOPMENT="development"
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [ "$ref" = "refs/heads/$BRANCH_PRODUCTION" ];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TARGET_PRODUCTION --git-dir=$GIT_DIR checkout -f $BRANCH_PRODUCTION
elif [ "$ref" = "refs/heads/$BRANCH_DEVELOPMENT" ];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to development..."
git --work-tree=$TARGET_DEVELOPMENT --git-dir=$GIT_DIR checkout -f $BRANCH_DEVELOPMENT
else
echo "refs/heads/$BRANCH_DEVELOPMENT"
echo "Ref $ref received. Doing nothing: $ref not recognized."
fi
done