Due to some maintenance issues, this service has been switched in read-only mode, you can find more information about the why

and how to migrate your plugin documentation in this blogpost

Skip to end of metadata
Go to start of metadata

Plugin Information

View Quality Gates on the plugin site for more information.


This plugin will fail the build whenever the Quality Gates criteria in the Sonar analysis aren't met (the project Quality Gates status is different than "Passed").

Requirements

Jenkins

Jenkins version 1.532 or newer is required.

Sonar

Working with all the versions of Sonar.

Setup

1. Global Configuration

1.1. No Sonar Instance Configured

        If there isn't any instance configured the Global Configuration will look like this:

       

        And the plugin will be running with default Sonar Instance. Which contains the following values: URL='http//localhost:9000', Username='admin' and Password='admin'.

1.2. One Sonar Instance Configured

       

  • Name Specify the value of the name, so that is not empty. Configurations with duplicated values for the name will not be saved.
  • SonarQube Server URL Specify the URL of your Sonar instance URL format for HTTP protocol is any legal http address, e.g. http://myhost.com:9000
  • SonarQube account login Specify the username for your account on this instance of Sonar, otherwise Quality Gates Plugin won't be able to get the necessary data if anonymous access is disabled for your project. If not specified the default Sonar username will be used.
  • SonarQube account password Specify the password for your account on this instance of Sonar, otherwise Quality Gates Plugin won't be able to get the necessary data if anonymous access is disabled for your project. If not specified the default Sonar password will be used.
1.3. Two or more Sonar Instances Configured

        If you have two or more configurations for Sonar Instances in the Global Configuration you will need to choose one of them in your job configuration.

2. Job Configuration

Quality Gates Plugin needs a SonarQube code analysis so it can check its Quality Gates status. In order to get the latest quality gates status from the most recent Sonar analysis, Quality Gates Plugin should be put below SonarQube plugin in the post-build step of the job if you use the older version of SonarQube analysis or if you use the Standalone SonarQube Analysis in the build step of the job just put our plugin in the post-build step.

2.1. No Sonar Instance in the Global Configuration

        

  • When using the older SonarQube way of analysis in the post-build step:
    Project Key - To get the Project Key go to your Sonar instance (login if necessary) and click on the project whose key you need. You will be redirected to an URL like this: 'http://myhost.:9000/overview?id=123'.  
    Another click on the project name will redirect you to the same view with the URL looking like this: 'http://localhost:9000/overview?id=com.company%3AMyProject'.  
    ex: 'com.company%3AMyProject' is your Project Key
  • When using the Standalone SonarQube Analysis as a build step: When using the Standalone SonarQube Analysis as a build step: 
    You define your own Project Key in the 'sonar-project.properties' file of your project or directly in the 'Analysis properties' textbox of the Standalone SonarQube Analysis
2.2. One Sonar Instance in the Global Configuration

       

  • Project Key - same as above.
2.3. Two or more Sonar Instances in the Global Configuration

        

  • Sonar instance 'Name' -  Choose the configuration for the Sonar Instance that you want to associate with this build.
  • Project Key - same as above.
  • No labels

