Git post-接收挂钩未使用 rbenv 指定的 ruby 版本

Git post-receive hook not using ruby version specified by rbenv

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

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

错误

Bundler::RubyVersionMismatch: Your Ruby version is 2.7.0, but your Gemfile specified 2.7.1

手动 ssh'ing 到服务器和 运行 代码,它显示 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]

post-接收挂钩

#!/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

.bash_profile

. ~/.bashrc

.bashrc

...
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

$ type rbenv returns

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
}

我不太了解应用程序级别以下的代码,所以我不确定这里出了什么问题。

有什么方法可以解决这个问题吗?

几年前我曾遇到过同样的问题,但使用的是 cron。

我认为您的 .bashrc 需要一些详细信息。 bundlerake 命令是 rbenv 垫片,但您没有将它们加载到您的路径中。 .bashrc

export PATH="$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

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

...
$HOME/.rbenv/shims/bundle install
$HOME/.rbenv/shims/rake db:create
$HOME/.rbenv/shims/rake db:migrate
$HOME/.rbenv/shims/rake assets:precompile
...

我通过简单地在 post-receive 挂钩中加载 rbenv 来解决这个问题,而不是在 .bashrc.

# hooks/post-receive

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"