我有一个 post-receive 钩子,它在用 gitolite 设置的中央 git 存储库上运行,以触发暂存服务器上的 git pull。它似乎工作正常,但在运行时会抛出“命令未找到”错误。我试图追踪错误的来源,但没有任何运气。手动运行相同的命令不会产生错误。
错误会根据推送到中央存储库的提交中所执行的操作而变化。例如,如果提交了“git rm”并将其推送到中央存储库,则错误消息将为“远程:hooks/post-receive:第 16 行:已删除:未找到命令”,如果提交了“git add”并将其推送到中央存储库,则错误消息将为“远程:hooks/post-receive:第 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
?否则,请在脚本运行时查看环境变量。您可以使用“export”转储它们的列表,它们可能与您想象的不同。