Skip to main content
In this guide, we’ll configure GitLab CI/CD for the Solar System NodeJS project and add a robust unit testing job. You’ll learn how to create a structured .gitlab-ci.yml, manage sensitive MongoDB credentials via CI/CD variables, and ensure your NodeJS tests run reliably.

1. Initialize GitLab CI/CD Configuration

Start by creating a new file named .gitlab-ci.yml at your repository root. Define the pipeline stages and add a placeholder build job:
Explicit stages improve readability and make it easier to extend the pipeline with additional jobs.

2. Define Workflow Rules

Specify when the pipeline should trigger by adding a workflow section with conditional rules:

3. Add the Unit Testing Job

Next, define the unit_testing job in the test stage. This job uses the official NodeJS Docker image, installs dependencies, and runs tests with Mocha:
Commit these changes to a new feature branch:
Pushing to feature/* will trigger a pipeline run.
The image shows a GitLab CI/CD pipeline interface for a project named "Solar System NodeJS Pipeline," indicating a failed unit testing job. The sidebar includes options for managing the project, such as issues, merge requests, and pipelines.

4. Diagnose the Test Failure

Inspect the job logs. The runner pulls the NodeJS image and installs packages, but npm test fails with a Mongoose connection error. Our application code in app.js relies on environment variables:
Without the MongoDB credentials, tests cannot connect.

5. Supply Environment Variables

Add a variables section at the top of your .gitlab-ci.yml to inject the connection details:
Never expose plain-text passwords in your repository. Always use masked CI/CD variables for credentials.
Here’s a quick reference for these variables:

6. Configure Masked Variables in GitLab

In your GitLab project, navigate to Settings > CI/CD > Variables. Create a new variable:
  • Key: M_DB_PASSWORD
  • Value: (your database password)
  • Masked:
  • Protected: as required
The image shows a GitLab CI/CD settings page, specifically focusing on the section for managing variables. It includes details about a masked variable named "M_DB_PASSWORD" and a notification confirming its successful addition.
Return to the Pipeline Editor on your feature branch and commit the updated .gitlab-ci.yml:
The image shows a GitLab Pipeline Editor interface with options to switch branches and configure a CI/CD pipeline. The sidebar includes various project management options like Merge requests, Manage, Plan, and more.

7. Verify the Successful Pipeline

A new pipeline should start automatically. This time, the unit_testing job completes successfully:
The image shows a GitLab CI/CD pipeline interface for a project named "Solar System NodeJS Pipeline," indicating a successful test job labeled "unittesting." The pipeline has passed, and the interface displays various project management options on the left sidebar.

8. Review the Test Logs

The job log confirms the install and test steps:
Now that unit tests are passing, the next step is to collect test reports using the artifacts keyword.

References

Watch Video