简体   繁体   English

process.env在Heroku上不包含配置变量

[英]process.env does not contain config variables on Heroku

I have a few apps on Heroku. 我在Heroku上有一些应用程序。 All of them use GruntJS to build assets and deploy to S3. 所有这些都使用GruntJS来构建资产并部署到S3。 One app has been working fine for quite some time. 一个应用程序已经工作了很长一段时间。

The other apps have a problem where I can not read my config variables from the Gruntfile. 其他应用程序有一个问题,我无法从Gruntfile读取我的配置变量。 When I use Heroku's toolbelt to view my setup, I see: 当我使用Heroku的工具带查看我的设置时,我看到:

$ heroku config --app mydevapp --account personal
=== mydevapp Config Vars
AWS_ACCESS_KEY:         #########     
AWS_BUCKET:             #########
AWS_SECRET_KEY:         #########
BUILDPACK_URL:          https://github.com/ddollar/heroku-buildpack-multi.git
DATABASE_URL:           #########
PAPERTRAIL_API_TOKEN:   #########
TEST:                   test

Which is great. 哪个好。 However none of these variables are available to me from Grunt. 但是Grunt没有这些变量可供我使用。 When I console.log(process.env) from the Gruntfile, I see: 当我从Gruntfile中调用console.log(process.env)时,我看到:

{ GEM_HOME: '/tmp/build_e26d1d60-d447-40d8-b09b-02d3758a6027/.gem/ruby/1.9.1',
  SHELL: '/bin/bash',
  SSH_CLIENT: '10.207.46.127 55868 50678',
  GROUP: 'production',
  DEPLOY: 'production',
  STACK: 'cedar',
  SHLVL: '3',
  HOME: '/app',
  CPPPATH: '/tmp/node-node-hP8q/include',
  _: '/tmp/build_e26d1d60-d447-40d8-b09b-02d3758a6027/node_modules/grunt-cli/bin/grunt' 
}

There are some other vars in there, but I'm not sure what is safe to show. 那里还有一些其他的变种,但我不确定什么是安全的。 I don't see ANY of my config vars listed. 我没有看到列出的任何配置变量。

I have no idea what the difference is between my working app, and the two apps that don't have config vars in the process.env variable. 我不知道我的工作应用程序与process.env变量中没有配置变量的两个应用程序之间有什么区别。

I've read that using Grunt in this manner isn't really the best idea, but it is what we have setup. 我已经读到以这种方式使用Grunt并不是最好的主意,但它是我们设置的。 Of course that could change if need be. 当然,如果需要可能会改变。

Any ideas? 有任何想法吗? Is there anything I need to clarify? 有什么我需要澄清的吗?

Heroku does not expose the config variables to the build stage by default. 默认情况下,Heroku不会将配置变量公开给构建阶段。 It you want this, you'll have to enable the user-env-compile lab by issuing: 你想要这个,你必须通过发出以下命令来启用user-env-compile实验:

heroku labs:enable user-env-compile -a myapp

Docs: https://devcenter.heroku.com/articles/labs-user-env-compile 文档: https//devcenter.heroku.com/articles/labs-user-env-compile

2017 Update: 2017年更新:

Its seems you should use the ENV_DIR argument. 它似乎应该使用ENV_DIR参数。

According to doc: 根据文件:

ENV_DIR is a directory that contains a file for each of the application's configuration variables. ENV_DIR是一个目录,其中包含每个应用程序配置变量的文件。 Config vars are made available as environment variables during execution of commands specified in the Procfile, as well as when running one-off processes. 在执行Procfile中指定的命令期间以及运行一次性进程时,Config变量可用作环境变量。

Check the bin/compile section for further details 检查bin / compile部分以获取更多详细信息

Here is a snippet to extract the config vars in the build phase: 以下是在构建阶段提取配置变量的代码段:

export_env_dir() {
  env_dir=$1
  whitelist_regex=${2:-''}
  blacklist_regex=${3:-'^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH)$'}
  if [ -d "$env_dir" ]; then
    for e in $(ls $env_dir); do
      echo "$e" | grep -E "$whitelist_regex" | grep -qvE "$blacklist_regex" &&
      export "$e=$(cat $env_dir/$e)"
      :
    done
  fi
}

For me its seems an overkill - happy to hear your thoughts. 对我来说,这似乎是一种矫枉过正 - 很高兴听到你的想法。

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

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