ryanday
ryanday

Reputation: 2516

process.env does not contain config variables on Heroku

I have a few apps on Heroku. All of them use GruntJS to build assets and deploy to 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. When I use Heroku's toolbelt to view my setup, I see:

$ 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. When I console.log(process.env) from the Gruntfile, I see:

{ 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.

I've read that using Grunt in this manner isn't really the best idea, but it is what we have setup. Of course that could change if need be.

Any ideas? Is there anything I need to clarify?

Upvotes: 9

Views: 4820

Answers (2)

chenop
chenop

Reputation: 5143

2017 Update:

Its seems you should use the ENV_DIR argument.

According to doc:

ENV_DIR is a directory that contains a file for each of the application’s configuration variables. Config vars are made available as environment variables during execution of commands specified in the Procfile, as well as when running one-off processes.

Check the bin/compile section for further details

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.

Upvotes: 2

Nitzan Shaked
Nitzan Shaked

Reputation: 13598

Heroku does not expose the config variables to the build stage by default. It you want this, you'll have to enable the user-env-compile lab by issuing:

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

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

Upvotes: 8

Related Questions