Git deploy – how to implement git deploy in a project

Lately I’m becoming a lazy developer, and this reflects in my work.

I tend to choose the easiest solution that will work for any project, and sometimes a simple project, is still in it’s early stages and it does not pay to configure a capistrano deploy or anything fancy, so I’m just using git to do the deployment, and it almost feels like heroku for me.

And the setup is pretty simple, might help you your projects too.

To setup that, we’ll use git hooks, and a bare git repository.

I’ll use a simplified version of my scripts in this post, to create a simple step by step.

In the server, create a directory for the bare git repo and initialize theĀ  repository:

mkdir myproj.git
cd myproj.git
git init --bare
cd ..
git clone myproj.git

After that, we’ll setup the post-receive hook in the bare repository, to do that, create a file called post-receive in the myproj.git/hooks directory with this content:

/bin/bash --login <<_EOF_
export GIT_DIR=/home/urubatan/myproj/.git
rvm use 2.4.0
cd /home/urubatan/myproj
git pull
npm install
bundle install
RAILS_ENV=production bundle exec rake db:migrate
bundle exec rake assets:precompile
touch tmp/restart.txt

Since we want this hook to execute every time we push something to that repository, do not forget o make the script executable:

chmod 755 myproj.git/hooks/post-receive

now back to your machine, just create your rails project as usual:

rails new myproj_client

add the bare repository as “deploy” remote

git remote add deploy user@server:~/myproj.git

and when you are done, push your changes to the server:

git add .
git commit -m "sample commit for the blog"
git push deploy master

Of course, you still need to configure the server, using for example, nginx + passenger, or puma, or any other thing, but that is subject to another post.

Please add any question to the comments of this post, I’ll answer everything as soon as possible.