Synchronous continuous integration with Rails/Rake
Versão em portugues aqui
I think that everyone agrees that continuous integrationis a need for any and every software project, but the asynchronous integration largely used has some problems, like:
- The code is commited even if the developer forgot to run the unit tests
- The tests are executed after a while on the integration server, only after that the team will know if the tests passed or not
- The developer is probably working on something else when he/she get the message telling that a problem ocurred with the build, and needs to stop what he/she is doing now to correct the error, or let the error on the repository for a while probably breaking the updates for other developers
I’m not telling that you should no have a build server, you will have a build server to enable every involved on the project to get the latest version of the application, and to generate reports about the source code, these reports usually take a long time to generate and you there is no need for the developer to wait this generation to start the next task, but I do not think that the integration of the developers work are the responsibility of this server.
In other words, you do no need an integration server, just a build server if you are using synchronous integration!
The ideal is that during the commit process, the developer update his source code to the latest version, then run all tests and only if all tests passes he can commit the code to the repository.
Of course you can improve this process adding some restrictions, for example a minimum test coverage, but in my case, I wrote this Rake taks for a project where I’m working alone, and I’m using GIT for version control (I’ll write a post about GIT this week if I have the time), I’m using synchronous integration in some Java projects too, in other opportunity I’ll write about this process in java projects.
I’m adopting this synchronous integration practice for all new projects in my company and in every client that likes the idea.
Back to this post subject, it is really easy to implement synchronous integration in a Rails project, you just need to follow this two steps:
1 - create a ‘git.rake’ file in the directory lib/tasks with the following content:
namespace :git do desc "Update every thing before the tests" task :update do puts "Lets update it all, but we are using GIT so we already have the latest source for this repo" end desc "Run all tests, if all are OK, then commit every thing to the git local repository" task :commit => [:update, :test] do puts "No test failures, now we can commit it all" exec 'git commit -a' end end
As you can see, this Rake task is very simple, and I’m sure it will improve a lot your projects quality.
2 - at the and of each task, instead of running “git commit -a”, run the command: “rake git:update”
That is it! using that you are now using synchronous integration
Of course this Rake task can be improved, you can configure it to pull from a central repository, or update your source code if you are using subversion for example, but I think this one is already a good start point.
It all tests are OK, GIT will open VIM for the developer to write the commit message, and after that, the code will be committed with every thing working, and good bye integration problems!
And you, what do you think about synchronous versus asynchronous continuous integration? what approach do you think is best? why?
If you enjoyed this post, make sure you subscribe to my RSS feed!




