首页 > 解决方案 > 打包一个 Git Gem 以部署到 Ruby Lambda 运行时

问题描述

我正在构建一个 Ruby 2.7 Lambda 应用程序。

我的应用程序依赖于 github 中存在的 gem。

gem 'my-gem', git: 'https://github.com/my-org/my-gem', branch: 'main'

我想编写一个脚本来构建一个包含此依赖项的部署 zip 文件。

当我运行时bundle install,我的 gem 安装到 vendor/bundle/ruby/2.7.0/bundler/gems/my-gem-GITHASH。

对于 Lambda 包装,我相信我需要构建以下内容

以下脚本操作可以组装此结构,但我希望我有一个更简单的方法。

    cd vendor/bundle/ruby/2.7.0/bundler/gems/my-gem-* 
    # build the git gem (*.gem)
    gem build 
    # copy the gem and the gemspec to the vendor/bundle/ruby/2.7.0 directories
    cp *.gem ../../../gems 
    cp *.gemspec ../../../specifications/my-gem-1.0.0.gemspec 
    # upack the .gem file in the proper directory
    cd ../../../gems 
    gem unpack *.gem 
    # return to the working directory
    cd ../../../../.. 
    # Zip the dependencies 
    zip -r deploy.zip \
           vendor/bundle/ruby/2.7.0/gems \
           vendor/bundle/ruby/2.7.0/specifications \
           vendor/bundle/ruby/2.7.0/extensions \
           lib

我很想找到一个更简单的方法来解决这个问题。

标签: rubyaws-lambdarubygems

解决方案


另一种解决方案是将bundler/gems路径添加到$LOAD_PATH,如下所示:

load_paths = Dir['./vendor/bundle/ruby/2.7.0/bundler/gems/**/lib']
$LOAD_PATH.unshift(*load_paths)

(在最顶部lambda_function.rb


推荐阅读