GitHub-Mark-120px-plusWhile we still have our own repositories located at https://git.netways.org it’s reasonable to sync the existing repositories to GitHub targetting a wider audience (and not everyone likes the gitorious web interface either). There are certain requirements syncing git repositories in general:

  • create, update and delete branches
  • create and delete tags
  • don’t always clone the repository but fetch local changes

Aside from that, you’ll need

  • a shell user, e.g. github on a box running the sync
  • ssh key for pushing remote origin
  • small sync script
  • a cronjob for that sync script, e.g. every 5 minutes
  • git binary >= 1.7.10 providing the –prune option

If you are planning to push your local repository (in our example, git.netways.org) to github.com, you’ll also need the following

  • add the public ssh key to your github user at https://github.com/settings/ssh
  • ensure that this user is allowed to push your company’s repositories (make it a team member)

The sync script part is easy thanks to the possibilities git already provides. An older version of the sync script is used for syncing the Icinga Github repositories in a similar fashion. For Icinga, it’s most important to sync the git tags, making the release tarballs available on Github.

#!/bin/bash
REPO_HOME="/data/scm/sync"
declare -A REPOS
# syntax is [github_repo]="netways_repo"
REPOS=([ingraph]="ingraph/ingraph" [lconf]="lconf/lconf" [lconf-icinga-module]="lconf/icinga-module" [lconf-web]="lconf/lconf-web")
cd $REPO_HOME
for REPO_GITHUB in "${!REPOS[@]}"
do
	REPO_LOCAL=${REPOS[$REPO_GITHUB]}
        echo "### Processing repo $REPO_LOCAL"
        if [ ! -d $REPO_LOCAL ]; then
                git clone --bare --mirror git://git.netways.org/$REPO_LOCAL.git $REPO_LOCAL
        fi
        (cd $REPO_LOCAL; git fetch --prune; git push --prune git@github.com:NETWAYS/$REPO_GITHUB.git +refs/heads/*:refs/heads/* +refs/tags/*:refs/tags/*)
done

git {fetch,push} –prune ensures that deleted branches/tags on the source repository are also deleted on the target repository.
Saving the sync script as /data/scm/sync/create_and_sync allows you to add the following cron job every five minutes:

# su - github
$ crontab -e
# Sync repos to github
*/5 * * * * /data/scm/sync/create_and_sync > /dev/null 2>&1

Ynetways_githubou can check the result watching the development branches at https://github.com/netways for LConf and inGraph – if you’re looking for your own custom git integration, don’t hesitate to contact us 🙂
PS: Another cool way of syncing github repositories is the Icinga Exchange integration!

Michael Friedrich
Michael Friedrich
Senior Developer

Michael ist seit vielen Jahren Icinga-Entwickler und hat sich Ende 2012 in das Abenteuer NETWAYS gewagt. Ein Umzug von Wien nach Nürnberg mit der Vorliebe, österreichische Köstlichkeiten zu importieren - so mancher Kollege verzweifelt an den süchtig machenden Dragee-Keksi und der Linzer Torte. Oder schlicht am österreichischen Dialekt der gerne mit Thomas im Büro intensiviert wird ("Jo eh."). Wenn sich Michael mal nicht in der Community helfend meldet, arbeitet er am nächsten LEGO-Projekt oder geniesst...