Github Actions: Spring Boot Postgres Gradle

Goal : Run ./gradlew test after each push to master

Setup : Fairly simple Spring boot setup with Postgres Database

Issue: Spring can’t connect to DB

See this commit to see the last effort I made to get github actions working.

The following code is primarly based on this github action.

Test.yml

name: Gradle Build

on:
  push:
    branches:
      - master
  pull_request:

jobs:
  vm-jobs:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:12.2
        env:
          POSTGRES_USER: user
          POSTGRES_PASSWORD: pass
          POSTGRES_DB: spring-jwt
        ports:
          # will assign a random free host port
          - 5432/tcp
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - uses: actions/checkout@v1

      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8


      - name: Run tests
        run: ./gradlew test

application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/spring-jwt
spring.jpa.generate-ddl=true
spring.datasource.username=user
spring.datasource.password=pass

Errors I am seeing:

AuthApplicationTests > Successfull login FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1796
            Caused by: org.hibernate.service.spi.ServiceException at AbstractServiceRegistryImpl.java:275
                Caused by: org.hibernate.HibernateException at DialectFactoryImpl.java:100

I am 99% sure these errors arre because Spring Boot can find the database.

Any pointers would be great!

-Daniel

1 Like

I fixed it. See this commit for the first passing test. To be honest I don’t really know what fixed it (it was a combination of things). If you somehow stumble upon this I would recommend first learning how to use docker and docker compose. There are way more tutorials on that topic.

@zfz7 ,

Glad to hear that the problem has been solved.

If you have any other question about this ticket, feel free to tell us. Thanks.