简体   繁体   English

在rake任务中获取服务器url

[英]get server url in rake task

Is there any way I can get the url of the application inside a rake task? 有什么办法可以在rake任务中获取应用程序的URL? I'm running heroku, so maybe there is a way I can get hold of the appname? 我正在运行heroku,所以也许有一种方法可以让我掌握应用名称?

I was able to do the following in my Rakefile: 我可以在Rakefile中执行以下操作:

task(:foo => :environment) do
  puts ENV['APP_NAME']
end

and it successfully returned Heroku's application name, my_foo_app. 它成功返回了Heroku的应用程序名称my_foo_app。 when I ran the command: 当我运行命令时:

heroku rake foo --app my_foo_app

You can just set a config variable with the app URL using 您可以使用以下方法通过应用网址设置配置变量

heroku config:add APP_URL=http://myapp.com

Then you can get the value inside your rake task (or anywhere in your app) using ENV['APP_URL'] 然后,您可以使用ENV['APP_URL']在rake任务(或应用程序中的任何位置)中获取值

Documentation: http://docs.heroku.com/config-vars 文档: http : //docs.heroku.com/config-vars

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

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