Setting up Git with Automatic Deployment on a VPS
Introduction to Git
In this tutorial we will teach you how to operate Git as you please. There are a ton of ways you can use Git to deploy your application but this tutorial’s main focus is will be on the one that is most straight forward. We assume you already know how to make and operate a repository on your local machine.
While using Git, the workflow is normally towards the version control only. You have a local repository in which you work as well as a remote repository where you can have everything in sync and can work with a team and various machines. You could also use Git to move your application to production.
The workspace we will be using:
Your live server directory: /var/www/vpsdomain.com
Your VPS repository: /var/repo/site.git
Creating Our Repository
‘–bare’ will mean that the folder has no source files, just the version control.
cd /var mkdir repo && cd repo mkdir site.git && cd site.git git init --bare
–bare will mean that the folder has no source files, just the version control.
Hooks and Git Repository
Usually a Git repository will have a folder named ‘hooks’. In this folder you will have some sample files for possible actions that you can hook and use custom actions made by you.
Git documentation http://git-scm.com/book/en/Customizing-Git-Git-Hooks:
Define three possible server hooks: one of them is called ‘pre-receive’, and the last 2 are ‘post-receive’ and ‘update’.
‘Pre-receive’ will be executed whenever the server receives a ‘push’. ‘update’ is nearly the same except that it only executes once for each branch. Furthermore, ‘post-receive’ is only used when a ‘push’ is completely finished and it is the one we are interested in.
If you type in your repository the following.
You should have a couple files and folders, this includes the ‘hooks’ folder. Go to the ‘hooks’ folder.
Now, create the file ‘post-receive’ by executing the below.
cat > post-receive
Once you ha executed the command, you should have a blank line which indicates that everything you type will be saved to this file. Now write the below.
#!/bin/sh git --work-tree=/var/www/vpsdomain.
com --git-dir=/var/repo/site.git checkout -f
After you have finished typing, press ‘control-d’ in order to save. Now to execute the file, you will have to set the proper permissions with the following.
chmod +x post-receive
You will be able to see on the documentation that ‘git-dir’ is the path to the repository. Using ‘work-tree’, you can define another path to where your files are going to be transferred.
The ‘post-receive’ file will be looked into every time a push is finished; it says your files will need to be in ‘/var/www/vpsdomain.com’
Now create your local repository. You will need to change the path and name to whatever you would prefer. If you are on a VPS, just write the below.
Now make your repo.
cd /my/workspace mkdir project && cd project git init
Now you will need to configure the remote path of your repository. Tell Git to add a remote called ‘live’.
git remote add live ssh://[email protected]/var/
Now you can add the repository link instead of the ‘live’ folder.
Assuming that you have got some great work ready in the folder; you can do the usual steps to add the files and afterwards commit using a message.
git add . git commit -m "My project is ready"
Make sure to remember, the dot after ‘git add’ means you will be adding all files to sage. After ‘git commit’ you will have ‘-m’ which means you would have to write a message.
To finish, simply ‘push’ everything into the server. You can use the ‘live’ alias that you needed once you have set up the remote.
git push live master Counting objects: 7, done.Delta compression using up to 4 threads.Compressing objects: 100% (7/7), done.Writing objects: 100% (7/7), 10.56 KiB, done.Total 7 (delta 0), reused 0 (delta 0)To ssh://[email protected]/var/
repo/site.git* [new branch] master -> master
Now we tell Git to push to the live remote on the ‘master’ branch.
If you are not interested in deploying everything with one step and maybe would like to test it first and have a beta directory.
One way to do this is to simply create another repository. So login again to the VPS and create the directory.
cd /var/www/ mkdir beta
To create the repository.
cd /var/repo mkdir beta.git && cd beta.git git init --bare
Now you will again need to create the ‘post-receive’ file since you want to see the project in your beta directory.
cd hooks cat > post-receive
Write the content of the file.
#!/bin/sh git --work-tree=/var/www/beta --git-dir=/var/repo/beta.git checkout -f
After you have finished writing, use ‘control-d’ to save. Then, to execute the file, you must set the proper permissions with the below.
chmod +x post-receive
Then, go back to the local repository.
exit cd /my/workspace/project
And now you may set another remote pointing to the beta repository.
git remote add beta ssh://[email protected]/var/
Using this, you will have a two step process. Before anything we push to beta and check; if everything looks fine we can push to live.
git add . git commit -m "New version" git push beta master
git push live master
Going Live From the Server
If you have a team working in the same project and you want to enable others to also decide the time to go live; you can link the beta and live repository on the server. Log in to your VPS and type the below.
cd /var/repo/beta.git git remote add live ../site.git
You should now able to push from beta to live on the server.
cd /var/repo/beta.git git push live master
Congratulations, your VPS is now set to automatically deploy with Git.