Raspberry PiでのGitサーバー構築とフック設定

このメモは、Raspberry Pi上に小さなGitサーバーを構築し、SSHキーによるアクセスを追加し、ベアリポジトリを作成し、post-receiveフックを使ってmasterブランチを作業ディレクトリへデプロイするための基本手順を記録したものです。

1. Git環境をセットアップする

Gitをインストールし、専用のgitユーザーを作成します。

sudo apt-get update
sudo apt-get install git
sudo adduser git

専用ユーザーを使うことで、リポジトリへのアクセスを通常のシステムユーザーから分離できます。

2. SSHキーペアを生成する

各クライアントマシンでキーペアを生成します。

ssh-keygen -t rsa -C "user@lazying.art"

秘密鍵はクライアント側に保持します。例は次のとおりです。

/home/pi/.ssh/id_rsa

各クライアントの公開鍵を、サーバー上のGitユーザーのauthorized keysファイルに追加します。

/home/git/.ssh/authorized_keys

.sshディレクトリとファイルの権限がSSHで許可される状態になっていることを確認します。

sudo mkdir -p /home/git/.ssh
sudo chmod 700 /home/git/.ssh
sudo chmod 600 /home/git/.ssh/authorized_keys
sudo chown -R git:git /home/git/.ssh

3. サーバー側リポジトリを初期化する

ベアリポジトリ用のディレクトリを作成し、リポジトリを1つ初期化します。

mkdir Git
cd Git
sudo git init --bare sample.git
sudo chown -R git:git sample.git

ベアリポジトリはGitサーバーで通常使われる形式です。Gitデータは保存しますが、チェックアウト済みの作業ツリーは持ちません。

gitユーザーをGit操作だけに制限するには、/etc/passwdを編集します。

sudo vim /etc/passwd

ユーザーのシェルをBashから変更します。

git:x:1001:1001:,,,:/home/git:/bin/bash

これをgit-shellにします。

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

この変更を行う前に、git-shellがシステム上に存在することを確認してください。

which git-shell

4. Gitサーバーへコードをプッシュする

クライアント側のプロジェクトディレクトリでGitのユーザー情報を設定し、最初のコミットをプッシュします。

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git add .
git commit -m "init commit"
git push -u origin master

まだリモートを追加していない場合は、先に追加します。ホストとパスは自分のサーバー情報に置き換えてください。

git remote add origin git@192.168.1.108:/home/pi/Git/sample.git

5. デプロイ用フックを設定する

ベアリポジトリ内にpost-receiveフックを作成します。

vim sample.git/hooks/post-receive

フックスクリプトを追加します。

#!/bin/bash
TARGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"

while read oldrev newrev ref
do
    # Only check out the branch that should be deployed.
    if [ "$ref" = "refs/heads/$BRANCH" ];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done

次に、所有者と実行権限を設定します。

chown git:git hooks/post-receive
chmod +x hooks/post-receive

プッシュによってフックが起動すると、Gitはリモートサーバーからの出力を表示します。権限が原因でデプロイに失敗した場合、次のように表示されることがあります。

Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 237 bytes | 23.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Ref refs/heads/master received. Deploying master branch to production...
remote: error: unable to create file main.py (Permission denied)
remote: error: unable to create file test.py (Permission denied)
remote: Already on 'master'
To 192.168.1.108:/home/pi/Git/printer.git
   2e6a796..6085cd0  master -> master

これはフック自体は実行されたものの、gitユーザーがデプロイ先ディレクトリへ書き込めなかったことを意味します。デプロイ先ディレクトリの所有者をgitに変更します。

chown git:git /path/to/deployment/directory/

権限を修正した後、成功したプッシュは次のように表示されることがあります。

Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 239 bytes | 21.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Ref refs/heads/master received. Deploying master branch to production...
remote: Already on 'master'
To 192.168.1.108:/home/pi/Git/printer.git
   6085cd0..b83c0d5  master -> master

よく使うクライアント側Gitコマンド

グローバルなGitユーザー名とメールアドレスを設定します。

git config --global user.name "username"
git config --global user.email "user@example.com"

新しく作成したリモートリポジトリをクローンし、READMEを追加してプッシュします。

git clone https://git.aiiage.com:9999/song.yl/ReID.git
cd ReID
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

既存のローカルフォルダを新しいリモートリポジトリへプッシュします。

cd existing_folder
git init
git remote add origin https://git.aiiage.com:9999/song.yl/ReID.git
git add .
git commit -m "Initial commit"
git push -u origin master

既存のGitリポジトリに新しいリモートを設定し、すべてのブランチとタグをプッシュします。

cd existing_folder
git remote rename origin old-origin
git remote add origin https://git.aiiage.com:9999/song.yl/ReID.git
git push -u origin --all
git push -u origin --tags

参考: yllifesong, CSDN, <https://blog.csdn.net/yllifesong/article/details/81041156>.

Leave a Reply