How To Use A GitHub Repository With Redmine Using Cron


Are you a developer? Do you use GitHub to host your repo? Last question, do you use Redmine to manage your projects? It does not really matter if you answer those questions or not, I am going to write this how-to anyway. I needed a way to use my GitHub repo with our Redmine install at http://code.idlesoft.net/, so I started googling for answers. After finding numerous pages that just told me to quit and host a git server myself, I came up with my own solution. (AFAIK, Git server’s are a real pain if you don’t do the procedure perfectly) The answer is your friendly neighborhood Cron service. Now if you do not run Linux then sorry, I can not help you. Anyways, here’s the how-to.

First you need to clone the repo you want to link with Redmine, do this by typing the following in the linux console (I am using /var/git/repos as the parent directory for my git repos, you can choose something different):

cd /var/git/repos
git clone git@github.com:yourname/projectname.git

You may have to create that parent directory first, you may also need root access depending on your server setup.

Make sure the user that Redmine runs under (usually the same as the HTTP user) has read and write permissions (I just make the web user the owner, it’s the easiest). For me, that user is www-data:

chown -hR www-data:www-data /var/git/repos/

Next, you’ll need to edit the web user’s crontab, get root permissions and type the following:

crontab -u www-data -e

You will see a text editor window open and, depending on whether the user’s crontab has been modified before, lines with cron commands. Do not worry if there is already text in there, as it is unimportant. Create a new line and type the following:

*/2 * * * * cd /var/git/repos/projectname && git pull >> /dev/null

Let me explain each part of that line:

*/2 * * * *

The above tells cron to perform the command every two minutes of every hour, of every day, of every month (so, every two minutes)

cd /var/git/repos/projectname && git pull >> /dev/null

This is the command that cron will perform. Change into the project directory and pull the latest code from github. The “>> /dev/null” means to throw out all the output, which we don’t want to see. If you do want to see that output, change /dev/null to the path of a log like “/home/bob/git.log”.

Now in Redmine, go to your project’s settings and click on the Repository tab. Select Git from the drop-down and for the path, enter in the path to your project repo, with /.git/ appended to the end, so:
/var/git/repos/projectname/.git/

Save it, and you are done! Check out your repository on the Repository tab of your project.

One more thing, you’ll need to make sure permissions are set each time you add a new repo.

8 Comments

Have u seen this http://mentalized.net/journal/2009/08/03/redmine_plugin_github_hook/ ?

There are not many feedback comments on the post, so i thought would run it against u.

I haven’t heard of it up until now. Thanks for the link! :)

I was using the exact same method you describe to keep my git repositories up to date in Redmine. I wrote the Github Hook plugin to get away from the cronjob part, and I’d love to hear any feedback you might have.

Yes, I will try and give that a look sometime and link back to you. :)

[...] in April, I explained how to use a GitHub repository with Redmine using Cron. If that seemed complicated, do not worry, there is now an easier way. I was recently asked for my [...]

Hi!

I’m very happy that you did this how-to! Thanks a lot!

I did what you said, but I have one problem. The cronjob will ask for:
Enter passphrase for key ‘/home/user/.ssh/id_rsa’:

So when the cronjob runs, I don’t think that it’ll work…

Do you think there’s any trick for this?

Thanks!

Yeah, this kind of stuff won’t work with encrypted keys right now, and I can’t find anywhere in the Git docs of a way to include ssh information. Your only option right now is to use a key without a passphrase.

i am trying this to my local computer and having an error on public key, it says permission denied. i think eddie&tommy is right that it won’t work on keys with passphrase.

as for dipen, git_hub hook is useful if your server is exposed on the internet.

thanks for this anyways

Leave a Comment