如何配置 Bundler/Gemfile 在开发过程中使用不同的 gem 源?

发布于 2024-12-03 04:49:32 字数 834 浏览 1 评论 0原文

我有一个 Sinatra 应用程序,需要我在本地开发的另一个 gem。我在配置 Bundler 时遇到问题,以在开发期间使用我的本地 gem 代码,但在生产中使用我供应的 gem 代码。

理想情况下,我可以做这样的事情,但 Bundler 不允许您两次指定相同的 gem:

# Doesn't work:
group :development do
  gem 'awesome', :path => "~/code/awesome"
end

group :production do
  gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end

与此同时,我不得不手动供应 gem & 。每次部署的时候都要更新Gemfile中的gem源,比较麻烦。我的工作流程是这样的:

  1. 在开发过程中指向我的本地 gem (gem 'awesome', :path => "~/code/awesome")
  2. 准备部署时,将 gem 解压到 vendor /gems
  3. 更新 Gemfile 以指向供应商的 gem (gem 'awesome', :path => "vendor/gems/awesome-0.0.1")
  4. 运行 bundle install (更新 Gemfile.lock)
  5. 部署代码
  6. 返回步骤 1。

好麻烦!我想做一些比简单地编写 Rake 任务更干净的事情来自动化我当前的设置。

此场景的最佳工作流程是什么?

I have a Sinatra application that requires another gem I'm developing locally. I'm having trouble configuring Bundler to use my local gem code during development but my vendored gem code in production.

Ideally I could do something like this, but Bundler doesn't allow you to specify the same gem twice:

# Doesn't work:
group :development do
  gem 'awesome', :path => "~/code/awesome"
end

group :production do
  gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end

In the meantime I've resorted to manually vendoring the gem & updating the gem source in the Gemfile every single time I deploy, which is quite a hassle. My workflow is this:

  1. Point to my local gem during development (gem 'awesome', :path => "~/code/awesome")
  2. When ready to deploy, unpack gem into vendor/gems
  3. Update Gemfile to point to vendored gem (gem 'awesome', :path => "vendor/gems/awesome-0.0.1")
  4. Run bundle install (to update Gemfile.lock)
  5. Deploy code
  6. Return to step 1.

What a hassle! I'd like to do something cleaner than simply writing Rake tasks to automate my current setup.

What's the best workflow for this scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぽ尐不点ル 2024-12-10 04:49:32

有一个新功能可以通过简单地指定 local.gem_name 配置选项来实现这一点,例如:

bundle config local.rack ~/path/to/local/rack

仅当 gem 具有在 Gemfile 中指定的 git 存储库和分支时才有效。

有关更多详细信息,请参阅 Bundler 文档: http://bundler.io/v1.3/bundle_config.html

There is a new feature that allows to do that, by simply specyfing local.gem_name config option, like:

bundle config local.rack ~/path/to/local/rack

This only works if the gem has a git repo and branch specified in the Gemfile.

See thr Bundler docs for more details: http://bundler.io/v1.3/bundle_config.html

遮了一弯 2024-12-10 04:49:32

显然,您可以在 Gemfile 中使用常规 Ruby。根据 这篇文章 你可以设置一个环境变量(或者我发现的任何其他变量),让你选择您想要使用的 gem 版本。

## based on an ENV variable
if ENV['RACK_ENV'] == "development"
  gem 'awesome', :path => "~/code/awesome"
else
  gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end

也许这会起作用。如果您需要供应正在进行的 gem,也许您可​​以制作一个小脚本来设置 ENV、供应它并重置 ENV。呃?

Apparently, you can use regular Ruby in your Gemfile. According to this article you can set an environment variable (or any other variable, I've found), to let you pick which version of a gem you want to use.

## based on an ENV variable
if ENV['RACK_ENV'] == "development"
  gem 'awesome', :path => "~/code/awesome"
else
  gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end

Maybe that'll work. If you need to vendor your in-progress gem maybe you could make a tiny little script that'll set the ENV, vendor it, and reset the ENV. Eh?

紙鸢 2024-12-10 04:49:32

如果您使用 docker 构建容器,则始终可以在 dockerfile 中将路径设置为环境变量,并在 Gemfile 中使用此环境变量。请在下面找到 Dockerfile 和 Gemfile 的示例。

Dockerfile

ARG tenant
ENV mgm=3
ENV GEMBOX_URL='abc.com:9292'
WORKDIR /app
COPY Gemfile* ./
RUN bundle install --without development test
COPY . .
ENTRYPOINT ["entrypoint.sh"]
CMD ["crond", "-f"]

Gemfile

source 'https://rubygems.org/'
source ENV['GEMBOX_URL']
gem 'jwt'
gem 'activerecord-import'
gem 'wicked_pdf' 
gem 'wkhtmltopdf-binary-edge', '~> 0.12.5.0'
gem 'zgear', '~> 0.6.4.1', source: ENV['GEMBOX_URL']
gem "piston", '~> 1.3.1', source: ENV['GEMBOX_URL']
gem 'communication_connector', '~> 0.1.4', source: ENV['GEMBOX_URL']
gem 'health_check', source: ENV['GEMBOX_URL']

If you are using doccker to build your containers, you can always set the path as an environment variable in the dockerfile, and use this environment variable in Gemfile. Please find an example of Dockerfile and Gemfile below.

Dockerfile

ARG tenant
ENV mgm=3
ENV GEMBOX_URL='abc.com:9292'
WORKDIR /app
COPY Gemfile* ./
RUN bundle install --without development test
COPY . .
ENTRYPOINT ["entrypoint.sh"]
CMD ["crond", "-f"]

Gemfile

source 'https://rubygems.org/'
source ENV['GEMBOX_URL']
gem 'jwt'
gem 'activerecord-import'
gem 'wicked_pdf' 
gem 'wkhtmltopdf-binary-edge', '~> 0.12.5.0'
gem 'zgear', '~> 0.6.4.1', source: ENV['GEMBOX_URL']
gem "piston", '~> 1.3.1', source: ENV['GEMBOX_URL']
gem 'communication_connector', '~> 0.1.4', source: ENV['GEMBOX_URL']
gem 'health_check', source: ENV['GEMBOX_URL']
紧拥背影 2024-12-10 04:49:32

这是一个我没有完全发挥作用的建议(用于狂欢主题,我在主题中的一些样式表中遇到了问题):

group :production do
      gem 'gemname', '~> 0.1.6', :git => 'git://github.com/foouser/gemname.git'
end

group :development do
      gem 'gemnamedev', :path => '~/path/gemname' # use local version
end

复制您的 gemname.gemspec 文件并将其命名为 gemnamedev.gemspec,然后更改里面的 s.name它是“gemnamedev”。

Here is a suggestion which I didn't get to fully work (used for a spree theme and I got problems with some stylesheets from the theme):

group :production do
      gem 'gemname', '~> 0.1.6', :git => 'git://github.com/foouser/gemname.git'
end

group :development do
      gem 'gemnamedev', :path => '~/path/gemname' # use local version
end

Duplicate your gemname.gemspec file and call it gemnamedev.gemspec, and change s.name inside it to "gemnamedev".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文