Jon Rumsey

An online markdown blog and knowledge repository.


Project maintained by nojronatron Hosted on GitHub Pages — Theme by mattgraham

Lessons Learned Deploying Springboot to Heroku

Content

Set up apt with Heroku cli's latest deb:

# Run this from your terminal.
# The following will add our apt repository and install the CLI:
sudo add-apt-repository "deb https://cli-assets.heroku.com/branches/stable/apt ./"
curl -L https://cli-assets.heroku.com/apt/release.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install heroku

Change directory to your project root and commit any final changes.

Pull those changes to "main".

Login to Heroku (website) first, then login to Heroku CLI: heroku login

Update git remotes to include heroku fetch/push urls: git remote add heroku heroku_project_url.git

Push 'main' code to Heroku using git push heroku main.

Update 'system.properties' to run the app in Heroku by specification in article Specifying a Java version

System Properties Updates

Specify java runtime version: java.runtime.version=nn

Recommend not specifying java runtime to enable automatic security updates (otherwise dev must do it and re-push).

Gradle-specific Notes

Devcenter Heroku article Deploying Gradle Apps on Heroku

Heroku supports Java Apps using Gradle that contain the following:

TODO: Take an hour to go through this Gradle tutorial

Create a plain text file at the root of your Spring project named Procfile. without an extention and paste in the following code:

web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/demo-0.0.1-SNAPSHOT.jar

Commit these changes to the deployable branch. In Heroku-cli, launch the webapp with heroku open

GitHub Connect Method

  1. Create a new Github Repo.
  2. Build your code, make sure the website and db connection works.
  3. Login to Heroku (webapp) and create a new App.
  4. Select Deployment Method "GitHub: Connect to GitHub".
  5. If you have not connected Heroku to GitHub before you will be asked by GitHub to allow Heroku access to your Repos.
  6. Type the name of the repo (or at least part of it) and below the Search your matching repo(s) will appear.
  7. Click "Connect" next to the repo you want to connect this to.
  8. Assuming that succeeds (a Disconnect... buton appears) scroll down to "Automatic Deploys" section and make sure the correct branch is selected (usually main).
  9. Optional: Wait for CI to pass before deploy.
  10. Click Enable Automatic Deploys button. A green check-mark should appear next to "Automatic deploys from (branch) are enabled".

Notes

Instead of automatic deploys, you can configure Manual Deploy, it is just below Automatic Deploys subsection.

Setting the java compatibility in ...

TODO: Check out section 68.1 Build regarding https behind a proxy.

TODO: This other reference to Force the use of HTTPS

Heroku detects the Java app through presence of a 'pom.xml' file, but this applies to Maven not Gradle.

Heroku uses OpenJDKs Heroku (heroku-18 or heroku-20) or AzulZulu (heroku-22) which are certified Java SE specification compliant.

Postgres DB is automatically provisioned for Java apps that have a dependency on "Postgres JDBC driver" or "pgjdbc-ng driver" in pom.xml which will auto-populate env variable DATABASE_URL for you.

Thursday 16-Jun

stackfellows-wip Heroku Activity

Stuff I Did That Seemed To Make Things Work

  1. Add system.properties file to project root and added java.runtime.version=17 (solves "Invalid source release")
  2. Built app using gradle build and used find . -name "*.jar" to locate jar path (need to fix build cannot find JAR)
  3. Add Procfile (no period) to project root and added web: java -Dserver.port=$PORT -jar build/libs/app-0.0.1-SNAPSHOT.jar (as a result of gradle build output step above) (fixes build cannot find JAR error)
  4. Add postgres to Heroku via (web or) heroku-cli: heroku addons:create heroku-postgresql:hobby-dev (Fixes failure to connect to datasource error)
  5. Added SERVER_ERROR_WHITELABEL_ENABLED=false to Vars (fixes Whitelabel error messages)

Current Remaining Known Issue: Unable to add to database (Register new user).

Other VARs Changed

It is not clear to me whether/if any/all of these have made a difference:

Worth Noting

Once Heroku's postgres addon is included in the App config, the necessary VARs were added automatically by Heroku.

The automatic postgres VARs include the username and password to the postgres installation, and its path!

References

Spring Docs: System Environment Property Source

Heroku Dev Center: Config Vars

Tom Gregory dot Com: Get Going With Gradle Course (1 hour)

Stack Overflow: Error Connecting to PostgresQL with Spring

Spring dot IO guides: Gradle

Arose 13's Heroku Spring Gradle Example

Heroku Dev Central: Secure Java Web App for Production

Heroku Dev Center: Deploying Spring Boot Apps to Heroku

Heroku Dev Center: Proc File

Heroku Dev Center: Deploying Gradle Apps to Heroku

Heroku Dev Center: Default Web Process Type for Java

Heroku Postgres

Heroku Postgres Stuff

heroku pg:psql

Random Snippets here

Heroku DATABASE_URL: postgres://pqskdaargkkfri:16000d36cf0a21f22155a279dee5bfba4f9fc3004a0678229946d2ce99b195d4@ec2-52-73-184-24.compute-1.amazonaws.com:5432/d2kall4uhuucg2

Note: If only one DB exists then it will be connected by default.

Back to root Readme