Environment variable in app.json in Heroku not working

Viewed 579

I have an app.json that looks like this (roughly):

{
  "addons": [
    "heroku-postgresql"
  ],
  "env": {
    "RAILS_MASTER_KEY": {
      "description": "Key to decrypt Rails credentials.",
      "require": true,
      "generator": "secret"
    },
    "SAFE_EMAIL_DEST": {
      "description": "Where to send emails when in safe mode.",
      "required": false
    }
  },
  "environments": {
    "test": {
      "scripts": {
        "test": "bundle exec rake spec"
      }
    },
    "staging": {
      "env": {
        "SAFE_EMAIL_DEST": "staging@example.com"
      }
    },
    "production": {
    }
  }
}

but when I run the app, I get the error: RuntimeError (SAFE_EMAIL_DEST needs to be defined) meaning, that environment variable is not defined.

What am I missing? Why isn't it being defined? Is "staging" the correct environment name for an app in the staging state in a Heroku pipeline?

1 Answers

For environments key in app.json, only 2 are understood by the platform:

  • test: applied to continuous integration environments created by Heroku CI.
  • review: applied to Review Apps environments created by Heroku Review Apps.

so basically you need to set up directly in the env key your "SAFE_EMAIL_DEST" variable :

{
  "addons": [
    "heroku-postgresql"
  ],
  "env": {
    "RAILS_MASTER_KEY": {
      "description": "Key to decrypt Rails credentials.",
      "require": true,
      "generator": "secret"
    },
    "SAFE_EMAIL_DEST": {
      "description": "Where to send emails when in safe mode.",
      "value": "staging@example.com"
    }
  },
  "environments": {
    "test": {
      "scripts": {
        "test": "bundle exec rake spec"
      },
      "env": {
        "SAFE_EMAIL_DEST": "Your SAFE_EMAIL_DEST for continuous integration environments"
      }
    }
  }
}

source

Related