I recently started using Redmine for numerous projects and could not be happier with the application so far. Not only is it actively developed and already includes a ton of great features but it doesn’t seem bloated at all. Many of the projects I have configured for Redmine have their code base stored on Github so I needed to find a way to add code updates that referenced tickets to the tickets themselves. At first I was having a bit of trouble understanding exactly how this should be achieved but after digging around it ended up being pretty easy. You will need to configure local repos underneath your Redmine project that are updated fairly regularly and then after they are updated you will need to run a script that updates the any ticket that code has been associated with.
I ended up writing a Ruby script that I run in cron to update the repos and then runs Repository.fetch_changesets to add the updates within Redmine tickets. Below I include the code for the script, the data to add a cronjob, and information about logging.
Here is the ruby script itself which I store in a scripts directory underneath the user that runs the Redmine applications. I initially took a script I found online hereand then added extra steps. I have two Redmine installations one of which is a development install so I can test any new plugins, import of data, etc. before pushing the changes live and the other installation is our live installation. So if you only have one instance of Redmine you can remove all of the DEV labeled items from the script below.
There are numerous items that need to be changed to reflect your server environment including paths, app locations, and the log file. Also I am a newbie to Ruby so any recommendations on how to improve the below script are appreciated.
Redmine Github Fetch Changesets Code:
- #!/usr/bin/env ruby
- require 'rubygems'
- require 'fileutils'
- time_zone = 'CDT'
- ROOT = '/home/web/redmine'
- DEVROOT = '/home/web/devredmine'
- REPOS = '/git_repos/*'
- REPOROOT = ROOT + REPOS
- REPODEVROOT = DEVROOT + REPOS
- puts Time.now
- Dir[REPOROOT].each do |path|
- puts path
- FileUtils.cd(path)
- system("git pull")
- end
- Dir[REPODEVROOT].each do |path|
- puts path
- FileUtils.cd(path)
- system("git pull")
- end
- puts DEVROOT
- FileUtils.cd(DEVROOT)
- puts "...fetching changesets"
- system("/usr/local/bin/ruby script/runner \"Repository.fetch_changesets\" -e production")
- puts ROOT
- FileUtils.cd(ROOT)
- puts "....fetching changesets"
- system("/usr/local/bin/ruby script/runner \"Repository.fetch_changesets\" -e production")
- puts "Update Done! \n \n \n"
Make sure the script is owned by the same user that runs your Redmine installation which in my case is the web user. After creating the script also make sure it is executable by the same user. You will need to also create a folder under the Redmine application folder to store your git repositories from github. Once the folder is created clone each project using the below syntax.
Git Clone Each Project:
- git clone git@github.com:userid/project.git
Now below is the cron entry which in my case is again for the web user. You can modify it to run as much or as little as you like but in my case it runs once every 15 minutes.
Redmine Github Fetch Changesets Crontab Entry:
- # script to update repos for redmine
- */15 * * * * /usr/local/bin/ruby /home/web/scripts/redmine_fetch_git.rb >> /var/log/updaterepos.log
Notice in the above I output the results to /var/log/updaterepos.log so if you would like to do the same thing simply create a file in /var/log by whatever name you like. Make sure it is owned by the correct user (web user in my case) and that this user has access to edit the file.
I also suggest making sure that the log file is rotated by something like logrotate so it is not forgotten and ends up growing out of hand. I added a updaterepos file in the /etc/logrotate.d/ directory with the below contents.
Update Repos Logrotate File:
- /var/log/updaterepos.log {
- rotate 3
- missingok
- notifempty
- size 500k
- create 0644 web web
- }
With a couple modifications you will be posting Github updates to your Redmine tickets in no time. Check out the below books for more knowledge to sharpen your Ruby skills.