簡體   English   中英

Capistrano沒有為捆綁器設置RAILS_ENV

[英]Capistrano doesn't set RAILS_ENV for bundler

在我的Gemfile中,我基於RAILS_ENV指定要在git存儲庫上使用的分支。 但是,在Capistrano部署時,它會運行bundle install命令-由於它是通過Shell運行的,因此未設置適當的環境(登台)。 它默認為開發,並給我一個錯誤,指出Gemfile.lock與所安裝的內容不匹配。

您嘗試在更改Gemfile后以部署模式安裝。 在其他地方運行bundle install然后將更新的Gemfile.lock添加到版本控制中。

您已添加到Gemfile:*來源:git@bitbucket.org:MyRepository / manager-engine.git(在開發中)

您已從Gemfile中刪除:*源:git@bitbucket.org:MyRepository / manager-engine.git(在主目錄中)

您已在Gemfile中進行了更改:*經理從git@bitbucket.org:MyRepository/manager-engine.git (at develop)更改為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 }"

也就是說,如果Rails環境不是“生產”或“分期”,則使用“開發”分支。

部署/ 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}

因此最簡潔:

在常規SSH終端中,要在適當的環境下安裝存儲庫gem,我必須發出RAILS_ENV=staging bundle install 否則,只需運行bundle install從develop分支安裝存儲庫。 由於Capistrano僅運行bundle install ,並且不包括RAILS_ENV ,因此發生了此問題。 但是Capistrano是否未設置:rails_env,或者這不是真實的系統環境變量嗎?

我想如果沒有更好的方法...

我最終使用了SO中其他地方說明的方法,並根據需要對其進行了修改。

修改Gemfile使其包含:

# 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'

如果.rails-env包含任何內容或不存在,則默認為開發。 否則,它將第一個單詞作為環境。 要從命令行創建文件,假設您位於應用程序的根目錄中,只需鍵入echo "your-environment-name-here" > .rails-env

您還可以在每次使用capistrano進行部署時使用上面的命令創建文件,或者僅創建指向文件的符號鏈接並在部署之間共享它:

Deploy.rb:

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

因此,現在可以通過應用程序根目錄中名為.rails-env的文件來強制使用環境。 明確的RAILS_ENV調用(例如RAILS_ENV=test bundle exec ...仍將按公告進行工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM