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.