We use different Continuous Integration tools in our projects. One of them is TeamCity software. A pipeline for TeamCity can be configured easily and has two steps, such as run tests and build a docker image for further deployment. However, I needed to run Postgres before running tests. I made a research, I read the documentation and this article may be useful to close a gap for team city’s documentation.
The documentation says that you can enable it using a docker-compose plugin. Okay, let’s configure it.
Go to Build settings -> Build steps -> Add build step
Seems pretty easy, huh? Yes, but I found the following problems:
It’s unclear how to be sure that Postgres is up and running. How to configure credentials to connect to the Postgres database.
The documentation says that we can use HEALTHCHECK configuration settings. Full configuration in docker-compose listed below:
version: '3'
services:
postgres:
image: postgres:10.6
environment:
- POSTGRES_DB=test
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=test
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready", "-U", "postgres"]
interval: 5s
timeout: 5s
retries: 5
With such configuration, TeamCity waits when a Postgres container will be ready to accept new connections and adds created network to TEAMCITY_DOCKER_NETWORK environment variable. NB! We should to work with our dependencies like we were inside docker-compose. E.g in our case we need to specify host: postgresand port: 5432 to connect to our database
As I said before, remember to choose the right hostname to connect to the database. I wasted about one hour to realize it.
To configure it, go to Build settings -> Parameters -> Add new parameter
I use PG_HOSTNAME and PG_PORT environment variables for Postgres connection configuration and I set them in the Parameters section
I hope this article will help you to use docker-compose as a build step inside TeamCity. You can run more compose dependencies inside TeamCity and use them in your tests. If you have any questions feel free to ask them.
Previously published at https://maddevs.io/blog/running-postgres-as-a-build-requirement-in-teamcity-build/