Running Postgres as a build requirement in TeamCity build

Fixing gaps in TeamCity's documentation. How to make sure that Postgres is running and how to configure the credentials to connect to the database.

TeamCity + Docker Compose.

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.

How to run Postgres before running tests

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

Runner type: Docker compose
Step name: Choose your own name
Execute step: If all previous steps finished successfully
Docker-compose file: your-compose.yml

How to run Postgres before running tests.

Seems pretty easy, huh? Yes, but I found the following problems:

  1. It’s unclear how to be sure that Postgres is up and running.
  2. How to configure credentials to connect to the Postgres database.

How to be sure that Postgres is running

The documentation says that we can use HEALTHCHECK configuration settings. Full configuration in docker-compose listed below:

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

Configuring credentials to connect to Postgres 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

Environment Variables.

Conclusion

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.

Andrew Minkin.

Andrew Minkin

Chief technology officer
Contributors:
  • Darika Nurmamatova
  • Meder Akkozov
Custom Software and Mobile App Development.
Application Design Proccess.

Software architecture 101 - introduction to the world...

Software architecture 101 -...

Software architecture 101 - introduction to the world of application design

Hello, everyone!Today, I would like to introduce you to the world of application design.Designing applications can be called a multidisciplinary...

Dec 17, 2020
Mad Devs Internship Program Automation.

Mad Devs internship program automation

Mad Devs internship program automation

Mad Devs internship program automation

Mad Devs has an Internship program for different programming languages. The communication with interns takes place in Telegram messenger. 5...

Jul 17, 2019
Golang.

Underestimating Go

Underestimating Go

Underestimating Go

Hi, I’m Pavel!I’m a software engineer in Mad Devs. In the past six years, I’ve been working with Ruby and Rails, also I love React and Redux. I was a...

Aug 20, 2019