gitreceiveを使う

git push させるだけで何か処理をする何かを作りたいときに重宝する(PaaS的な意味で)

サーバ側

wget -O /tmp/gitreceive https://raw.github.com/progrium/gitreceive/master/gitreceive
mv /tmp/gitreceive /usr/local/bin/
adduser git --disabled-password
echo 'git ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/80-git
chown git:git /usr/local/bin/gitreceive 
/usr/local/bin/gitreceive init

/home/git/receiver に git push 時に行いたい処理を書く。(引数$1から$4に情報が入ってくる)
以下は /tmp/app/リポジトリ名/リビジョン にソースコードを展開する例。

/home/git/リポジトリ名にgitリポジトリが形成されるのでそれを使ってもいい。
あと、git push --tags で反応しないようにするものが参考になったので書いておいた。

#!/bin/bash

app=${1%%.git}

## Exit if there is no revision to support branch deletion
if [[ "$2" == "0000000000000000000000000000000000000000" ]]; then
  echo "----> No revision to deploy."
  exit
fi

## Deploy code
echo "----> Deploying code to /tmp/app/$app/$2 ..."
rm -rf /tmp/app/$app
mkdir -p "/tmp/app/$app/$2" && cat | tar -x -C "/tmp/app/$app/$2"

クライアント側では以下の感じでgit pushするとよい

# あらかじめsudo gitreceive実行可能なユーザでssh可能にしておく(できればパスワード不要で)
# ここでは foo ユーザとする(ssh-copy-id -i ~/.ssh/id_dsa.pub foo@server などでOK)
% cat ~/.ssh/id_rsa.pub | ssh foo@server "sudo gitreceive upload-key $USER"
% git remote add deploy git@server:application-name
% git push deploy master