Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Git + Lazyness = happy Rails developer

Posted on 2018-01-11 by Rodrigo Urubatan

I had some problems that you probably have too, if you are a ruby developer that works in a team…

Check if you have at least one of these problems:

  • Someone commits something that does not passes the test suit
  • Someone have deployed a version of the rails application without precompiling assets
  • Similar to the above but forgot to bundle install or npm install

The list can go on and on…

This short post will just give some tips of cool uses for the .git/hooks scripts to help solve some of these problems…

I have a small web application running, and the deploy of that application is just a git pull in the server (ok, blame on me, I’m not using docker for all my apps…)

And to prevent some of the above problems in this application, I created a .git/hooks/post-merge file with the bellow code:

bundle install
bundle exec rake db:migrate
bundle exec rake assets:precompile
touch tmp/restart.txt

just do not forget to “chmod u+x .git/hooks/post-merge”

With this small script, every time you run “git pull” the hook will fire and do all the dirty work for you.

The problem is that sometimes you are just updating a controller and does not need to run all that, and that is fine.

Of course you can make a more complex script to run only the commands you need, but this is good enough for simple scenarios, and at least in my case, I do no do that many deploys a day (most days there aren’t any deploys)

Another problem I had was some developers in the team, not running the tests before pushing changes to the central repository, and for this, a pre-push hook was just perfect, but I wanted to harden the things a little, and used a pre-commit hook, so to commit any small change, the developer should run the app tests.

To do that I created a .git/hooks/pre-commit (again, do not forget to make it executable) with this code:

#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only)
if [[ "$STAGED_FILES" = "" ]]; then
    exit 0
fi
grep migrations $STAGED_FILES
if [[ $? == 0 ]]; then
  bundle exec rake db:migrate
fi
TESTS=""
CUCUMBER=0
grep models $STAGED_FILES
if [[ $? == 0 ]]; then
  TESTS="tests/models"
  CUCUMBER=1
fi
grep controllers $STAGED_FILES
if [[ $? == 0 ]]; then
  TESTS="$TESTS tests/controllers"
  CUCUMBER=1
fi
grep features $STAGED_FILES
if [[ $? == 0 ]]; then
  CUCUMBER=1
fi
if [[ "$TESTS" != "" ]]; then
  bundle exec rake test $TESTS
  if [[ $? == 1 ]]; then
    exit 1
  fi
fi
if [[ $CUCUMBER == 1 ]]; then
 bundle exec cucumber
 if [[ $? == 1 ]]; then
   exit 1
  fi
fi
exit 0;

we have some more verifications in the real file, but this is the idea, if you changed a file, we’ll run the tests before allowing you to commit.

We have some more ideas about how to make git help us, and some of them is building a “heroku like” experience, but we do not really need it but the “trick” that makes it possible, and makes the github web hooks possible too, is the “post-receive” hook.

Since we use github, we have not implemented a post-receive patch, but we have a webhook calling a “cgi script” that was written in ruby (just for fun) that fires a deploy, the script is stupidly simple, only the following:

#!/bin/env ruby
 Dir.chdir('applicationdir')
`git pull`
puts "<html></html>"

with this script, protected by authentication of course, and the hooks we mentioned before, I have github firing a deploy in my development/test environment every time a pull-request is merged to the master branch.

Of course we do not do anything that simple and unsecure to production, but this helps a lot our test environment 😀

 

I hope these git/rails tips help you to improve your project, probably not with the exactly same scripts, but the ideas can be adjusted to your environment.

If you need more ideas or have questions about anything that I wrote here, please leave a comment.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme