无法在 ruby Gemfile 中包含 'git' gem

Not able to include 'git' gem in ruby Gemfile

我是 ruby 的新手,我创建了一个使用 Git gem 的脚本。 (require 'git')。 我必须对 jenkins 执行此脚本,为此我添加了 GemfileGemfile.lock,条目如下:

Gemfile
source 'https://rubygems.org'
gem 'pg'
gem 'git', '~> 1.3'

Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    pg (0.18.4)
    git (1.3.0)

PLATFORMS
  ruby

DEPENDENCIES
  pg
  git

当尝试使用以下命令通过 jenkins 执行脚本时:

#!/bin/bash -l
rvm use 1.9.3
bundle install --gemfile Gemfile --deployment
bundle exec ruby processMetadata.rb

请帮我解决下面提到的错误:

Using /usr/local/rvm/gems/ruby-1.9.3-p551
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

You have added to the Gemfile:
* git (~> 1.3)

You have deleted from the Gemfile:
* git
/usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler/definition.rb:181:in `rescue in specs': Your bundle is locked to git (1.3.0), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of git (1.3.0) has removed it. You'll need to update your bundle to a different version of git (1.3.0) that hasn't been removed in order to install. (Bundler::GemNotFound)
    from /usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler/definition.rb:175:in `specs'
    from /usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler/definition.rb:235:in `specs_for'
    from /usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler/definition.rb:224:in `requested_specs'
    from /usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler/runtime.rb:118:in `block in definition_method'
    from /usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler/runtime.rb:19:in `setup'
    from /usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler.rb:99:in `setup'
    from /usr/local/rvm/gems/ruby-1.9.3-p551@global/gems/bundler-1.13.1/lib/bundler/setup.rb:20:in `<top (required)>'
    from /usr/local/rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /usr/local/rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
Build step 'Execute shell' marked build as failure

您似乎手动制作了 Gemfile.lock 文件。这通常不需要,因为该文件是由 bundle 自动生成的。它基本上包含捆绑器为您下载的确切版本号。

Bundle,当 运行 处于部署模式 (--deployment) 时,将检查您的两个文件,如果它们之间存在任何不匹配,将拒绝 运行,或者如果 Gemfile.lock 文件需要任何更新。这是作为健全性检查完成的,因为这些文件应该在开发期间同步制作,而不是在生产期间制作。

尝试删除 Gemfile.lock,然后重新开始。由于您需要使用的版本号已经存在于您的 Gemfile 中,因此应该可以毫无问题地生成正确的 Gemfile.lock。另外,一开始不要在 --deployment 模式下 运行 它,因为那样不会生成 Gemfile.lock 文件。

所有这些步骤实际上都记录在您从捆绑程序收到的错误消息中:

You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

请注意,之后生成的 Gemfile.lock 文件仍然很重要(它包含您正在使用的确切版本),并且应该是您存储库的一部分。

您可能遇到的另一个问题是您仍在使用 ruby 1.9,因此您还必须修复 Gemfile 中的 pg gem 版本像这样:

source 'https://rubygems.org'
gem 'pg', '~> 0.18.4'
gem 'git', '~> 1.3'

结果 Gemfile.lock 将是这样的:

GEM
  remote: https://rubygems.org/
  specs:
    git (1.3.0)
    pg (0.18.4)

PLATFORMS
  ruby

DEPENDENCIES
  git (~> 1.3)
  pg (~> 0.18.4)