質問

In my Gemfile I specify what branch to use on a git repository based on RAILS_ENV. However when Capistrano deploys, it runs the bundle install command - and since it's run through a shell, the proper environment (staging) isn't set. It defaults to development and gives me an error stating that there's a mismatch between Gemfile.lock and what is installed.

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: * source: git@bitbucket.org:MyRepository/manager-engine.git (at develop)

You have deleted from the Gemfile: * source: git@bitbucket.org:MyRepository/manager-engine.git (at master)

You have changed in the Gemfile: * manager from git@bitbucket.org:MyRepository/manager-engine.git (at develop) to no specified source

Gemfile:

RAILS_ENV = ENV['RAILS_ENV'] || 'development'
gem 'manager', git: "git@bitbucket.org:MyRepository/manager-engine.git", branch: "#{ [:production, :staging].include?(RAILS_ENV.to_sym) ? :master : :develop }"

i.e., use the 'develop' branch if the rails environment is anything other than 'production' or 'staging'.

deploy/staging.rb:

set :branch, :master
set :keep_releases, 2
set :stage, :staging
set :rails_env, 'staging'
set :bundle_without, [:development, :test]
set :deploy_to, '/home/useraccount/rails_deployments/staging.www'
server 'localhost', user: 'useraccount', roles: %w{web app db}

So to be the most concise:

In regular SSH terminal, to install the repository gem under the proper environment, I have to issue RAILS_ENV=staging bundle install. Otherwise, just running bundle install installs the repository from the develop branch. Since Capistrano just runs bundle install and doesn't include the RAILS_ENV, this problem is occurring. But doesn't Capistrano set :rails_env, or is that not a real system environment variable?

役に立ちましたか?

解決

I guess if there's no better way...

I ended up using a method illustrated somewhere else in SO and modified it for my needs.

Modify Gemfile to contain:

# Read environment from A) Explicit set through command, B) Existence of override file's contents or C) Default to development
RAILS_ENV = ENV['RAILS_ENV'] || `cat .rails-env`.to_s.split.first || 'development'

If .rails-env contains nothing or doesn't exist, it defaults to development. Otherwise it takes the first word as the environment. To create the file from the command line, just type echo "your-environment-name-here" > .rails-env, assuming you're in the app's root directory.

You can also create the file upon every deploy with capistrano using the command above, or just create a symlink to the file and share it between deployments:

Deploy.rb:

set :linked_files, %w{ .rails-env }

So now the environment can be forced via a file in the root of your app called .rails-env. Explicit RAILS_ENV calls such as RAILS_ENV=test bundle exec ... will still work as advertised.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top