GitLab CI with Postgres ERROR: connect ECONNREFUSED 127.0.0.1:5432

Viewed 1866

I have a Gitlab CI pipeline which tests, builds and deploys code. The code is basically a nodejs api and database is managed using Sequelize ORM. Recently, I tried the pipeline with a Postgres Database.

Here is the gitlab-ci.yml file :

image: trion/ng-cli-karma

variables:
  POSTGRES_DB: test
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: password
  NODE_ENV: test

cache:
  paths:
  - wiki-quotes-client/node_modules/
  - wiki-quotes-server/node_modules/

stages:
    - test
    - build
    - deploy

test_server:
  services:
   - postgres:latest
  script:
   - cd wiki-quotes-server
   - npm install
   - npm install -g sequelize-cli
   - sequelize db:migrate
   - sequelize db:seed:all
   - npm run test

test_client:
    script:
        -  cd wiki-quotes-client
        -  npm install
        -  ng test --watch=false

build_angular:
  only:
    - master
  stage: build
  script:
    - npm install
    - cd wiki-quotes-client
    - ng build --prod
  artifacts:
    paths:
      - wiki-quotes-client/dist/wiki-quotes-client/.

deploy:
  only:
    - master
  stage: deploy
  dependencies:
    - build_angular
  script:
    - ls -all
    - apt-get update -qq
    - apt-get install -qq git
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - ls ~/.ssh/
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\t StrictHostKeyChecking no \n\n" > ~/.ssh/config'
    - ssh-keyscan 159.65.156.240 >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - ssh goutambseervi@159.65.156.240 'cd ~/wikiquotesapp; git checkout master; git pull;  cd wiki-quotes-server; npm install; npm start:prod'

Now it shows me this error :

ERROR: connect ECONNREFUSED 127.0.0.1:5432

Also, additional question I had was...

What would be the ideal path to follow when I'm deploying a api with a database using Sequelize ?

I want to make sure that my migrations go live and at the same time my database should be seeded everytime I deploy and I don't want the database to have redundant data.

1 Answers

Using services in Gitlab CI works basically the same way as running different docker containers (named services in docker-compose) that are made available to your container running the specific job.

So according to your error message I assume your code tries to reach the postgres database on localhost which works when you run both on your developer machine. In Gitlab CI these are two containers that "have both their own localhost" and must use dns names like postgres to connect to another container. So as you are using an image called postgres, Gitlab also names the service like that.

Please try using the hostname postgres instead of localhost for your database connection in the node process and the access should work. According to your other variables defined, this may already work by just adding one more variable of POSTGRES_HOST: postgres (or similar - I'm not familiar with sequelize config)

For details on services in Gitlab CI please check the docs that even provide a sample for your specific use case of accessing a postgres service and some clarification on this specific question.

Related