Job services cannot communicate with one another (DB not connectable...)

Job services cannot communicate with one another (DB not connectable…)

I am working on a Spring Boot + Next.js project. I managed to correctly set up pipelines for building both backend and frontend, and testing the backend. I am trying to create a test job for the frontend as well, which includes integration testing. The frontend needs a connection to the backend, and the backend needs a connection to postgres. However, no matter what I try, I just can’t manage to set this up correctly.

The exception PSQLException: The connection attempt failed. gets always thrown when preparing the backend service.

Steps to reproduce

Tried to add two services, one for postgres and one for the backend image. These two services is what is causing me issues.

The test job itself works: the npx cypress run part in the configuration below runs correctly. It just won’t retrieve data from the backend, because it fails to get instantiated correctly.

Configuration

test-frontend-job:
  stage: test
  image:
    name: cypress/included:latest
    entrypoint: [""] # this is necessary to have the cypress image working correctly
  services:
    - name: postgres:latest
      variables:
        POSTGRES_DB: my_db
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: password
    - name: $CI_REGISTRY/backend:latest # Use the backend image as a service
      alias: backend
  script:
    # we are inside the next.js project
    - apt-get update # Updating system dependencies in a Docker image
    - npm ci # Install node modules (clean installation)
    - npm run build && npm start & # Build the app and start it in the background
    - npx wait-on http://localhost:3000 # Wait for frontend to start
    - npx cypress run # Run Cypress tests

I also made sure that the backend (Spring boot) uses url=jdbc:postgresql://postgres:5432/my_db instead of url=jdbc:postgresql://localhost:5432/my_db (both for liquibase and datasource).

When the test job runs, I can see:

Starting service postgres:latest ...
Pulling docker image postgres:latest ...
Using docker image sha256:ABC for postgres:latest with digest postgres@sha256:XYZ ...

And a few moments later I always get the following backend error:

ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: liquibase.exception.DatabaseException: org.postgresql.util.PSQLException: The connection attempt failed.

I have been stuck for several days now. I would be glad if someone could help me troubleshoot what the issue is.

Also, would you set up this integration test job differently?

Versions

Please select whether options apply, and add the version information.

  • Self-managed
  • GitLab.com SaaS
  • Self-hosted Runners

Versions

  • GitLab: v17.3.3-ee

Not sure but PostgreSQL typically requires additional auth settings specified in pg_hba.conf. Since you cannot configure this directly, a variable setting in CI could help. The examples in Using PostgreSQL | GitLab use POSTGRES_HOST_AUTH_METHOD: trust.

At the bottom of the docs, there’s an example project showing different auth methods: .gitlab-ci.yml · master · GitLab-examples / postgres · GitLab