git post-receive フックは「コマンドが見つかりません」というエラーをスローしますが、手動で実行すると正常に実行され、エラーは発生しません。

git post-receive フックは「コマンドが見つかりません」というエラーをスローしますが、手動で実行すると正常に実行され、エラーは発生しません。

gitolite でセットアップされた中央 git リポジトリで実行される post-receive フックがあり、ステージング サーバーで git pull をトリガーします。正常に動作しているように見えますが、実行すると「コマンドが見つかりません」というエラーが発生します。エラーの原因を突き止めようとしていますが、うまくいきません。同じコマンドを手動で実行しても、エラーは発生しません。

エラーは、中央リポジトリにプッシュされているコミットで何が行われたかによって変わります。たとえば、「git rm」がコミットされて中央リポジトリにプッシュされた場合、エラー メッセージは「remote: hooks/post-receive: line 16: Removed: command not found」となり、「git add」がコミットされて中央リポジトリにプッシュされた場合、エラー メッセージは「remote: hooks/post-receive: line 16: Merge: command not found」となります。どちらの場合でも、ステージング サーバーで実行される「git pull」は、エラー メッセージにかかわらず正常に動作します。

受信後のスクリプトは次のとおりです。

#!/bin/bash
#
# This script is triggered by a push to the local git repository.  It will
# ssh into a remote server and perform a git pull.
#
# The SSH_USER must be able to log into the remote server with a
# passphrase-less SSH key *AND* be able to do a git pull without a passphrase.
#
# The command to actually perform the pull request on the remost server comes
# from the ~/.ssh/authorized_keys file on the REMOTE_HOST and is triggered
# by the ssh login. 

SSH_USER="remoteuser"
REMOTE_HOST="staging.server.com"

`ssh $SSH_USER@$REMOTE_HOST` # This is line 16

echo "Done!"

ステージング サーバーで git pull を実行するコマンドは、ssh ユーザーの ~/.ssh/authorized_keys ファイルにあり、次のようになります。

command="cd /var/www/staging_site; git pull",no-port-forwarding,no-X11-forwarding,no-agent-forwarding, ssh-rsa AAAAB3NzaC1yc2EAAAABIwAA... (the rest of the public key)

これは、ローカル リポジトリからファイルを削除し、ローカルにコミットして、中央の Git リポジトリにプッシュした場合の実際の出力です。

ben@tamarack:~/thejibe/testing/web$ git rm ./testing
rm 'testing'
ben@tamarack:~/thejibe/testing/web$ git commit -a -m "Remove testing file"
[master bb96e13] Remove testing file
1 files changed, 0 insertions(+), 5 deletions(-)
delete mode 100644 testing
ben@tamarack:~/thejibe/testing/web$ git push 
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 221 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: From [email protected]:testing
remote:    aa72ad9..bb96e13  master     -> origin/master
remote: hooks/post-receive: line 16: Removed: command not found # The error msg
remote: Done!
To [email protected]:testing
aa72ad9..bb96e13  master -> master
ben@tamarack:~/thejibe/testing/web$

ご覧のとおり、post-receive スクリプトがecho "Done!"行に到達し、ステージング サーバーで確認するとgit pull正常に実行されていますが、依然としてエラー メッセージが表示されます。

エラー メッセージの原因をどこで確認すればよいかに関する提案があれば、ぜひ教えてください。stderr を /dev/null にリダイレクトしたいのですが、何が問題なのかを知りたいです。

答え1

フックは、PATH が適切な値に設定されていない状態で実行されている可能性があります。 へのフルパスを使用してみましたかssh? それ以外の場合は、スクリプトの実行時に環境変数を確認してください。 'export' を使用してそれらのリストをダンプできますが、おそらく期待どおりではないでしょう。

関連情報