如何在Heroku应用中添加.env文件或设置环境变量? [英] How to add .env file or otherwise set environment variables in a Heroku app?

查看:288
本文介绍了如何在Heroku应用中添加.env文件或设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网络上尝试了许多不同的解决方案来解决这个问题,但都没有成功.

I've tried many different solutions on the web for this problem, but all have been unsuccessful.

这里是问题:我的应用程序需要知道它是在Heroku(生产模式)还是本地(开发模式)上运行.为此,我们要使用环境变量.我了解Heroku上的环境变量可以在.env文件中设置.所以我的尝试是运行heroku run bash -a <app-name>然后通过执行以下操作来安装vim:

Here's the problem: My app needs to know whether it is being run on Heroku (production mode) or locally (development mode). For this purpose, we want to use environment variables. I've understood that environment variables on Heroku can be set in a .env file. So my attempt was to run heroku run bash -a <app-name> and then to install vim by doing this:

mkdir ~/vim
cd ~/vim

# Staically linked vim version compiled from https://github.com/ericpruitt/static-vim
# Compiled on Jul 20 2017
curl 'https://s3.amazonaws.com/bengoa/vim-static.tar.gz' | tar -xz

export VIMRUNTIME="$HOME/vim/runtime"
export PATH="$HOME/vim:$PATH"
cd -

除了反复崩溃之外,当我登录和退出shell时,vim不再起作用:

Apart from crashing repeatedly, vim didn't work anymore when I logged in and out of the shell:

~ $ vim // in the heroku shell
vim: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory

我也尝试了heroku plugins:install heroku-vim,但是在那之后运行heroku vim只会导致很长的延迟,随后是正常的heroku shell打开,没有vim.

I also tried heroku plugins:install heroku-vim but running heroku vim after that only resulted in a long delay followed by the normal heroku shell opening, no vim.

我不太在乎是否有vim可以工作.我只想能够在Heroku上写一个名为.env的文件,以便可以在其中设置环境变量.

I don't really care if I get vim to work. I just want to be able to write in a file named .env on Heroku so I can set environment variables in it.

我该如何实现?

推荐答案

Heroku上不需要.env文件.实际上,自从开始以来,这样的文件就不能很好地工作了

There is no need for an .env file on Heroku. In fact, such a file won't work very well since

  • Heroku从您的Git存储库中获取所有文件,
  • 具有一个临时文件系统,这意味着更改为类似.env的文件将很快丢失,并且
  • 如果您缩放应用程序,.env文件将在其他测功机上不可用
  • Heroku gets all of its files from your Git repository,
  • has an ephemeral filesystem, meaning that changes to files like .env will be quickly lost, and
  • the .env file won't be available on other dynos if you scale your app

因此,在Heroku上创建.env文件不是一个好方法.

As such, creating an .env file on Heroku isn't a good approach.

相反,您可以使用对环境变量的内置支持, c9>或其Web UI.无论哪种方式,您都会获得一个常规的环境变量.

Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable.

这篇关于如何在Heroku应用中添加.env文件或设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