post-接收挂钩生成 'no such file or directory' 错误

post-receive hook generating 'no such file or directory' error

我正在尝试为生产网站设置 post-接收挂钩。该代码似乎部分有效,但未复制文件。在此目录中,我设置了一个裸存储库:

public_html/deploy-dir

在hooks/post-receive中有如下代码:

#!/usr/bin/env ruby
# post-receive

# 1. Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "

# 2. Only deploy if master branch was pushed
if (branch =~ /master$/) == nil
    puts "Received branch #{branch}, not deploying."
    exit
end

# 3. Copy files to deploy directory
deploy_to_dir = File.expand_path('../')
`GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"

遥控器url是这样的:

ssh://username@thesite.org/home/username/public_html/deploy-dir

执行'git push production master'时,我在终端中得到以下输出:

Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: /usr/bin/env: ruby: No such file or directory
To ssh://username@thesite.org/home/username/public_html/deploy-dir
    3a5ab4d..6c49ce0  master -> master

我认为 'no such file or directory' 错误与 post-receive 挂钩的第 1 行有关。那里缺少一些东西,我不知道它应该是什么。此 post-接收代码可在另一台服务器上运行。

或者,我的 deploy-to-dir 路径可能不正确?应该是:

deploy_to_dir = File.expand_path('../')

deploy_to_dir = File.expand_path('..')

正如您在评论中指出的那样,env 的输出表明 ruby 在远程端的 PATH 中不可用。

您有 git,这意味着您有 perl。您会考虑以下内容吗?

#!/usr/bin/env perl
# post-receive

use warnings;
use strict;

use Cwd 'abs_path';

# 1. Read STDIN (Format: "from_commit to_commit branch_name")
my($from,$to,$branch) = split " ", scalar <STDIN>;

# 2. Only deploy if master branch was pushed
if ($branch ne "master") {
    warn "Received branch $branch, not deploying.\n";
    exit 0;
}

# 3. Copy files to deploy directory
my $deploy_to_dir = abs_path('../');
$ENV{GIT_WORK_TREE} = $deploy_to_dir;
if (system("git checkout -f master") == 0) {
    print "DEPLOY: master($to) copied to '$deploy_to_dir'\n";
}
else {
    warn "DEPLOY: checkout failed\n";
    exit 1;
}

这只是 ruby 没有安装在服务器上的简单问题。已安装支持 ruby 并已解决问题。