Heroku.yml file. How to pass environment variables from .env file?

Viewed 1106

I have been looking for this solution for a while but the documentation about the heroku.yml file is quite limited at the moment.

I have this heroku.yml file:

build:
  docker:
    web: Dockerfile
  config:
    SECRET_KEY: django-secret-key

setup:

  addons:
  - plan: heroku-postgresql
    as: DATABASE
  config:
    DB_NAME: database-name

I have my environment variables in an .env file. I don't want anybody to have access to them. This .env file is added to .gitignore and .dockerignore files. The thing is, if I harcode them to the heroku.yml file they'll be accessible both from git and from the docker container

My question is the next, is there a way to pass these environment variables from the .env file to the heroku.yml file withouth hardcoding them?

2 Answers

You need to add an env_file: .env to setup.config in heroku.yml and then add your config vars to heroku in the settings

config-vars

Example of my heroku.yml

setup:
  config:
    env_file: .env # <-- important line
build:
  docker:
    web: Dockerfile
run:
  web: npm run start

This heroku.yml should work for you:

setup:
  addons:
  - plan: heroku-postgresql
    as: DATABASE
  config:
    env_file: .env
build:
  docker:
    web: Dockerfile

Your .env shouldn't be committed so Heroku won't have access to it. You'd usually set the env vars one-by-one either by hand or via the CLI. You can automate setting them from the .env file:

xargs -a .env -I {} heroku config:set {}
Related