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.

Image.

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 the 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.

Custom Software and Mobile App Development.

Latest articles here

Bishkek City Map with Open Source Geocoder.

Ariadna ā€” Open Source Geocoder for Transportation Apps

The process of creating a geocoder based on ElasticSearch, which searches for coordinates by synonyms and names of places, looks for crossroads and...

Protocol Buffers vs JSON.

Protocol Buffers vs JSON

Have you ever wondered how your favorite mobile app delivers lightning-fast updates or how social media platforms handle millions of messages per...

Concept of Unit-Tests Writing in Golang.

Concept of Unit-Tests Writing in Golang

Unit Tests in most cases are associated with a lot of wasted time.Ā A test is an imitation of a user browsing the page.Ā Finding errors during the...

Go to blog