Search code examples
groovyjenkinssonarqubejenkins-job-dsl

Configure Jenkin's SonarQube section using Job-DSL


Using Job-DSL we can configure a C# project in Jenkins.

The SonarQube tasks is giving us a hard time.

        StepContext.metaClass.sonar = {
        -> NodeBuilder nodeBuilder = new NodeBuilder()
            stepNodes << nodeBuilder.'hudson.plugins.sonar.SonarRunnerBuilder' {
                jdk('(Inherit From Job)')
                usePrivateRepository(false)
            }
    }

How to set the path to the sonar-project.properties config file, using the Job-DSL script?

Sonar section

Final script

Thanks to @Bruno César, I added pathToSonarProjectProperties as parameter.

    StepContext.metaClass.sonar = { String pathToSonarProjectProperties
        -> NodeBuilder nodeBuilder = new NodeBuilder()
            stepNodes << nodeBuilder.'hudson.plugins.sonar.SonarRunnerBuilder' {
                jdk('(Inherit From Job)')
                usePrivateRepository(false)
                project(pathToSonarProjectProperties)
            }
    }

The sonar function is called with the relative-to-project-root path of sonar-project.properties:

sonar("Framework\\xxx\\xxx\\sonar-project.properties")

Solution

  • In SonarRunnerBuilder class there is a project attribute that represents the path to a file with properties for the project.

    In the same way in which you set the JDK (jdk('(Inherit From Job)')) you can set the path property. In your example, try like this:

    StepContext.metaClass.sonar = {
        -> NodeBuilder nodeBuilder = new NodeBuilder()
        stepNodes << nodeBuilder.'hudson.plugins.sonar.SonarRunnerBuilder' {
            jdk('(Inherit From Job)')
            usePrivateRepository(false)
            project('${your.path.here}')
        }
    }