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?
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
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.
Related
I need to update a domain name in the static file when my app promoted to production. I've added this line to my Procfile:
release: ./update-domain.sh
Here is the content:
if [ -n "$DOMAIN" ]; then
sed -i 's#$DOMAIN#'"$DOMAIN"'#' ./assets/config.json
fi
But it seems like Heroku doesn't have access to the env variables from bash. Is that true?
I'm not sure if that is related, but in Heroku Exec documentation I found this:
The SSH session created by Heroku Exec will not have the config vars set as environment variables (i.e., env in a session will not list config vars set by heroku config:set).
Thank you.
I'm trying to run some commands on my NodeJS app that need to be run via SSH (Sequelize seeding for instance), however when I do so, I noticed that the expected env vars were missing.
If I run eb printenv on my local machine I see the expected environment variables that were set in my EB Dashboard
If I SSH in, and run printenv, all of those variables I expect are missing.
So what happens, is when I run my seeds, I get an error:
node_modules/.bin/sequelize db:seed:all
ERROR: connect ECONNREFUSED 127.0.0.1:3306
I noticed that the port was wrong, it should be 5432. I checked to see if my environment variables were set with printenv and they are not there. This leads me to suspect that the proper env variables are not loaded in my ssh session, and NodeJS is falling back to the default values I provided in my config.
I found some solutions online, like running the following to load the env variables:
/opt/python/current/env
But the python directory doesn't exist. All that's inside /opt/ is elasticbeanstalk and aws directories.
I had some success so I could at least see that the env variables exist somewhere on the server by running the following:
sudo /opt/elasticbeanstalk/bin/get-config environment --output YAML
But simply running this command does not fix the problem. All it does is output the expected env variables to the screen. That's something! At least I know they are definitely there! But the env variables are still not there when I run printenv
So how do I fix this problem? Sequelize and NodeJS are clearly not seeing the env variables either, and it's trying to use the fallback default values that are set in my config file.
I know my answer is late, but I had the same problem and after some attempts with bash script I found a way to store it in your env vars.
you can simply run the following command:
export env=`/opt/elasticbeanstalk/bin/get-config environment -k <your-variable-name>`
now you will be able to easily access this variable:
echo $your-variable-name
afterward, you can utilize the env var to do what ever you like. in my case, I use it to decide which version of my code to build in a file called build-script.sh and its content is as follows:
# get env variable to know in which environment this code is running in
export env=`/opt/elasticbeanstalk/bin/get-config environment -k environment`
# building the code based on the current environment
if [ $env = "production" ]
then
echo "building for production"
npm --prefix /var/app/current run build-prod
else
echo "building for non production"
npm --prefix /var/app/current run build-prod
fi
hope this helps anyone facing the same issue 🤟🏻
I have an application in node.js which depends on env variables. I made some changes in code and now one of this vars should be changed after deploy. I don't want to do this manually. What is the best practise to do this automatically.
I guess that running some script after deploy could be solution, but I want to run this script only once (with this one particular change).
My only idea is that I should have script that will be checking (after each deploy) some directory if there is another script to run and then run it and remove it. But how can I achieve that?
The best way to approach this is to use the Heroku Toolbelt to set your environment variables as described here:
heroku config:set GITHUB_USERNAME=joesmith
You can then refer to these variables in your Node.js application by using the following syntax:
var dbUsername = process.env.DB_USERNAME;
Assuming you set a DB_USERNAME variable like this:
heroku config:set DB_USERNAME=myAppUserName
I like to ensure there's a fallback if the environment variable is not set, you can achieve that like this:
var dbUsername = process.env.DB_USERNAME || 'fallbackUsername';
// The string after || will be used if the process.env.DB_USERNAME variable is undefined (not set)
I'm using NPM's scripts.postinstall to build our application using GulpJS when we push it to Heroku:
"postinstall": "./node_modules/.bin/gulp build --require coffee-script/register"
I want to be able to set an environment variable at this time with the value of the commit hash. This will then be referenced in files as a cachebuster, e.g.:
<script type="text/javascript" src="/index.{{ process.env.commit }}.js"></script>
I can use git-rev to easily get this value asynchronously but I want it stored on an environment level. How can I set a custom Node environment variable?
If I'm going about this the wrong way and there's a better solution please let me know (I can only find methods to set NODE_ENV and not the process.env object itself).
You cannot do this via a postinstall script: the heroku command won't be available in the build environment. You can create your own lightweight deployment script that runs the command on your machine when a successful build occurs however:
#!/bin/sh
git push heroku master && heroku config:set COMMIT=$(git rev-parse --short HEAD)
To note, I got confirmation of this from Heroku support directly. This was their response:
There isn't an installation of heroku toolbelt on the slug compiler. If you need to set an environment variable as part of the push then you'll have to do it on your machine rather than the Heroku compiler.
Solution I've found is to use the heroku config system to set custom environment variables:
git = require "git-rev"
exec = require "exec-sync"
gulp.task "commit", ->
git.long (hash) ->
exec "heroku config:set COMMIT=#{ hash }"
Needs further testing but for now it works (also, because the command is run on the server there's no need to use heroku config:push).
You can now add this metadata with this labs feature from heroku !
heroku labs:enable runtime-dyno-metadata -a <app name>
Other than PORT, MONGO_URL, where can we find the documentation for all the environmental variables that's exposed by Meteor?
In the Meteor docs, I can only find this line
PORT=3000 MONGO_URL=mongodb://localhost:27017/myapp node bundle/main.js
Is there a variable that combines all the JS files and minifies them?
I've been using grasp to answer your question. Here is the command line I used:
grasp -reo --no-line-number --no-color --no-filename process.env.__ . 2> /dev/null | sort | uniq | cut -c 17-
And here is the result on the devel branch:
_
ADMIN_APP
APP_CONFIG
APPDATA
AUTOUPDATE_VERSION
BIND_IP
COMP_CWORD
COMP_LINE
COMP_POINT
COMPUTERNAME
ComSpec
DDP_DEFAULT_CONNECTION_URL
DEBUG
DEBUG_MIME
DEBUG_NOPT
DESTDIR
DISABLE_WEBSOCKETS
EDITOR
GALAXY
GALAXY_APP
GALAXY_JOB
HOME
HOSTNAME
http_proxy
HTTP_PROXY
https_proxy
HTTPS_PROXY
JOBS
LAST_START
MAIL_URL
MAKE
METEOR_SETTINGS
MONGO_URL
NODE_BINDINGS_ARROW
NODE_BINDINGS_COMPILED_DIR
NODE_DEBUG
NODE_ENV
NODE_NDEBUG
NOPT_DEBUG
npm_config_proxy
path
Path
PATH
PATHEXT
PORT
PREFIX
PROMPT
PS1
PWD
PYTHON
ROOT_URL
ROUTE
SERVER_ID
SHELL
SUDO_GID
SUDO_UID
SystemDrive
TEMP
TMP
TMPDIR
ULTRAWORLD_DDP_ENDPOINT
USE_JSESSIONID
USER
USERDOMAIN
USERNAME
USERPROFILE
VISUAL
windir
Most of thoses environment variables are not documented yet.
About minifing all js and css files, you can use the --production parameter:
meteor run --production
To get the documentation use the --help parameter:
meteor run --help
The easiest way to discover what exists "under the covers" of any open source project is to look at its source code. A simple grep "process.env" * at the top of the meteor source tree will show you every instance of Meteor's use of the process environment.
Alternately, you can visit Meteor's github and search for 'process.env'. Doing this should display thirty matches.