SonarQube Integration for Node JS Project using GitLab

In this article we are going to cover SonarQube integration for Node JS Project using GitLab CI/CD.

Prerequisites

  • Ubuntu Server 20.04/18.04/16.04 LTS
  • SSH access with sudo privileges

Please find below articles to Install Gitlab-Runner

How to Install GitLab Runner on Ubuntu 20.04 LTS

#1:Install nodejs on Ubuntu 20.04 LTS

Update your system packages:

sudo apt-get update

Install nodejs on GitLab Runner Instance

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - 
sudo apt-get install -y nodejs

To verify nodejs version 

node -v

#2:Download and Install Sonar Scanner on Linux

Download the Sonarqube scanner package and move it to the OPT directory.

Make a directory /downloads/sonarqube

mkdir /downloads/sonarqube -p

You need to inside this  /downloads/sonarqube directory 

cd /downloads/sonarqube

Download  sonar-scanner using wget command:

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873-linux.zip

Install unzip if you don’t have

sudo apt install unzip

Unzip the file:

unzip sonar-scanner-cli-4.2.0.1873-linux.zip

Move to /opt directory

mv sonar-scanner-4.2.0.1873-linux /opt/sonar-scanner

Edit the sonar-scanner.properties file using below command:

vi /opt/sonar-scanner/conf/sonar-scanner.properties

Add this line in this file:

sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8

create a file to automate the required environment variables configuration

vi /etc/profile.d/sonar-scanner.sh

Add this lines in this file

#/bin/bash
export PATH="$PATH:/opt/sonar-scanner/bin"

Use the source command to add the sonar scanner command to the PATH variable:

source /etc/profile.d/sonar-scanner.sh

To verify version of sonar-scanner

sonar-scanner -v

#3:Create sonar-project.properties in your repository

Create a file in your repository with the name sonar-project.properties and add this lines into it

sonar.projectKey=devopshint_nodejs
sonar.qualitygate.wait=true

#4:Add SonarQube variables in your gitlab repository

SONAR_HOST_URL   : <<sonarqube-url>>
SONAR_LOGIN           : <<sonarqube username>>
SONAR_PASSWORD: <<sonarqube password>> 

#5:SonarQube integration for Node JS Project using GitLab

Create a .gitlab-ci.yml file in your repository and paste the below code into it.



sonarqube-check:
  image: 
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - sonar-scanner -X -Dsonar.sources=. -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASSWORD
 
  only:
    - main # or the name of your main branch


Conclusion:

We have covered SonarQube integration for Node JS Project using GitLab CI/CD.

Reference:

SonarQube official page for GitLab CI/CD

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap