Gitlab CI/CD pipeline tests fails to connect to MongoDB Atlas even though my local machine testing does

When running NestJS tests in my local machine all tests runs smoothly and no error arises. However, when my pipeline runs tests on Gitlab, it fails to connect to the MongoDB Atlas giving the following error. ERROR [MongooseModule] Unable to connect to the database. Same happens when simply trying to run the server.

From doing my own tests on the pipeline I have noticed that environment variables are indeed recognized by the pipeline environment and that my MongoDB atlas is also allowing all IP addresses to access it, so I have no idea what to do, any help would be greatly appreciated.

Hi @Eric_McEvoy,

Welcome to the community! Normally when I’ve seen something this in general it tends to be something with the connection string. Perhaps you can include more information here - such as how you’ve input the connection string in the env var, with sensitive information redacted, to see if we can notice something that might be remiss?

Best,
Melissa

2 Likes

Hello @Melissa_Plunkett, yes my connection string (which can be seen below) works locally but not when on gitlab, the connection string also works when the app is deployed on another third party service.

mongodb+srv://username:password@cluster0.utenxun.mongodb.net/test

In my gitlab env file and local env file this var is referenced as:
MONGODB_URI

I have also tried plugging in the username and password as env vars within the connection string individually but that also did not work.

If anyone could still help me with this, it would be greatly appreciated.

Hi @Eric_McEvoy,

I’ve asked around to see if I can find anyone who has hands on experience with this specific setup but no luck yet. Could you perhaps share more specifics/details on how you’ve set this up locally vs GitLab? That might help me either figure this out with you or help find someone who’s better able to see what the issue is between local and GitLab.

Thanks!
Melissa

Start with this, and modify accordingly if you have Mongoose installed.

Hello again @Melissa_Plunkett and thanks for the reply.

Not entirely sure what you mean by this question, but on my gitlab pipline I use the latest node image (image: node:latest) and my scripts below is what my current YML file looks like. All .env variables are recognized by the pipeline as they print to the terminal when i console.log() them.

backend_build:
  stage: backend-build
  image: node:latest
  script:
    - cd ./src/nestjs-backend
    - npm ci
    - npm run lint
    - npm run build
  artifacts:
    paths:
      - ./src/nestjs-backend/dist/
  cache:
    key:
      files:
        - ./src/nestjs-backend/package-lock.json
    paths:
      - ./src/nestjs-backend/node_modules/
    policy: pull-push
  only:
    - backend
    - master

backend_test:
  stage: backend-test
  image: node:latest
  services: [mongo]
  cache:
    - key:
        files:
          - ./src/nestjs-backend/package-lock.json
      paths:
        - ./src/nestjs-backend/node_modules/
      policy: pull
  before_script:
    - cd ./src/nestjs-backend
    - npm run test

  only:
    - backend
    - master

backend_deploy:
  stage: backend-deploy
  image: node:latest
  script:
    - cd ./src/nestjs-backend
    - echo $RAILWAY_PROJECT_ID
    - npm i -g @railway/cli
    - railway link --environment $RAILWAY_ENVIRONMENT_NAME $RAILWAY_PROJECT_ID
    - railway service $RAILWAY_SERVICE_NAME
    - railway up -d
  artifacts:
    when: always
    paths:
      - ./src/nestjs-backend/dist/
    expire_in: 10 min
  cache:
    key:
      files:
        - ./src/nestjs-backend/package-lock.json
    paths:
      - ./src/nestjs-backend/node_modules/
    policy: pull-push
  only:
    - backend
    - master

It works locally and when deployed on railway just not on the gitlab pipeline.
And more context on how I run it locally, I just npm run start/test on my personal desktop on vs code and it works fine.