简体   繁体   中英

Rails capistrano set deploy.rb variables based on user logged in to computer

I'm working on a project with multiple developers and currently we have to change the variables in our deploy.rb script depending on who's computer we're deploying from.

How can we set the username and path/to/project depending on which computer we're on?

We're all running on osx.

Example

if osx logged in user == "jeff"
  set :user, "jeff's username'
else if ...
  set :user, 'blah'
end

How can i do this in rails/capistrano/osx?

The os/x shell sets an env variable $USER that has the logged in user's login name. You can reference that as ENV['USER'] in your cap files

The whoami command should be available on most Unix systems, so the following should also work on Linux, etc.:

if `whoami` == "jeff"
  set :user, "jeff's username'
elsif ...
  set :user, 'blah'
end

This is where environment variables come in handy.

set :user, ENV['USERNAME']

There are many ways to set this. You can do it directly in shell with

export USERNAME=blah

But for a cleaner, per-project solution I like the dotenv gem. With this enabled in :development group of your Gemfile , when Rails loads, it will export each line of a special .env file in the root of your application as environment variables.

USERNAME=blah

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM