Qasim
Qasim

Reputation: 97

Deploying Spring boot App, to Heroku With Docker using github

I have a Spring-Boot App, that I am hosting on Heroku and is soon going on production. Currently I am using free tier version of Heroku, but will buy standard paid tier before production.

The problem I am facing is that, when packaged with all dependencies the jar size of my App exceeds 200+M.

I have tried two deployment ways (both are functional right now)


1) Using Heroku maven plugin


2) Deploying from Git hub, along with Git-LFS (to host the jar file, as GitHub doesn't allow a file larger than 100mb) and the jar is needed for Heroku.

My Procfile looks like this:

web: java -jar target/x-x-1.1.1.jar

Note: GitHub-LFS free tier also has a limit of 1GB on upload/download per month, so I am right now sticking to Heroku maven build plugin tool (as I have also reached to Git-LFS limit).


In both ways I have to upload around 200+M even if there is a slight change in my source code as it repackaged and then deployed to Heroku.

I don't have much knowledge and expertise about docker, What am I looking right now is a way to deploy using docker and git hub to Heroku without uploading the packaged jar every time, Docker will build the jar file using maven before deployment!

So If some one knows about this or can point to the guide on how to achieve this, It will reduce my deployment time alot!

Spring-Boot version: 2.2.5

Maven-Build-Plugin: 3.1.0

Upvotes: 3

Views: 2207

Answers (1)

Beppe C
Beppe C

Reputation: 13933

Indeed using Docker should solve your problem as the slug limitation does not apply to the Heroku Container Registry. You can use GitHub actions to achieve this following the steps below.

Create Dockerfile

The Dockerfile is pretty straight-forward. It is important that you bind the PORT that Heroku assigns to you at runtime

FROM adoptopenjdk/openjdk11:latest

RUN mkdir -p /software

ADD target/x-x-1.1.1.jar /software/x-x-1.1.1.jar

CMD java -Dserver.port=$PORT $JAVA_OPTS -jar /software/x-x-1.1.1.jar

Configure GitHub Actions workflow

Create the GitHubActions workflow (/.github/workflows/main.yml) configuring the pipeline to build the application jar file and use the Heroku CLI to build/push the image into the Container Registry.
The workflow below is triggered at every push to the main branch (you can obviously change the trigger event if you like).

name: Build & Push to Heroku Docker Registry

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Git Checkout
      uses: actions/checkout@v1
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11
    - name: Build with Maven
      run: mvn -B package --file pom.xml
    - name: Heroku Container Registry login
    env: 
      HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
    run: heroku container:login 
    - name: Build and push
    env:
      HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
    run: heroku container:push -a ${{ secrets.HEROKU_APP_NAME }} web 
    - name: Release
    env:
      HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
    run: heroku container:release -a ${{ secrets.HEROKU_APP_NAME }} web 

Please notice the GitHub Actions secrets HEROKU_API_KEY and HEROKU_APP_NAME that you need to configure accordingly with the Heroku API Key and the name of your Heroku application.

I have captured all of the above and provided a simple GitHub repo here

Upvotes: 3

Related Questions