29 Comments

  1. Unknown User (ebrahim_moshaya)

    It's a big shame that the Project Key field does not accept environment variables

    . I'm using a template and would like to use use $

    Unknown macro: {POM_GROUPID}

    :$

    Unknown macro: {POM_ARTIFACTID}

    as the project key. Please could you update this plugin to allow for environment variables?

    1. Unknown User (dpd90)

      The plugin is in constant development phase. In our latest release environment variables are accepted.

      Thank you for your input (big grin)

    2. Unknown User (egrozdani)

      Hello Ebrahim,
      As Dimitar said in the new version of our plugin you can use environmental variables in the  Project Key field. Just be sure that your project key is saved in a single environmental variable for now, so  it should look like:

      ${PROJECT_KEY}

      and not 

      ${POM_GROUPID}:${POM_ARTIFACTID}
      1. Unknown User (bowles123)

        @Eleni: I tried using and environment variable for the project key and jenkins just spit out an error when I tried to save/apply the configuration. I tried using an environment variable I injected myself (PROJECT_KEY) and that didn't work, but neither did using and environment variable from the list of environment variables given by Jenkins. Any insight would be helpful, thanks!

      2. Unknown User (anuragsharma1989)

        Guys, I have tried and it just pops up below error once you enter variabled project key in configuration and save javax.servlet.ServletException: quality.gates.jenkins.plugin.QGException: Environment variable with name 'PROJECT_KEY' does not exist.
        at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:796)
        at org.kohsuke.stapler.Stapler.invoke(Stapler.java:876)
        at org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:249)
        at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:53)
        at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:746)
        at org.kohsuke.stapler.Stapler.invoke(Stapler.java:876)
        at org.kohsuke.stapler.Stapler.invoke(Stapler.java:649)

        I have created the same key value in jenkins configuration >> variable though. Please suggest.

        I need to dynamically populate the project key as I am having single job for building the project of multiple branch in a repo.

        1. Unknown User (sarg)

          I added support for expanding variables in my fork: https://github.com/sarg/quality-gates-plugin

          1. Unknown User (anuragsharma1989)

            Hi Sergey,

            Thanks for the reply. How am I supposed to use it now. Will it work fine if I use it like I was using earlier ?

          2. Unknown User (anuragsharma1989)

            Hi Sergey,

            And wen it will be added as a version to the Quality Gate Plugin?

          3. Unknown User (anuragsharma1989)

            I tried to download the zip from he link you provided from github and changed the extension to .hpi, but then while installing from jenkins UI advanced panel I am getting below exceptionjavax.servlet.ServletException: java.lang.NullPointerException
            at hudson.PluginManager.doUploadPlugin(PluginManager.java:952)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:606)
            at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:298)
            at org.kohsuke.stapler.interceptor.RequirePOST$Processor.invoke(RequirePOST.java:46)
            at org.kohsuke.stapler.Function$InterceptedFunction.invoke(Function.java:399)
            at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:161)
            at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:96)
            at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:121)

            Please help and let me know how to resolve.

  2. Unknown User (wimederveen)

    I'm wondering if you are already working on supporting the new Jenkins 2 pipeline script functionality.
    What we want to do is let a stage in the pipeline fail when the sonar quality gates are not passed.
    Currently it can only be configured in a traditional Jenkins job and not through the pipeline Jenkinsfile script

    1. Unknown User (pyrocks)

    2. Unknown User (janvandeklok)

      +1  Being able to execute the quality gateway in a pipeline Jenkins file would be a great enhancement.

      1. Unknown User (brunohe)

        +1 It would be a great help

  3. Unknown User (deag)

    Any plans to support auth tokens instead of user and password? 

  4. Unknown User (ruchirb)

    How to use this plugin with bitbucket. We use bitbucket teams so all repositories are scanned for jenkinsfile and automatically jobs are created. We want to enable quality gates checking in this scenario but couldn't find such configuration in organization folders.

  5. Unknown User (reshmashinde)

    1. Unknown User (reshmashinde)

      We found solution to mark build as failure when Quality gate is not passed using Sonar Rest api. Add “Execute Shell” after Sonar Step and use below code

      Tip : Introduce sleep time of 10s before this step , just to ensure that Sonar site is updated with task result status.

      #Fetching TASKURL from report-task.txt in workspace

      url=$(cat $WORKSPACE/.sonar/report-task.txt | grep ceTaskUrl | cut -c11- )

      #Fetching Task attributes from Sonar Server

      curl -u  admin:${admin_pwd} -L $url | python -m json.tool

      #Setting up task status to check if sonar scan is completed successfully.

      curl -u  admin:${admin_pwd} -L $url -o task.json

      status=$(python -m json.tool < task.json | grep -i "status" | cut -c20- | sed 's/.\(.\)$/\1/'| sed 's/.$//' )

      echo ${status}

      #If SonarScan is completed successfully then set analysis ID & URLS.

      if [ $status = SUCCESS ]; then

      analysisID=$(python -m json.tool < task.json | grep -i "analysisId" | cut -c24- | sed 's/.\(.\)$/\1/'| sed 's/.$//')

      analysisUrl="https://sonar.net/api/qualitygates/project_status?analysisId=${analysisID}"

      echo ${analysisID}

      echo ${analysisUrl}

      else

      echo "Sonnar run was not sucess"

      exit 1

      fi

      #Fetching SonarGate details using analysis URL
      curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool

      curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool | grep -i "status" | cut -c28- | sed 's/.$//' >> tmp.txt
      cat tmp.txt
      sed -n '/ERROR/p' tmp.txt >> error.txt
      cat error.txt

      if [ $(cat error.txt | wc -l) -eq 0 ]; then
      echo "Quality Gate Passed ! Setting up SonarQube Job Status to Success ! "
      else
      exit 1
      echo "Quality Gate Failed ! Setting up SonarQube Job Status to Failure ! "
      fi

      #Cleaning up the files
      unset url
      unset status
      unset analysisID
      unset analysisUrl
      > task.json
      > tmp.txt
      > error.txt

      1. Unknown User (reshmashinde)

        Happy to share this info.

  6. Unknown User (ramden)

    State of Apr. 10. 2017 -> Does not work. 

    After install the latest plugin, after adding the quality step build step on the latest jenkins, only the title is visible.

    The entry field (input box) for project key is not visible and saving without entering anything causes general error.

    This is probable a bug?

    Additional info: Quality gate is correctly configured in the jenkins system settings

  7. Unknown User (reshmashinde)

    You can also create Sonar Project and bind it to Quality gate using command line. Hope so this will help somebody.

    #Create Project
    curl -v -u admin:${admin_pwd} -H "Content-Type: application/json" -X POST -d '{"key": "ABC:'${Branch_Name}'", "name": "ABC '${Branch_Name}'", "branch": "'${Branch_Name}'"}' https://sonar.net/projects/create

    # To Fetch Project ID
    project_id=$(curl -u admin:${admin_pwd} -X GET -H "Accept: application/json" -d search="ABC ${Branch_Name}" https://sonar.net/api/projects/index | python -m json.tool | grep -i "id" | cut -c16- | sed 's/.\(.\)$/\1/'| sed 's/.$//')
    echo ${project_id}

    #To Fetch Quality Gate ID
    quality_id=$(curl -u admin:${admin_pwd} -X GET -H "Accept: application/json" -d search="ABC" https://sonar.net/api/qualitygates/list | python -m json.tool | grep -i "id" | cut -c16- | sed 's/.\(.\)$/\1/'| sed 's/.$//')

    #Associate Project to Quality Gate
    curl -u admin:${admin_pwd} -X POST "https://sonar.net/api/qualitygates/select?gateId=${quality_id}&projectId=${project_id}"

  8. Unknown User (alexis_legros)

    Hi,

    I noticed the addition of unstable Jenkins build for warning quality gate status in sources but this has not been released, are there any plan to publish this feature ?

    Thanks in advance.

    Best regards,
    Alexis.

  9. Unknown User (johntiger1)

    Not working as of 1/26/2018. Running on Jenkins 2.73.3, and only the title part shows up

    1. Unknown User (johntiger1)

      Actually, I retract my above comment. I got it to work, but the documentation was not very clear imo. I had to add some items to the configuration part of Jenkins (just follow this guide: https://stackoverflow.com/questions/42189231/cant-fill-in-project-key-for-quality-gates-plugin)

      1. Unknown User (bloodymir)

        Having the same issue. May I ask what exactly have you tried? Adding Sonar both to Quality Gate and Sonar Scanner plugin? 

  10. Unknown User (virginia_mesa)

    Hi,
    It takes a while for me to get the Sonar QG plugin. I do not know if someone else will happen, install the new version (2.73) to see if it was solved, but nothing. I have debugged, and I see that I fail to do the requestPOST and GET. I returned a 401, and I thought it would be because I was not sending the credentials well, but I've seen that it sends them well. Up to point 1, where I put the parameters (user and pass) I take it well, add the header and proceed to the call RequestPOST, but I get an error in point 2.
    Do you know why it could be?
  11. Unknown User (nicolasw)

    Not working for me, using Jenkins 2.119 and sonar 5.6.3

    The plugin always logs "PostBuild-Step: Quality Gates plugin build passed: TRUE" and does not notice any FAILED quality gates. The quality gate flag is RED in Jenkins.

    1. Unknown User (nicolasw)

      Got it to work using the quality gates plugin https://plugins.jenkins.io/quality-gates

    2. Unknown User (nicolasw)

      Please disregard, Issue above is for sonar-quality-gates-plugin

  12. Unknown User (shen3lu4)

    We have a sonar server set up with a proxy in front of it to redirect user to SSO login. In Jenkins, we configured the sonar server & quality gates URLs to point directly to the sonar server (not the proxy URL) to bypass the SSO redirect. But since we would like all email notifications to contain the proxy URL, we configured the "Server base URL" in sonar Administration → Configuration → General Settings to capture the proxy URL. However, as a result, the quality gate check in Jenkins pipeline is picking up the Server base URL and therefore failed. Any ways to make this work? Why can't quality gate just use the URL configured in Jenkins?

    Jenkins 2.121.2 sonar 7.2 quality gate plugin 2.5