Nikita Fedyashev
Nikita Fedyashev

Reputation: 18993

Capistrano: How to set global environment variable?

I need to find a way to set global env variable from Capistrano. Actual value is generated on in runtime, I can not check it in the repo and load from there.

This value must be loaded as ENV['RAILS_ASSET_ID'] in one initializer.

How can I do so?

Upvotes: 3

Views: 5066

Answers (4)

Flackou
Flackou

Reputation: 3661

Can't you use the default_environment capistrano variable ? cf github/capistrano

For instance, we can use it for rbenv in production :

set :default_environment, {
'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
}

Upvotes: 6

tadman
tadman

Reputation: 211540

If by global variable you mean something that is generated in Capistrano and later used in Rails then yes, you will have to create some sort of file. Ruby variables do not persist between runs. Environment variables can be set but only apply to sub-processes.

One way to dump a file on the server during your deployment is like what you have there, only remote:

run "echo #{asset_version} > #{release_path}/config/asset_version.conf"

You can later pick up and read that value as you've done.

Upvotes: 3

Unixmonkey
Unixmonkey

Reputation: 18784

How about just specifying it in your initializer?

ENV['RAILS_ASSET_ID'] = 12345

Upvotes: 3

Nikita Fedyashev
Nikita Fedyashev

Reputation: 18993

As a workaround, I'm using this method so far:

desc 'My custom task'
task :task_foo do
  asset_version = 12345
  system "echo #{asset_version} > RAILS_ASSET_ID"
end

and it is retrieved in intializer :

File.open(Rails.root.join 'RAILS_ASSET_ID').read.strip]

But there must be a better way. Any ideas?

Upvotes: 1

Related Questions