Configure SonarQube scanner in Jenkins pipeline for a typescript/react project

First of all, thank you very much for a very useful good product SonarQube! Nice job guys!

  • SonarQube Community Edition Version 8.8, SonarQube Scanner for Jenkins [2.13.1], )
  • I am trying to run static code analysis in the Jenkins pipeline.
  • I already have one project that runs code analysis in a SonarQube and it works fine.
    I have configured SonarQube installations in Jenkins configurations. Right now I trying to run analyze React/typescript 4.4 code in SonarQube through the pipeline but don’t know how to do it.

Could you please tell me a pipeline template for static code analysis?

stage('SonarQube analysis') {
        steps {
            script{
            def scannerHome = tool 'sonarscan';
            withSonarQubeEnv('sonarqube') {
                sh "${tool("sonarscan")}/bin/sonar-scanner \
                    -Dsonar.projectKey=reactapp \
                    -Dsonar.projectName=reactapp"
            }
            }
        }
    }

After finish pipeline in SonarQube have

CSS rules were not executed. Error when running: 'node -v'. Is Node.js available during analysis?
JavaScript and/or TypeScript rules were not executed. Error when running: 'node -v'. Is Node.js available during analysis?

any help.

Solved by myself, and my little contribution to the SonarQube community.
Below is the working pipeline.

Needs to be specified and configured in Jenkins NodeJS
Manage Jenkins → Global Tool Configuration → Scroll down → NodeJS


and then specify NodeJS name in pipeline as you configured it in Jenkins, like on picture “nodejs”

properties([disableConcurrentBuilds()])
pipeline {
  agent {
    label 'master'
  }
  tools {
    nodejs 'nodejs'
  }
  options {
    // This is required if you want to clean before build
    skipDefaultCheckout(true)
  }
  stages {
    stage('Clone SCM for sonar') {
      steps {
        // Clean before build
        cleanWs()
        git branch: 'develop',
          credentialsId: 'credentials',
          url: 'git@bitbucket.org:Company-Bucket/reactapp.git'
      }
    }
    stage('SonarQube analysis') {
      steps {
        script {
          def scannerHome = tool 'sonarscan';
          withSonarQubeEnv('sonarqube') {
            sh "${tool("sonarscan ")}/bin/sonar-scanner -Dsonar.projectKey=reactapp -Dsonar.projectName=reactapp"
          }
        }
      }
    }
    stage("Quality gate") {
      steps {
        script {
          def qualitygate = waitForQualityGate()
          sleep(10)
          if (qualitygate.status != "OK") {
            waitForQualityGate abortPipeline: true
          }
        }
      }
    }
  }
}
2 Likes

Hi @kda33 ,

Welcome to SonarSource Community! :sonarsource:

Thank you for sharing your resolution and thank you for the compliments. Yes, you need to ensure Node.js is available in your Jenkinsfile pipeline.

Come back to this forum and create a new topic if you have any further questions or need assistance.

Joe