GitLab CI variables are not sourced from node.js testing

Viewed 273

I have this .gitlab-ci.yaml:

image: node:lts

services:
  - redis:alpine
  - postgres:latest

variables:
  REDIS_HOST: redis
  POSTGRES_HOST: postgres
  POSTGRES_DB: mydb
  POSTGRES_USER: myusr
  POSTGRES_PASSWORD: mypwd
  POSTGRES_HOST_AUTH_METHOD: trust

cache:
  paths:
    - "**/node_modules/"

node-test:
  stage: test
  script:
    - cd node-app/
    - echo "$POSTGRES_HOST" and "$POSTGRES_USER" and "$POSTGRES_PASSWORD" # postgres and myusr and mypwd
    - POSTGRES_HOST="$POSTGRES_HOST" npm run test # I thought I could pass variables here
  rules:
    - if: $CI_COMMIT_BRANCH

And this is knexfile.js:

const {
  POSTGRES_HOST = "localhost", // `localhost` for docker-compose on local machine
  POSTGRES_USER = "myusr",
  POSTGRES_PASSWORD = "mypwd",
  POSTGRES_DB = "mydb",
} = process.env;

module.exports = {
  development: {
    client: "pg",
    connection: {
      host: POSTGRES_HOST,
      user: POSTGRES_USER,
      password: POSTGRES_PASSWORD,
      database: POSTGRES_DB,
    },
    pool: {
      min: 2,
      max: 10,
    },
    debug: process.env.DEBUG,
  },

  production: {
    client: "pg",
    connection: {
      host: POSTGRES_HOST,
      user: POSTGRES_USER,
      password: POSTGRES_PASSWORD,
      database: POSTGRES_DB,
    },
    pool: {
      min: 2,
      max: 10,
    },
  },
};

But it just keeps connecting to 127.0.0.1:5432 and failed:

...

Using environment: development
connect ECONNREFUSED 127.0.0.1:5432
Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16)

...

Also tried setting variables at job level, but it does not work either.

Please help me, what I am doing wrong?

0 Answers
Related