Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ENV var in Heroku preview app postdeploy script

I want to set the HOST env var to $HEROKU_APP_NAME.herokuapps.com on a preview app. It doesn't look like I can do this in app.json since this is a computed value.

I was hoping to do it in a "postdeploy" script like this

heroku config:set HOST="`heroku config:get HEROKU_APP_NAME -a neon-dev-pr-520`.herokuapps.com"

but it wants to authenticate me as a Heroku user. Alas, this doesn't work either:

export HOST=$HEROKU_APP_NAME.herokuapps.com

Any suggestions?

like image 572
Chris Beck Avatar asked Dec 05 '16 21:12

Chris Beck


People also ask

Does Heroku read .ENV file?

Heroku Local is a command-line tool to run Procfile-backed apps. It is installed automatically as part of the Heroku CLI. Heroku Local reads configuration variables from a . env file.

How do I access Heroku config vars in Python?

Heroku's environment variables are called config vars. To set a config var, navigate to the Settings tab of your app's dashboard, and click Reveal Config Vars.

Are Heroku config vars strings?

All heroku config vars will be environment variables, which, in linux environments, can only be strings. So all your variables will be strings once in your app.


1 Answers

Worked out with the assistance of Heroku's awesome support team a few years ago. We needed to set a reflexive environment variable for a middleware (parse-server) to know what to connect to. It's set manually on our Staging and Production apps, but to get it set on our review apps:

My app.json incldues:

  "scripts": {
    "postdeploy": "bin/bootstrap"
  },
...
  "env": {
    "HEROKU_APP_NAME": {
      "required": true
    },
    "HEROKU_PARENT_APP_NAME": {
      "required": true
    },
...

bin/bootstrap is:

#!/usr/bin/env bash

echo $HEROKU_APP_NAME
export SERVER_URL=https://$HEROKU_APP_NAME.herokuapp.com/parse

SERVER_URL is available (and correct) in my review apps. It's been 👍no problems since we implemented.

like image 97
woodardj Avatar answered Oct 04 '22 00:10

woodardj