akishin999の日記

調べた事などを書いて行きます。

Gitlab で作成したリポジトリでのコミットメール送信とリポジトリのミラーリング

実運用するにあたっては結構重要な push 時のコミットメール送信とバックアップとしてのリポジトリのミラー作成についてまとめてみました。
他に良いやり方があるかもですが、手元では以下のようにして実現しています。

コミットメール送信

まずは gitolite 実行ユーザでメール送信のための設定を行っておきます。

# su - git
$ git config --global hooks.emailprefix [Gitlab]
$ git config --global hooks.mailinglist git-ml@example.com
$ git config --global hooks.announcelist git-ml@example.com
$ git config --global hooks.envelopesender git@example.com
$ git config --global hooks.showrev "git show -C %s; echo"

後は post-receive hook の中で /usr/share/git-core/contrib/hooks/post-receive-email を実行すれば OK なのですが、Gitlab の場合は post-receive hook が上書きされるので、独自の処理を行いたい場合は Gitlab 側のファイルの方を編集しておく必要があります。

前回の通りインストールしている場合、ファイルは以下の場所にあります。

# vi  /home/gitlabhq/gitlabhq/lib/post-receive-hook 

以下のように post-receive-email スクリプトを呼び出すように修正します。

#!/usr/bin/env bash

# This file was placed here by Gitlab. It makes sure that your pushed commits
# will be processed properly.


. /usr/share/git-core/contrib/hooks/post-receive-email    #<= 追記

while read oldrev newrev ref
do
  # For every branch or tag that was pushed, create a Resque job in redis.
  pwd=`pwd`
  reponame=`basename "$pwd" | cut -d. -f1`
  env -i redis-cli rpush "resque:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\",\"$GL_USER\"]}" > /dev/null 2>&1
done

このスクリプトをみると Gitlab は resque を使って Job を登録したりしているようなので、メール送信も同じような仕組みでできないかな、と思って調べてみたのですが、イマイチ情報が少なく、実現方法がよく分からなかったため、ここではベタにスクリプトでメールを飛ばすことにしました。

このままだとリポジトリ名がちゃんとメールに記載されないので、post-receive-email スクリプトの 662 行目付近を以下のように修正しました。

# vi /usr/share/git-core/contrib/hooks/post-receive-email
# 以下のように修正
#projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
projectdesc=$(basename $(pwd) .git)

これで Gitlab 上で作成したプロジェクトに対して push を実行したタイミングで、指定したメールアドレスにメールが送信されるはずです。

参考:

多人数開発で Git を使う場合の環境構築 | GREE Engineers' Blog
http://labs.gree.jp/blog/2011/03/2885/

リポジトリミラーリング

ドキュメントを読むと gitolite にもリポジトリミラーリングの仕組みがあるようなのですが、私は以下のように簡単な hook スクリプトを書いて実行しています。

まずは準備として、git ユーザがリポジトリサーバ(serverA)とミラーサーバ(serverB)間で相互にパスワード無し SSH ログインを行えるように設定します。

serverA:# passwd git
serverA:# su - git
serverA:$ ssh-keygen -t rsa -P ""
serverA:$ ssh-copy-id -i ~/.ssh/id_rsa.pub git@serverB
serverA:$ ssh git@serverB

serverB:# passwd git
serverB:# su - git
serverB:$ ssh-keygen -t rsa -P ""
serverB:$ ssh-copy-id -i ~/.ssh/id_rsa.pub git@serverA
serverB:$ ssh git@serverA

お互いに SSH 接続が問題なく動作することを確認したら hook スクリプトを設置します。
作成したスクリプトは以下のようなものです。

環境に合わせ IP アドレスやパスは適宜変更してください。
ここではこのスクリプトを /home/git/bin/gitsync.sh として作成しました。

# vi /home/git/bin/gitsync.sh
# chmod +x /home/git/bin/gitsync.sh

コミットメールの時と同じく、Gitlab の post-receive-hook を編集します。

# vi  /home/gitlabhq/gitlabhq/lib/post-receive-hook 

以下のように作成した gitsync.sh スクリプトを実行するよう追記します。

#!/usr/bin/env bash

# This file was placed here by Gitlab. It makes sure that your pushed commits
# will be processed properly.


. /usr/share/git-core/contrib/hooks/post-receive-email
. /home/git/bin/gitsync.sh                                #<= 追記

while read oldrev newrev ref
do
  # For every branch or tag that was pushed, create a Resque job in redis.
  pwd=`pwd`
  reponame=`basename "$pwd" | cut -d. -f1`
  env -i redis-cli rpush "resque:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\",\"$GL_USER\"]}" > /dev/null 2>&1
done

これで push の度に指定したサーバ上にリポジトリが存在しなければ clone、存在すれば push --mirror を実行してくれるようになります。

以上です。

Gitlab については「入れてみた」といった情報が多く、あまり運用周りの情報が無いように思います。
もしもっと良いやり方をご存じの方がいたら教えて頂けると嬉しいです。