Set environment variables on heroku [duplicate] - heroku

This question already has answers here:
How to add .env file or otherwise set environment variables in a Heroku app?
(2 answers)
Closed 1 year ago.
I tried deploying one of my projects on heroku via my github repository. I had a .env file that had my API keys and private tokens. I want to make my repo public but it would mean revealing my .env file to everyone. I tried searching for how to set up .env for heroku but found answers that confused me even more. So is there a way to supply my env variables to herkou while I remove them from my github repository ?

Your .env keys are secret and should remain so. Fear not, you can make your repo public and still keep your keys secret.
Off the back, the .env file is a hidden file. You should ignore this file before pushing it to version control. Add a .gitignore file in your top-level directory and supply all the files you want ignored from version control. You can check here to see what files you may want to ignore.
If you had already committed your keys to github, meaning that there is an available copy of these keys online, i'd recommend that you generate new keys for each variable in your .env file. This would be the easiest way around it. With .env file already listed in .gitignore, then your next commit will omit the file and its content from version control.
How to load .env file
Assuming you have a simple structure as the one shown below where your .env and config.py files are in the top-level directory:
project
| --- .env
| --- config.py
| --- requirements.txt
| --- app/
First, you will need to install python-dotenv in your virtual environment. This package helps with the loading of environment variables.
(venv)$ pip3 install python-dotenv && pip3 freeze > requirements.txt
In your config module, load .env:
# config.py
import os
from dotenv import load_dotenv
load_dotenv('.env')
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY')
Your .env file should have this key:
# .env
SECRET_KEY=<you-will-never-guess>
Set environment variables in Heroku
In your terminal, you can set the values of .env:
(venv)$ heroku config:set SECRET_KEY=<you-will-never-guess>
To help interested users of your application know what keys they would need when they test the app locally, create a text file called .env-template and create your environment variables without passing their actual values:
# .env-template
SECRET_KEY=

Related

How to rename .env variables in package.json?

What I have
I have multiple projects using Percy for Cypress where I set the PERCY_TOKEN env variable inside the .env file. The token is different for each project. In the CI I set different env variables for each project, but locally I have to do it in the .env file. Because of this, I have to edit the .env file whenever I change between projects.
Goal
I would like to set them in the .env file this way:
PROJECT_A_PERCY_TOKEN=tokenhash1
PROJECT_B_PERCY_TOKEN=tokenhash2
So later I could rename these variables to PERCY_TOKEN, eliminating the need to constantly change the .env file.
What I tried
I'm trying to do this inside the package.json file's scripts property. Unfortunately echo $PROJECT_A_PERCY_TOKEN prints nothing. I know that I could create a shell/python/js script that parses the .env file, then passes the value back or calls npm run directly but I would like to do this without an external script.
Problem
It appears to me that I can't access the env variables inside package.json. Is there a way to rename the variable only using the npm script?
tl;dr
If the package you try to configure has the ability to do configuration via a JavaScript file, you can add the renaming at the beginning of it:
process.env.PERCY_TOKEN = process.env.CYPRESS_PERCY_SALESFORCE_TOKEN;
Explanation
While this isn't the solution I was looking for, it is a workaround for this specific use case. Percy supports JavaScript config files so I migrated my YAML config file, then I logged process.env and the .env file's variables were there, so I just need to copy the correct one. This might work for other packages that support JavaScript config files (or some alternative kind of hook/preloader where custom code can be placed), but if they don't, then the question is still unanswered.

How to avoid git sync of prisma .env files

When I set up prisma in my project, by following this article, I can set up .env and set database url and password.
After that,DB connection was established and some tables were migrated. But I am wondering that Are there any problem to add .env to .gitignore ?
password is hardcoded and this credentials are not pushed.
If someone has experienced this problems or know other options, will you please let me know.
It is strongly recommended that the env files are added to .gitignore.
Executing npx prisma init should create a .gitignore file and a .env file along with creating prisma folder. The default behaviour is that the env file gets added to the .gitignore file so that the credentials are not accidentally pushed to source control.
Here is a reference for using multiple env variables or using prisma without any env variables.
There should be no issues on adding .env to .gitignore file.

Creating environment variables with Rails 6 and Heroku

How do I create a group of environment variables that can be used both locally in development and on Heroku using Rails 6?
There are many different ways to configure environment variables, and people have many different preferences.
Personally, for my local development, I typically use the dotenv gem. I'll git-ignore .env, but I'll add a .env.example with all the vars I need stubbed out.
Then in my local checkout(s), I'll cp .env.example .env, and I will edit that .env file for all of my local configuration.
dotenv-rails includes a railtie to load environment variables from the .env file if they have not already been supplied as real env vars.
When I deploy to Heroku, I just use the Heroku console or GUI to set up my environment variables there.
Rails credentials work great and they don't require any extra gems and keep all your app secrets in one location.
EDITOR=vim rails credentials:edit
You can access any variable you set in this encrypted file by Rails.application.credentials.name_of_key. Typically, your .gitignore file will exclude the master.key file, so to make it accessible on a cloud provider, you'd provide the single key as an environment variable for decryption.

How to add .env file or otherwise set environment variables in a Heroku app?

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.

Set an environment variable in a Sinatra app?

I want to set MONGOHQ_URL in my sinatra app in order to be able to do this:
uri = URI.parse(ENV['MONGOHQ_URL'])
How do I setup the MONGOHQ_URL?
on Windows: set MONGOHQ_URL=test
on Unix (bash): export MONGOHQ_URL=test
on Unix (csh): setenv MONGOHQ_URL test
In order for your environment variables to always be available to your app, you will need to make sure they get exported whenever a new terminal session launches. It's common to put these in .bashrc for example
export MONGOHQ_URL=https://some.long.secure.url # for example
But for your local development purposes you might want to check out dotenv gem which allows you to store local environment variables in .env file in root of your project. For production, you should be able to Figaro with Sinatra, for more see answer to this question or see readme on the github repo
In general you should always make sure not to commit sensitive config information in your codebase so make sure to add any files like .env or config/application.yml to your .gitignore file.

Resources