Skip to content

Instantly share code, notes, and snippets.

@mitchallen
Created June 15, 2012 19:02
Show Gist options
  • Save mitchallen/2938195 to your computer and use it in GitHub Desktop.
Save mitchallen/2938195 to your computer and use it in GitHub Desktop.
Example git post-receive hook - assumes you have a node.js app setup as a service that can be stopped and started. See the comments for usage.
#!/bin/sh
echo "Stopping service"
sudo stop myapp
echo "Deploying to test ..."
_TREE=/export/home/MY-USER/test
GIT_WORK_TREE=$_TREE git checkout -f
cd $_TREE
pwd
echo "Installing dependencies"
npm install
ls -la $_TREE
echo "Starting service"
sudo start myapp
echo "Deployment finished."
@mitchallen
Copy link
Author

These instructions are for RHEL6

On your server edit /etc/sudoers and make sure this line is commented out (precede with a '#') - else you'll get an error from the sudo commands in the gist:

# Defaults    requiretty

To setup the repo on your server do the following:

mkdir myrepo.git
cd myrepo.git
git --bare init

Use your favorite Linux text editor to create a file with the contents of this gist (change the value of $_TREE to suit your preferred deployment location):

vim ~/myrepo.git/hooks/post-receive
chmod +x ~/myrepo.git/hooks/post-receive

Setup the folder to deploy to after a git push to the server (assumes full path maps to $_TREE in gist):

cd ~
mkdir test

On your original source client git project add your server / repo as a remote (substituting USER and SERVER):

git remote add origin USER@SERVER:myrepo.git
git config branch.master.remote origin && git config branch.master.merge refs/heads/master
git push origin master

Dependencies:

If you don't plan to include a package.json file in the root of your project with dependencies or plan to push your local node_modules folder to the server, remove these two lines:

echo "Installing dependencies"
npm install

You may have to push your node_modules folder to the server if a firewall prevents npm from being able to contact the remote registry from the server.

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment