Elastic Beanstalk:找不到带有可执行包的 gem 捆绑器 (>= 0.a) (Gem::GemNotFoundException)

Elastic Beanstalk: can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)

此错误消息是众所周知的错误消息。 (例如,参见 https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html。)虽然我是通过带有 Ruby 2.6.1 和捆绑器 2.0.1 的新 Elastic Beanstalk 应用程序获得它的。错误是:

  /opt/rubies/ruby-2.6.1/lib/ruby/site_ruby/2.6.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
from /opt/rubies/ruby-2.6.1/lib/ruby/site_ruby/2.6.0/rubygems.rb:308:in `activate_bin_path'
from /opt/rubies/ruby-2.6.1/bin/bundle:23:in `<main>' (ElasticBeanstalk::ExternalInvocationError)

我试过将以下文件:01_install_bundler.config 放入 .ebextensions 文件夹中:

container_commands:
  01_install_bundler:
    command: "gem install bundler —-version 2.0.1"

虽然这永远不会得到 运行 因为如果我查看上面的错误,我可以看到它是在部署过程的这一点发生的:

.../AppDeployStage0/AppDeployPreHook/10_bundle_install.sh] : Activity failed.

(即在 AppDeployPreHook 脚本的 bundle install 命令期间)。有关 PlatformHooks 的参考,请参阅 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html

我很确定,如果我能确保使用的bundler版本至少是2.0.0版本,那么就不会有问题。虽然我不知道如何轻松指定。目前,我正在通过 ssh 连接到服务器以 [​​=20=] 进行编辑并 fiddle 使用脚本。尽管我显然需要一种自动化的、可重复的方式。

令人沮丧的是 ruby 2.6.1 没有默认选择捆绑器版本 2.0.0。有什么想法吗?

================================

更新:

如果我编辑文件/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh

if [ -f Gemfile ]; then
  echo "running 'bundle install' with Gemfile:"
  cat Gemfile

  +++ gem install bundler +++
  if [ -d $EB_APP_STAGING_DIR/vendor/cache ]; then
    bundle install --local
  else
    bundle install
  fi
else
  echo "no Gemfile found! Skipping bundle install stage!"
fi

并添加 gem install bundler(没有加号),然后这解决了问题,因为它安装了最新的捆绑器,即 2.0.1。对于那些想了解 hack 的人,命令是:

eb ssh

sudo -i

cd /opt/elasticbeanstalk/hooks/appdeploy/pre

vim 10_bundle_install.sh

这个解决方案的问题是它感觉有点像 hack,因为它不使用 .ebextensions。有没有更合适的方法来解决这个问题?

所以这是上述问题的编程解决方案。在 .ebextensions/gem_install_bundler.config:

下创建以下文件
files:
  # Runs before `./10_bundle_install.sh`:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/09_gem_install_bundler.sh" :
    mode: "000775"
    owner: root
    group: users
    content: |
      #!/usr/bin/env bash

      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
      EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
      # Source the application's ruby, i.e. 2.6. Otherwise it will be 2.3, which will give this error: `bundler requires Ruby version >= 2.3.0` 
      . $EB_SCRIPT_DIR/use-app-ruby.sh

      cd $EB_APP_STAGING_DIR
      echo "Installing compatible bundler"
      gem install bundler -v 2.0.1

那你下次eb deploy的时候,bundler已经更新到2.0.1版本了,你就不会再出现上面的错误了。

此处文档中的更多信息:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html

这里:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-files

最后说明:确保您在 运行 宁 eb deploy 之前提交这些更改,或者暂存它们并 运行 eb deploy --staged。参见:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-cli-git.html。我很难学到这一点!

我只是在找到替代(也许更简单)的解决方案后才看到这个 post:将捆绑器降级到 1.17.3(gem unistall bundler 随后 gem install bundler -v 1.17.3

您需要生成锁定文件的捆绑器的正确版本。要找出该版本,请使用以下命令

$ cat Gemfile.lock | grep -A 1 "BUNDLED WITH"
BUNDLED WITH
   1.17.3

这是一个可以在新的 Amazon Linux 2 平台版本上使用的版本,因为旧的 /opt/elasticbeanstalk/hooks/ 文件夹已完全停用。它从 Gemfile.lock

中解析出捆绑器版本

此脚本将进入 .platform/hooks/prebuild/01_install_app_bundler.sh,需要将其标记为可执行,否则将因权限问题而失败 (chmod +x 01_install_app_bundler.sh)

#!/usr/bin/env bash

# Load environment data
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppStagingDir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppUser)
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppDeployDir)

# Set up correct environment
export $(cat /opt/elasticbeanstalk/deployment/env | xargs)

BUNLDER_VER_TO_INSTALL=$(grep -A 1 "BUNDLED WITH" Gemfile.lock | tail -n1 | tr -d ' ')

echo "Installing bundler $BUNLDER_VER_TO_INSTALL"
gem install bundler -v $BUNLDER_VER_TO_INSTALL

我在那里留下了一些未使用的 EB_ 变量,只是为了展示如何在更新的平台上确定它们。