A typical web application will need services, such as databases and caches, to run. It will probably also need these services to run its test suite. In this blog post, we will look at how to set up and run PostgreSQL within Concourse CI prior to running our application's test suite.

The Problem

For a Ruby on Rails web app example, ideally you'd like a ci/scripts/tests.sh wrapper script that was simply:

Unfortunately we have not configured the application about its service dependencies (e.g. PostgreSQL). Nor does the container running this wrapper script actually contain a running PostgreSQL service. And that right there is the problem we will solve in this blog post.

The Solution

The following script fixes both issues - it will install & configure PostgreSQL, and it will configure the web app to use the local PostgreSQL service.

The Speed Up

This script takes about two minutes to run for an application with a small rails test suite. The breakdown of time:

installing and configuring PostgreSQL - 13 seconds

downloading and installing Ruby gems (bundle install) - 85 seconds

running PostgreSQL, creating databases and schemas - 8 seconds

running tests - 7 seconds

Since the version of PostgreSQL and most of the web application dependencies (RubyGems in the example above) are relatively static, you could gain a huge percentage speed improvement by pre-installing PostgresSQL and the RubyGems into the Docker image you are using.

In my pipeline, the test task dropped to 23 seconds instead of approximately 120 seconds.

Fortunately Concourse CI is also very good at creating and updating Docker images for use by other Concourse CI tasks. See our Concourse Tutorial lesson on Creating and Using Docker Images.