Git post-receive hook 不使用 rbenv 指定的 ruby​​ 版本

tim*_*xyz 5 ruby git bash ubuntu digital-ocean

我正在将代码部署到 ubuntu 服务器,我已将其声明为 git 远程服务器git push prod master

但它使用了错误版本的 Ruby。

错误

Bundler::RubyVersionMismatch: Your Ruby version is 2.7.0, but your Gemfile specified 2.7.1
Run Code Online (Sandbox Code Playgroud)

手动 ssh到服务器并运行代码,它会显示正确的 ruby​​ 版本。

$ rbenv local #=> 2.7.1
$ rbenv global #=> 2.7.1
$ ruby -v #=> ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
Run Code Online (Sandbox Code Playgroud)

接收后挂钩

#!/bin/bash

GIT_DIR=/home/me/appname_prod
WORK_TREE=/home/me/appname
export APPNAME_DATABASE_USER='appname'

export RAILS_ENV=production
. ~/.bash_profile

while read oldrev newrev ref
do
    if [[ $ref =~ .*/master$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        mkdir -p $WORK_TREE
        git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
        mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log

        # start deploy tasks
        cd $WORK_TREE
        bundle install
        rake db:create
        rake db:migrate
        rake assets:precompile
        sudo restart puma-manager
        sudo service nginx restart
        # end deploy tasks
        echo "Git hooks deploy complete"
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done
Run Code Online (Sandbox Code Playgroud)

.bash_配置文件

. ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

.bashrc

...
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Run Code Online (Sandbox Code Playgroud)

$ type rbenv回报

rbenv is a function
rbenv () 
{ 
    local command;
    command="${1:-}";
    if [ "$#" -gt 0 ]; then
        shift;
    fi;
    case "$command" in 
        rehash | shell)
            eval "$(rbenv "sh-$command" "$@")"
        ;;
        *)
            command rbenv "$command" "$@"
        ;;
    esac
}
Run Code Online (Sandbox Code Playgroud)

我对应用程序级别以下的代码没有很好的理解,所以我不确定这里出了什么问题。

我可以尝试解决这个问题吗?

zhi*_*sme 2

几年前我也遇到过同样的问题,但是是用 cron 来解决的。

我认为你.bashrc需要一些细节。bundle命令rake是 rbenv 垫片,但您没有将它们加载到路径中。 .bashrc

export PATH="$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Run Code Online (Sandbox Code Playgroud)

并在你的命令前面加上垫片路径(我刚刚从我的 crontab 中获取代码)

...
$HOME/.rbenv/shims/bundle install
$HOME/.rbenv/shims/rake db:create
$HOME/.rbenv/shims/rake db:migrate
$HOME/.rbenv/shims/rake assets:precompile
...
Run Code Online (Sandbox Code Playgroud)