Build Spring boot app with postgres on CircleCI ?

Amit Kumar
Xebia Engineering Blog
3 min readJun 3, 2020
Photo by Mpho Mojapelo on Unsplash

A few days back, a student from FS-102 batch in Xebia sought how to configure circleCI so that she can build her spring boot app with postgres database. Frankly, I did not do this kind of configuration earlier. So I decided to do it and write a blog so that anyone can get help from this blog.

So what we are going to do is that we will write a config.yml file to configure our spring boot app with circleCI with postgres database. You can create any spring boot app having postgres as a database or may use an existing spring boot app as well.

Prerequisites

  • You should know what a spring boot application is and how it is created.
  • You should know how to integrate your app with circleCI as I am just discussing a particular configuration over a default one. You can refer to this article if you have no idea.
  • Knowledge of basic aspects of the docker image is appreciated.

So by now, we have a spring boot app with postgres database and we are going to write a config.yml file. I will show you how the config.yml looks like and then we will discuss bit by bit.

The below image is of a config.yml file.

Now you have seen the file. Let us understand what we have written over there.

Starting with jobs it indicates that there can be many jobs under this section. But I have defined only one job named build.

After this you can see docker images, the first image is the primary image and all commands run in the primary image. Here the first image is openjdk:8 and it is required as our app is a spring boot application using java as a language. Under the first image, there is an environment of localhost(127.0.0.1). After the first image there is a second image of postgres which is a secondary image, also called service image in circleCI terms which we need as our application has it as a db.

Now moving on to the steps part, from here the real game begins. After setting images and environments, the first thing we need to do is get source code from github which is done by checkout keyword.

The run key is used to invoke all command-line programs. After checkout, I have made some optimization so that there is no need to download maven dependencies every time source code is checked-out. You can see there is restore_cache and save_cache that are doing that task.

There is an installation of dockerize which ensures that after the database gets ready only then our app will be able to make connections to it, saving us from unpredictable failures.

After that, I have installed postgres client and then create a database named YOUR_DB_NAME which surely you can replace with your db name and the last step is to run maven clean install that is what my aim is, you are free to edit it run what maven command you want to run.

That is it, this is the way we generally write config.yml for spring boot app with postgres db.

One last thing your application.properties should have these configurations as well. By default, the username is postgres and the password is blank for postgres docker image.

spring.datasource.url= jdbc:postgresql://localhost:5432/YOUR_DB_NAME
spring.datasource.username=postgres
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

--

--

Amit Kumar
Xebia Engineering Blog

Spring, Spring boot, Java developer, code quality, learning , writing and sharing.