Jenkins Kubernetes Plugin

With the Jenkins Kubernetes Plugin we can add a containerTemplate() for the database container we want to use.

containerTemplate

Add a containerTemplate entry for postgres like below:

containerTemplate(args: '-p 6432', name: 'postgres12', image: 'postgres:12',
  envVars: [containerEnvVar(key: 'POSTGRES_PASSWORD', value: 'admin')]
)

This will add a postgres container to the pod running with port 6432 with the password for the postgres user set to admin. This postgres instance will then be available on localhost:6432.

pg_isready

We add a code block that uses pg_isready to wait for postgres to be ready before we build and run tests.

container('postgres12') {
  print "wait for postgres ready"
  sh('pg_isready -t 60 -h localhost -p 6432')
  print "postgres ready now, continue"
}

When ebean-test starts it will make a JDBC connection using postgres:admin username and password and will use this to create the database, role and add extensions as needed.

Logs

In the logs before the tests actually run we see ebean-test connects to jdbc:postgresql://localhost:6432 and runs sql to create the database, role and extensions necessary to run the tests.

INFO  ?. io.ebean.EbeanVersion - ebean version: 12.3.6
INFO  ?. io.ebean.config.properties.LoadContext - loaded properties from [application.properties, application-test.yaml]
INFO  ?. io.ebean.test.config.platform.Config - Using jdbc settings - username:my_app url:jdbc:postgresql://localhost:6432/my_app driver:org.postgresql.Driver
TRACE ?. io.ebean.docker.commands.Commands - sqlRun: select 1 from pg_database where datname = 'my_app'
TRACE ?. io.ebean.docker.commands.Commands - sqlRun: select rolname from pg_roles where rolname = 'my_app'
DEBUG ?. io.ebean.docker.commands.Commands - sqlRun: create role my_app password 'test' login createrole
DEBUG ?. io.ebean.docker.commands.Commands - sqlRun: create database my_app with owner my_app
DEBUG ?. io.ebean.docker.commands.Commands - sqlRun: create extension if not exists hstore
DEBUG ?. io.ebean.docker.commands.Commands - sqlRun: create extension if not exists pgcrypto
DEBUG ?. io.ebean.docker.commands.Commands - Container ut_postgres ready with port 6432