Heroku docs indicate that "On a traditional host or when working locally, you often set environment variables in your .bashrc file. On Heroku, you use config vars". On Dokku the process is analogous: https://dokku.com/docs/configuration/environment-variables/
With the "traditional" method I can take a .env file append it's contents to .bashrc over ssh with a single automated standard unix command.
The Heroku docs only describe how to set "config vars" one by one. It would take unacceptably long to do this.
On Heroku one could perhaps use the api. But Dokku does not have an API.
Question is similar to Setting Dokku environment variables
The accepted answer there speaks about a local CLI for dokku where you can run $ dokku config:set:file <path/to/.env>. I can't find documentation on this command with "standard" dokku. I don't need a local CLI I can ssh and scp to my server.
Question is similar to Bulk set the environment variables in Heroku pipeline except that I don't know what a Heroku pipeline is and I'm not using one (and the question has no answer).
Is there a mechanism short of creating a bash script or installing a local Dokku CLI for bulk updating config vars in Heroku based on a .env file with a long list of variables?
Sure, use heroku config:set ENV_ONE=value ENV_TWO=value ENV_THREE=value from the heroku CLI in your terminal.
Make sure it's all a single line, use spaces, not line breaks.
Related
How do I get HEROKU specific ENV variables? (Such as $HEROKU_API_KEY_ENC)
I'm writing a buildpack to perform some operations on app deploys (not dyno restart, just deploy) but I need to know which variables are available at that point.
I know I have access to my own environment variables and HEROKU runtime dyno metadata but I'd like to see what else is there, such as which git user is making the deploy, or whether there's a variable to determine it is a deploy or a restart etc.
I appreciate your help.
P.S I already tried deployhooks and it doesn't solve my problem.
The linux printenv command will display every available environment variable.
How about this
heroku config --app your_app_id | grep "your_variable"
I've tried many different solutions on the web for this problem, but all have been unsuccessful.
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 -
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
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.
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?
There is no need for an .env file on Heroku. In fact, such a file won't work very well since
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
As such, creating an .env file on Heroku isn't a good approach.
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.
It is fairly simple.
Just as you added them in your .env file, do the same with heroku's command line and you will see heroku restart and you are all set to fly again.
Just use the command :
(heroku config:set VARIABLE=this_is_the_value)
Remember to use the underscores in the value as spaces are not allowed not inverted quotes (" ")to turn it into a single string is permissible.
I'm currently writing a couple Twitter bots for my friends using the Twitter gem for Ruby. My plan was to store the keys for them in a .txt file with the rest of the bot's code on my server, but everything I've read has said the keys shouldn't be readable within the code. Is this secure enough, and if not what would be a good solution? Thanks!
A common approach is to save the environment variables into a file called .env that is ignored by version control (and therefore won't be included on Github) but read by the code. One gem to help with this is dotenv.
add .env to the .gitignore file.
create a local .env file with all your env vars
require 'dotenv' and put Dotenv.load somewhere at the beginning of your script. In Rails, the require is unnecessary and you can place the load call in any file in the config/initializers folder
Check that your app works fine locally. The environment variables should be found in the ENV hash from Ruby code.
Save changes and push new version of app to digital ocean
manually create the .env file on the digital ocean server, in the root of the repo
run digital ocean server and check that everything works.
other notes:
see How To Read and Set Environmental and Shell Variables on a Linux VPS
some platforms like heroku have a different mechanism for setting environment variables, such as heroku config:set or web UIs.
You can set environment variables on a one-off basis using the env command in bash, for example:
env a=hello b=' world' ruby -e 'puts ENV["a"] + ENV["b"]'
# => hello world
This can give a quick way to configure a program without getting into argument parsing. For example in Rails, you can say rails c test to open a console using the test environment, but env RAILS_ENV=test rails c should do the same thing.
Our team is using foreman for development and .env files to preassign development ports to each piece of a service oriented application. It dramatically simplifies things for this file to just live with the repository as we are not doing any specific per-machine local configurations even though multiple docs seem to think this is a bad idea.
Does anybody know if Heroku will ignore these .env files automatically? What if they were added to .slugignore?
I setup a test app to try this out including a PORT=5005 in the .env file and then committing/deploying to Heroku. Heroku didn't seem to notice it was even there and no new config vars appeared when I checked heroku config.
You answered your own question, but just for confirmation: .env is entirely a Foreman construct, while Foreman and Heroku will make use of Procfile.
We actually wanted to be able to ensure consistent environments between local and Heroku deployments, so I wrote a python script to export .env up to Heroku.
In case others want to export .env to Heroku:
https://github.com/FinalsClub/karmaworld/blob/68f0f0340d7b6420e263cab648ff7de1ea851a0e/export_env_to_heroku.py
I have a Play! application which is on Heroku.
My config file is different between my local application and the same on Heroku. Especially for the URL of my MongoDB base.
On localhost my base address is 127.0.0.1 and on heroku it's on MongoHQ. So when I push my application to Heroku I modify my config file.
But some times, like this morning Heroku change the config file. I pushed my application correctly configured on Heroku this morning and everything worked until now.
When I watch the logs I see that Heroku changed my config and try to connect to my local MongoDB base.
Is someone knowing what ? I hope I'm clear :)
Thanks everybody !
If there are differences in your application in different environments (e.g. local vs production), you should be using assigning the values with environment variables. For Play apps, you can use environment variables in your application.conf file, like this:
`mongo.url=${MONGO_URL}`
Then, on Heroku you can set the environment variables with config vars, like this (note, this may already be assigned for you by the add-on provider):
$ heroku config:add MONGO_URL=...
Locally, you can use Foreman to run your application with the environment variables stored in an .env file in your project root.