If you are setting up your own remote server/VPS you will probably like to make it easy to deploy your code with simple git push. In other words you want to setup something that providers such as Heroku or OpenShift let you do to deploy your code. Luckily, it’s very easy!
First we need to set up a normal remote git server. Let’s say we will deploy our code as user
user on server
server to a directory called
~/app having our remote git server at
~/git. This requires you to install git on the remote server first. After that we can create our git repository as follows:
$ ssh user@server 'mkdir ~/git && cd ~/git && git init --bare'
This creates a git repository that we can add as a remote location, e.g.:
$ git remote add production user@server:git
Once added you can push your code from
release-branch to remote
$ git push production release-branch:master
Nice! We have our own remote git server. This can be useful for backups but if we want to run our code we need to pull it from our git repository to
~/app directory. For that we should use one of the git hooks to make the process automatic.
ssh user@server 'mkdir ~/app && touch ~/git/hooks/post-receive && chmod +x git/hooks/post-receive'
git push on our client remote git will now execute
post-receive script that can contains anything you want it to do. The bare minimal would be to checkout the new source code into
~/app directory, but depending on your needs you can now run
bundle command, database migrations and of course restarting your application server.
Here is an example:
server$ cat ~/git/hooks/post-receive #!/bin/sh GIT_WORK_TREE=/home/user/app git checkout -f cd /home/user/app && bundle ...