Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Correct Way To Add a :staging group to My Gemfile

I have remotes set up on Heroku for production and staging.

On staging I have set the app's envs to include:

RACK_ENV=staging
RAILS_ENV=staging

I would like to be able to specify a staging group in my Gemfile in the same way I can currently use production, test or assets:

group :staging do
  gem "example", "~> 0.9"
end

I understand how to add custom groups. From my application.rb:

  groups = {
    assets: %w(development test)
  }
  Bundler.require(:security, :model, :view, *Rails.groups(groups))

But how do I add a group that is only loaded in staging?

I've tried without success:

  groups = {
    assets: %w(development test),
    staging: %(staging)
  }
  Bundler.require(:security, :model, :view, *Rails.groups(groups))
like image 680
Undistraction Avatar asked Oct 30 '13 21:10

Undistraction


People also ask

Can I edit Gemfile lock?

Important! Gemfile. lock is automatically generated when you run bundle install or bundle update . It should never be edited manually.

Should we commit Gemfile lock?

You can (and should) specify versions of gems in your Gemfile but without the Gemfile. lock being included in Git. Then nobody else will get to benefit from your work of checking that the updated Gems still work. You should check your Gemfile for updates frequently and bundle update where appropriate.

How do you create a Gemfile in Ruby?

Gemfiles require at least one gem source, in the form of the URL for a RubyGems server. Generate a Gemfile with the default rubygems.org source by running bundle init . If you can, use https so your connection to the rubygems.org server will be verified with SSL.


1 Answers

Your Gemfile could include a group as follows:

# Gemfile
  group :staging do
    gem 'example','~>1.0'
  end

Create an environment for staging

# /config/environments/staging.rb
... 
copy config/environments/production.rb code here with adjustments as needed
...

The reason this works is found in /config/application.rb.

Rails.groups includes the :default group (all ungrouped gems) and the gem group matching the name of the environment, set by RAILS_ENV, which in this case would be "staging". Your require. Your Bundler.require should look like:

Bundler.require *Rails.groups(:assets => %w(development test))

For more info regarding Bundler and groups, read http://bundler.io/v1.5/groups.html

like image 56
Glenn Avatar answered Oct 05 '22 23:10

Glenn