At work, my lead asks the folks that manages the SVN repos to have a feature that notifies us every time somebody commits on trunk. They were not able to produce that feature nor find a plugin that does, therefore, I’ve created my own solution. This is how Trunk Spy was born.
Monitoring SVN trunk updates is critical in our team as multiple team members and even multiple sub-teams touches it and relies on up-to-date information about the current state of the SVN trunk. What we need is for us to be notified every time somebody commits on trunk. In our current process, it means that a new version as been released last night.
With the tool I’ve just created, the idea is to watch the latest revision number of the trunk and save it into a file. The next round of checking will fetch again the latest revision number. If they are not equal, then the state is saved into a file and notify the team via email that there is a new revision on trunk.
The command line is probably the best tool for monitoring SVN repositories. To get the latest revision from a certain repo/trunk, we just need to run
svn log command. Below is an example.
svn log https://svn.company.com/svn/project1/trunk
Above will print a long revision log into the terminal. To get only 1 (or the top revision) you can put a
--limit parameter. We can actually parse the output to get the revision number, commit message and date but good thing we have another parameter that let svn client to output xml with the
--xml parameter. Below is the example command.
svn log https://svn.company.com/svn/project1/trunk --limit 1 --xml
With this information in mind, we can create a tool that fetches the latest revision from an SVN repository, save them into an XML file, parse them via PHP and let PHP do the rest of the job. I get more information from Fabien on his site on how he monitors Symfony plugin development.
The code is already there up on github. Below are generic installation instructions.
Grab the code – get the source code on github and hit the download button. Or if you want, just clone the repo so you can just pull updates when needed. I may update the tool from time to time when need arises and maybe if someone would add features to it. Put the code into your server.
Setup the config file – we need to have a working
config.php file. Just copy
config.php.sample and save it as
config.php. There, you modify to enter the following information.
- Target SVN url
- SVN credentials
- XML storage paths
- Email address where you send notifications
You may add as many entry as possible. Below is an example with just 2 entries. Make sure the directory where you save the XML files is writable by you, otherwise it won’t work.
<?php return array( 'entries' => array( 'project1' => array( 'username' => 'user', 'password' => 'password', 'svnPath' => 'https://svn.company.com/svn/project1/trunk', 'label' => 'Academy Trunk', 'storagePath' => ROOT_DIR . '/data/trunk_current.xml', 'storagePathTmp' => ROOT_DIR . '/data/trunk_tmp.xml', 'notifyRecipients' => array('root@darkstar'), ), 'project2' => array( 'username' => 'user', 'password' => 'password', 'svnPath' => 'https://svn.company.com/svn/project2/trunk', 'label' => 'Topblip Admin Trunk', 'storagePath' => ROOT_DIR . '/data/trunk2_current.xml', 'storagePathTmp' => ROOT_DIR . '/data/trunk2_tmp.xml', 'notifyRecipients' => array('root@darkstar'), ), ) );
Test – to make sure you have it right, run the script to see if you receive notifications for newly setup Trunk Spy. Just run the command below for example.
Email – by default, Trunk Spy just uses the default mailer program which is typically
sendmail for Linux boxes. If your server is capable of sending email via
sendmail, you should receive email notifications when run test run the script. Unfortunately,
sendmail is the only option for now. If you want SMTP instead, you need to modify the method to have another way of sending emails.
I will probably add that in the future but for now,
sendmail is enough for me.
Once you’ve tested the email notifications capability, you are now ready to setup the scheduler.
cron – you need to setup a scheduler to periodically check each repos for latest updates. I did mine for every 5 minutes. Below is an example. Run
crontab -l to list cron jobs and
crontab -e to edit.
# Other entries # Trunk spy cron entry */5 * * * * /usr/bin/php /opt/scripts/trunk-spy/trunk-spy.php > /opt/scripts/trunk-spy/log.txt
Above cron entry will run the script every 5 minutes. It means notifications will never be real time. If you have a very long list of repos to monitor, 5 minutes may not be ideal. Increase/decrease interval as needed. It may take down your server if it is under-powered.
The tool is a good example on how you combine the already existing tool to create yet another useful tool. However, I think SVN commit hooks would be better than this as it can notify real-time. I already setup this tool on our development server and it should start notifying next week as work resumes. A good way to sped the long weekend indeed.
View Trunk Spy on github – https://github.com/lysender/trunk-spy.
Enjoy and share.