git post-receive hook 拋出“找不到命令”錯誤,但似乎運行正常,手動運行時沒有錯誤

git post-receive hook 拋出“找不到命令”錯誤,但似乎運行正常,手動運行時沒有錯誤

我有一個 post-receive 鉤子,它在使用 gitolite 設定的中央 git 儲存庫上運行,以觸發臨時伺服器上的 git pull 。它似乎工作正常,但運行時拋出“命令未找到”錯誤。我正在嘗試找出錯誤的根源,但沒有任何運氣。手動運行相同的命令不會產生錯誤。

錯誤會根據推送到中央儲存庫的提交中所做的操作而變化。例如,如果“git rm”已提交並推送到中央存儲庫,則錯誤訊息將為“remote: hooks/post-receive: line 16:Removed: command not find”,如果“git add”已提交並推送到中央儲存庫的錯誤訊息將為「遠端:掛鉤/後接收:第16 行:合併:找不到命令」。在任何一種情況下,儘管出現錯誤訊息,但在臨時伺服器上執行的「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$

正如您所看到的,接收後腳本到達線路echo "Done!",當我查看登台伺服器時,該腳本git pull已成功運行,但仍然存在煩人的錯誤訊息。

任何有關在哪裡查找錯誤消息來源的建議將不勝感激。我很想將 stderr 重定向到 /dev/null 但更想知道問題是什麼。

答案1

該鉤子可能在未將 PATH 設為正常值的情況下運行。您是否嘗試過使用完整路徑ssh?否則,請在腳本運行時查看環境變數。您可以使用「匯出」來轉儲它們的列表,它們可能不是您想像的那樣。

相關內容