如何使用 运行 bash 脚本从其目录外部更新 Rails 应用程序?

How to run a bash script to update a Rails app from outside its directory?

我有这个脚本应该是 运行 来自 /~ 目录:

#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt

如您所见,它提取新的提交并更新资产并重新启动 Apache。问题是当它运行 rake assets:precompile RAILS_ENV=production 行时,它说:

Could not find proper version of rake (12.1.0) in any of the sources
Run `bundle install` to install missing gems.

这很奇怪,因为执行此命令时我应该在应用程序的文件夹 (/root/apps/monitoring) 中。我做错了什么?

请尝试

#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
bundle exec rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt

使用 bundle exec 应该可以。

您可能需要在脚本中加载 rvm (https://rvm.io/workflow/scripting),并且可能 select 正确 ruby/gemset。

您也可以考虑使用 rvm wrapper

创建的 bundle 的包装器

我 运行 使用 bash 脚本包装器收集任务。技巧是在任务启动后使用source命令加载rvm环境

example.sh

#!/bin/bash
# change the directory
cd /home/ubuntu/GSeries
# load rvm ruby
source /home/ubuntu/.rvm/environments/ruby-2.4.2@mygemset
bundle exec rake  db:prune_logs RAILS_ENV="production" &>> /home/ubuntu/GSeries/log/prune_logs.log