Services Basic Question

Hi,

Apologies for the basic question, I’m pretty new to Drone.

Regarding the services section, I understand we can start a container running a DB (MySQL, PostgreSQL) as part of the build process.

How does Drone perform the following:

  • Ensure my test can connect to the database container hostname --> If the hostname is set to ‘database’, do I need to write my test to connect to this specific hostname ?

  • Load the database schema with some sample data --> Do I need to pass additional arguments to the services section so the database is loaded correctly with some data to test ?

Thanks & Regards

Yes exactly. The below example demonstrates connecting to the service using the container name as the hostname. In terms of your unit tests, I recommend the best practice of reading the database address from environment variable, instead of hard-coding. This will make your tests more portable, and make it easier to run them on Drone

pipeline:
  ping:
    image: postgres
    commands:
    - sleep 30
    - psql -U root -d test -h tcp://database:5432

services:
  database:
    image: postgres
    environment:
    - POSTGRES_USER=postgres
    - POSTGRES_DB=test

If you are loading your database schema from a file in your repository, you can typically create a pipeline step that is responsible for this task. For example:

pipeline:
  schema:
    image: postgres
    commands:
    - sleep 30
    - psql -U root -d test -h tcp://database:5432 -f schema.sql
  test:
    image: node
    commands:
      - npm install
      - npm test

services:
  database:
    image: postgres
    environment:
    - POSTGRES_USER=postgres
    - POSTGRES_DB=test

Thanks for your reply, much appreciated.