Question

My Scala Dropwizard uses the SBT Start Script plugin (commit c1fefeb59f3d6ba053f99b8d49a52779ac3dbce8 at the time of writing) so I can stage a run script which can be easily run with foreman.

However, unlike HOCON, Dropwizard's configuration system allows you to override variables in the YAML file using -Ddw.xxx switches via JAVA_OPTS.

I would like to set the port.

Here-in lies my problem: in the latest version of SBT Start Script, the JAVA_OPTS is generated as part of the start script, so you can't pass it in via the Procfile anymore. So, the only way to get the Heroku-managed $PORT into my app is to set it via JAVA_OPTS as -Ddw.http.port=$PORT

How do I do this?

I've tried heroku config:set arguments on the command line:

  • JAVA_OPTS="-Ddw.http.port=$PORT" — this evaluates to $PORT in my environment, mostly "".
  • JAVA_OPTS="-Ddw.http.port=\$PORT"heroku config then shows this as JAVA_OPTS="-Ddw.http.port=$PORT", but the app gets given the literal $PORT as the port number, i.e. it doesn't get evaluated on Heroku.
  • JAVA_OPTS="-Ddw.http.port=\\\$PORT"heroku config then shows this as JAVA_OPTS="-Ddw.http.port=\$PORT", but the app gets given the literal \$PORT
  • JAVA_OPTS="-Ddw.http.port=$$PORT"$$ is the pid of the current shell (not subshell), so Heroku sets something like 12345PORT.

Please let me know how I can set the Heroku-managed port via JAVA_OPTS.

Was it helpful?

Solution

This may not address your question (I don't know Scala or JAVA_OPTS things) but you can use a .profile.d script to dynamically set an environment variable when your app boots. Here's an example where I set a DATABASE_URL environment variable using the Heroku API:

# .profile.d file
export DATABASE_URL=`curl -s -H "Authorization: Bearer $API_KEY" \
 -X GET https://api.heroku.com/apps/$SOURCE_APP/config-vars \
 -H "Accept: application/vnd.heroku+json; version=3" \
 | grep "DATABASE_URL" | cut -d '"' -f 4 `

This script relies on a valid Heroku API key stored in the applications environment variable API_KEY, as well as a SOURCE_APP environment variable, designating the source app (in your case, itself). In this example I'm checking for DATABASE_URL, but you could use what you want.

OTHER TIPS

@catsby is correct -- you'll want to use the Heroku Platform API to do this.

SIDENOTE: Heroku's platform API is actually incredibly awesome. You can use it to scale dynos, set variables, provision / deprovision addons, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top