How to set Heroku config var with contents of a file
Asked Answered
U

3

30

To set config vars for a Heroku app, you do this:

$ heroku config:set GITHUB_USERNAME=joesmith

How would I set a config var with the contents of a file?

Untouched answered 15/9, 2013 at 18:5 Comment(0)
F
42

Take a look at the heroku-config plugin, which adds a heroku config:push command to push key-value pairs in a file named .env to the app.

It also has a heroku config:pull command to do the opposite and works very well with foreman for running the app locally with the config in .env.

https://github.com/xavdid/heroku-config

Example
heroku config:push --file=.env.production 
Fluoridation answered 16/9, 2013 at 8:26 Comment(1)
How to do it to config vars of pipeline's review apps?Fonda
M
0

I know this is too late but still it will be helpful for future users who land here.

I also wanted a quick solution to add variable to heroku app but copy-paste is so boring.. So wrote a script to read values from the .env file and set it at the requested app - all things passed as an option:

https://gist.github.com/md-farhan-memon/e90e30cc0d67d0a0cd7779d6adfe62d1

Usage

./bulk_add_heroku_config_variables.sh -f='/path/to/your/.environment/file' -s='bsc-server-name' -k='YOUR_CONFIG_KEY1 YOUR_CONFIG_KEY2'
Maddening answered 18/10, 2019 at 21:53 Comment(0)
H
0

A simple pure-python solution using honcho and invoke:

from honcho.environ import parse
from invoke import run

def push_env(file='.env'):
    """Push .env key/value pairs to heroku"""
    with open(file, 'r') as f:
        env = parse(f.read())
    cmd = 'heroku config:set ' + ' '.join(
        f'{key}={value}' for key, value in env.items())
    run(cmd)

The idea here is that you will get the same configuration as if you ran the project locally using honcho. Then I use invoke to run this task easily from the command line (using @task and c.run) but I've adapted it here to stand alone.

Heartbreak answered 11/8, 2020 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.