简体   繁体   English

Rails capistrano根据用户登录到计算机设置deploy.rb变量

[英]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. 我正在开发一个有多个开发人员的项目,目前我们必须根据我们部署的计算机来更改deploy.rb脚本中的变量。

How can we set the username and path/to/project depending on which computer we're on? 我们如何根据我们使用的计算机设置用户名和路径/项目?

We're all running on osx. 我们都在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? 我怎么能在rails / capistrano / osx中这样做?

The os/x shell sets an env variable $USER that has the logged in user's login name. os / x shell设置一个env变量$USER ,它具有登录用户的登录名。 You can reference that as ENV['USER'] in your cap files 您可以在cap文件中将其引用为ENV['USER']

The whoami command should be available on most Unix systems, so the following should also work on Linux, etc.: whoami命令应该可以在大多数Unix系统上使用,因此以下内容也适用于Linux等:

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 你可以直接用shell来做

export USERNAME=blah

But for a cleaner, per-project solution I like the dotenv gem. 但对于一个更清洁,每个项目的解决方案,我喜欢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. 启用此功能:development Gemfile :development组,当Rails加载时,它将导出应用程序根目录中的特殊.env文件的每一行作为环境变量。

USERNAME=blah

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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