Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set host name as an environment variable in Heroku review app

Tags:

I'm using the Review Apps feature integrated with Github on Heroku. In one of my apps, I set an environment variable called HOST_NAME . For example, if the site is http://www.purplebinder.com, then HOST_NAME would be set to www.purplebinder.com. It's used in a couple of places where we work with cookies and in our transactional emails.

When I open up a new pull request and spin up a review app, HOST_NAME should be something like purplebinder-pr-27.herokuapp.com.

Is there a way to set this value automatically? The Heroku documentation on review apps says an env var can inherit a value from the parent app or be hardcoded in app.json. Neither of those approaches work here, because the value needs to be different each time, and also different from the parent app.

Heroku also says an env var can be set "through a generator", but doesn't go into detail about what that is.

This question might be a duplicate of Setting ROOT_URL for Review Apps, but nobody answered that one. It's also similar to How to get Heroku app name from inside the app, but the answers there involved running a script after the app was created - here I'd like to set this value as part of the initial build.

like image 979
declan Avatar asked Jun 28 '16 22:06

declan


People also ask

What is Heroku_app_name?

HEROKU_APP_NAME : The name of the review app. HEROKU_BRANCH : The name of the remote branch the review app is tracking. HEROKU_PR_NUMBER : The GitHub Pull Request number if the review app is created automatically.

Do you need a .ENV file with heroku?

You don't need ,env file for heroku. Config variables is for that.

Is heroku config vars safe?

Heroku config vars are designed to be safe for storing sensitive information. All config vars are stored in an encrypted form and safely stored. These are only decrypted and loaded when booting your app in a dyno itself.

What is config ENV?

You configure your environment by setting environment variables and creating or modifying files that relate to the environment variables. You can control whether environment variables are set at the environment level, for a specific user, or for a database session.


2 Answers

From https://devcenter.heroku.com/articles/github-integration-review-apps#heroku_app_name-and-heroku_parent_app_name:

To help with scripting, two special config vars are available to review apps. If you specify HEROKU_APP_NAME or HEROKU_PARENT_APP_NAME as required or optional config vars in your app.json file, Heroku will set those config vars to the new application name and the parent application name respectively. They will then be available for use in the postdeploy script so that you can do more advanced bootstrapping and configuration.

Here is an example app.json file that uses HEROKU_APP_NAME and HEROKU_PARENT_APP_NAME:

{     "name":"Advanced App",     "scripts": {         "postdeploy": "rake db:setup && bin/bootstrap"     },     "env": {         "HEROKU_APP_NAME": {             "required": true         },         "HEROKU_PARENT_APP_NAME": {             "required": true         }     } } 
like image 81
user94559 Avatar answered Sep 20 '22 07:09

user94559


If you add the heroku-buildpack-cli to your parent app, then it enables you to set environment variables from your post-deploy script. The command should look something like the following:

heroku config:set HOST_NAME=${HEROKU_APP_NAME}.herokuapp.com --app ${HEROKU_APP_NAME} 
like image 44
tfm92 Avatar answered Sep 23 '22 07:09

tfm92