user6830821
user6830821

Reputation:

Jenkins : How to get sonar environment variables

How to get sonar environment variables like sonar host, sonar project key or sonar workspace in order to use them in email with Jenkins plugin: Editable Email Notification.

any help or idea ?

Upvotes: 4

Views: 8473

Answers (2)

albciff
albciff

Reputation: 18507

As @chizou explains, you can use withSonarQubeEnv() to get environment values to use in your pipeline. I only want to add more information, however I want to add extra information on how to use it.

In Jenkins sonar plugins source code you can see that that the available variables injected will be:

  • SONAR_CONFIG_NAME
  • SONAR_HOST_URL
  • SONAR_AUTH_TOKEN
  • SONAR_MAVEN_GOAL
  • SONAR_EXTRA_PROPS

This variables are configured from jenkins manager, and SONAR_EXTRA_PROPS is a custom parameter where you can add key=vaule pairs separated by coma.

To use in scripted pipeline:

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                withSonarQubeEnv('Sonarqube') {
                    script{
                        // check the SONAR_ variables in all environment vars
                        def output = sh(returnStdout: true, script: 'env')
                        echo "Output: ${output}"    
                        
                        // or use directly
                        echo "${env.SONAR_HOST_URL}"
                        echo "${env.SONAR_CONFIG_NAME}"
                    }
                    
                }
            }
        }
    }
}

Upvotes: 0

chizou
chizou

Reputation: 1272

you can use the withSonarQubeEnv() plugin function to pull the values. You have to configure Jenkins with the Sonar variables you want it to use. You can find more info about that at the link below. Unfortunately, I can't seem to find documentation from Sonarqube what variables are being provided. You might want to run something like sh "env" within the withSonarQubeEnv stanza to get an idea.

https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline

Upvotes: 3

Related Questions