Search code examples
gitlabgitlab-cigitlab-ci-runner

Gitlab CI can't connect to service / No HOST or PORT found


I am trying to run an integration test against my containers on gitlab.

To keep things as minimal as possible, here are the relevant parts of my .gitlab-ci.yml

image: ubuntu:latest

coverage:
    stage: test
    dependencies:
        - build
    services:
        - postgres:latest
        - registry.gitlab.com/username/project/image:latest

When I try and run the job, I get a container health check warning.

2019-06-06T02:13:34.508595817Z FATAL: No HOST or PORT found

Normally I would start my image with the standard docker run -p port:port image:version but I'm not sure how those options translate to gitlab services. How do I define the host and the port?


Solution

  • Below is an example pipeline connecting to postgres.

    The container aliases the service as the container name, unless you explicitly alias as shown here

    services:
    - name: postgres:9.4
    
    
    variables:
      # Configure postgres service (https://hub.docker.com/_/postgres/)
      POSTGRES_DB: $DB_NAME
      POSTGRES_USER: $DB_USER
      POSTGRES_PASSWORD: $DB_PASS
    
    cache:
      paths:
      - node_modules/*
    
    stages:
      - test
      - build
    
    
    db-test:
      stage: test
      image: ubuntu:latest
      tags:
        - consultancy
        - shared
      script:
        #set server connection env vars
      - export PGPASSWORD=$POSTGRES_PASSWORD
      - apt-get update -y && apt-get install postgresql postgresql-contrib -y
      - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;" #ensure the service is running
    
    node-test:
      stage: test
      image: node:latest
      tags:
      - consultancy
      - shared
      script:
        - npm install # Install Node dependencies
        - npm run test-unit # Execute unit testing suite
    #
    integration-tests:
      stage: test
      image: node:latest
      tags:
      - consultancy
      - shared
      script:
      - export PGUSER=$POSTGRES_USER
      - export PGHOST=postgres
      - export PGPASSWORD=$POSTGRES_PASSWORD
      - export PGDATABASE=postgres
      - export PGPORT=5432 #set the integration test env vars
      - npm install # Install Node dependencies
      - npm run test-integration # Execute integration testing suite