If you ever used (Heroku)[https://www.heroku.com/] services, you probably liked their release process. It is built around git. Every application you host there has its own repository. To deploy an application you simply push your changes to this repository and they do the magic. Have you ever wondered, how they do this magic? :) I did. So when I got my VPS in Digital Ocean to host my applications, I wanted to have a similar deployment experience like on Heroku. It turned out to be very simple. You just have to write a deployment script in bash and set it up as a post-receive git hook. In the script, you can compile your app, run gulp or grunt tasks, whatever you need. Then you copy files to the destination directory et voilà - your app has been deployed!
First of all, you need to setup git on your server - just follow this tutorial to do that. You can still use Github, or any other git server to host your repository. You just need to set up an additional remote using the git remote add command:
When you’ve done that, you’re ready to go!
For a simple static website you could have a script with just one command:
It will get files from your repository and copy them to /var/www/mywebsite.com/
You can also have a different setup for branches:
Here you can see the configuration for the master and dev branches. Depending on which branch has been pushed, the script deploys data to a different directory. It also runs npm and grunt commands after the files were copied.
The last example is a script for a .NET application, which is being compiled with mono:
Yes, it is a Linux server and I am running .NET apps on it! It’s going to be even easier and better supported with the new .NET Core and ASP.NET Core, which are cross-platform, but it’s also possible with mono.
If you’re a developer, you should script your whole life. Especially deployments. Doing this manually is a hassle and a waste of time. I hope I have shown you a better way.
PS. I think having a private VPS where you can host your applications is very useful. I will write a separate post about why I recommend it and what else you can use it for. If you don’t have a VPS yet, go on and get one. There are many providers with different offers. I did my research, picked Digital Ocean and am happy with the decision. It’s only $5 per month and you can do lots of cool stuff with it. If you register from this link, you will get a $10 credit for free!