In this tutorial we create a simple deployment stream with git hooks. We need to first create a folder in the server where various versions will be stored and managed.
SSH into your remote server
ssh root@host # In my case i will be storing all the application # in a folder named “repo” in the server /var cd /var/ mkdir repos && cd $_
now we will create the application directory as we may host multiple applications in a single instance.
mkdir projectname.git && cd $_
It is a general practice to initiate the live repository as bare repo
git init --bare
now we will access its hooks set by navigating into its hooks and creating a post-receive file
cd hooks vi post-receive
post-receive is a trigger that git hooks use to run a bash script once code is updated.
Initiate bash script and specify git —work-tree where we will update latest code and —git-dir where the version are stored in the server.
#!/bin/sh git --work-tree=/var/www/html/projectname --git-dir=/var/repos/projectname checkout -f # checkout is done with -f to avoid conflicts. # you can add any services/routines to be executed after checkout completes.
change the mod of the post-receive for the operating system is execute it without permission issues with
chmod +x post-receive
Make sure the directory in the —work-tree is present or else create it which is be mapped to your virtual host.
now setup in the server is complete. exit to your local machine.
Navigate to the project location if not create one for your local repository.
Next add the remote with an alias config of your choice.
git remote add central ssh://root@host/var/repos/projectname
you can replace “central” above with any term.
Now everything is ready you just have to follow your normal git routine.
git add . git commit -m “setting up auto deploy” git push central master
If you are getting an fatal permission error, you will have to update your ssh public key into remote servers authorized_keys file by pasting the following command.
cat ~/.ssh/id_rsa.pub | ssh root@host “mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys
and try pushing to your channel now.
You can checkout the sample project at here