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

查看:28
本文介绍了如何在 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.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

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 文件将无法在其他 dynos 上使用
  • 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.

相反,您可以使用其对环境变量的内置支持,使用<代码> Heroku 配置:设置 或其 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天全站免登陆