選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

sonarscanner-for-jenkins.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ---
  2. title: SonarScanner for Jenkins
  3. url: /analysis/scan/sonarscanner-for-jenkins/
  4. ---
  5. [[info]]
  6. | By [SonarSource](https://www.sonarsource.com/) – GNU LGPL 3 – [Issue Tracker](https://jira.sonarsource.com/browse/SONARJNKNS) – [Source](https://github.com/SonarSource/sonar-scanner-jenkins)
  7. | Click [here](https://plugins.jenkins.io/sonar) for the latest version.
  8. This plugin lets you centralize the configuration of SonarQube server connection details in Jenkins global configuration.
  9. Then you can trigger SonarQube analysis from Jenkins using standard Jenkins Build Steps or [Jenkins Pipeline DSL](https://jenkins.io/solutions/pipeline/) to trigger analysis with:
  10. * [SonarScanner](/analysis/scan/sonarscanner/)
  11. * [SonarScanner for Maven](/analysis/scan/sonarscanner-for-maven/)
  12. * [SonarScanner for Gradle](/analysis/scan/sonarscanner-for-gradle/)
  13. * [SonarScanner for MSBuild](/analysis/scan/sonarscanner-for-msbuild/)
  14. Once the job is complete, the plugin will detect that a SonarQube analysis was made during the build and display a badge and a widget on the job page with a link to the SonarQube dashboard as well as quality gate status.
  15. ## Installation
  16. 1. [Install the SonarScanner for Jenkins via the Jenkins Update Center](https://plugins.jenkins.io/sonar).
  17. 1. Configure your SonarQube server(s):
  18. 1. Log into Jenkins as an administrator and go to **Manage Jenkins > Configure System**.
  19. 1. Scroll down to the SonarQube configuration section, click **Add SonarQube**, and add the values you're prompted for.
  20. 1. The server authentication token should be created as a 'Secret Text' credential.
  21. ## Analyzing a .NET solution
  22. **Global Configuration**
  23. This step is mandatory if you want to trigger any of your analyses with the SonarScanner for MSBuild. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose with which launcher to use to run the SonarQube analysis.
  24. 1. Log into Jenkins as an administrator and go to **Manage Jenkins > Global Tool Configuration**
  25. 1. Click on **Add SonarScanner for MSBuild**
  26. 1. Add an installation of the latest available version. Check **Install automatically** to have the SonarScanner for MSBuild automatically provisioned on your Jenkins executors
  27. If you do not see any available version under Install from GitHub, first go to Manage Jenkins > Manage Plugins > Advanced and click on Check now
  28. **Job Configuration**
  29. 1. Configure the project, and go to the **Build** section.
  30. 1. Add the SonarQube for MSBuild - Begin Analysis to your build
  31. 1. Configure the SonarQube Project Key, Name and Version in the SonarScanner for MSBuild - Begin Analysis build step
  32. 1. Add the MSBuild build step or the Execute Windows batch command to execute the build with MSBuild 14 (see compatibility) to your build.
  33. 1. Add the SonarQube for MSBuild - End Analysis build steps to your build
  34. ## Analyzing a Java project with Maven or Gradle
  35. **Global Configuration**
  36. 1. Log into Jenkins as an administrator and go to Manage Jenkins > Configure System
  37. 1. Scroll to the SonarQube servers section and check Enable injection of SonarQube server configuration as build environment variables
  38. **Job Configuration**
  39. 1. **Configure** the project, and go to the **Build Environment** section.
  40. 1. Enable **Prepare SonarScanner environment** to allow the injection of SonarQube server values into this particular job. If multiple SonarQube instances are configured, you will be able to choose which one to use.
  41. Once the environment variables are available, use them in a standard Maven build step (Invoke top-level Maven targets) by setting the Goals to include, or a standard Gradle build step (Invoke Gradle script) by setting the Tasks to execute.
  42. Maven goal:
  43. ```
  44. $SONAR_MAVEN_GOAL
  45. ```
  46. Gradle task:
  47. ```
  48. sonarqube
  49. ```
  50. In both cases, launching your analysis may require authentication. In that case, make sure that the Global Configuration defines a valid SonarQube token.
  51. ## Analyzing other project types
  52. **Global Configuration**
  53. This step is mandatory if you want to trigger any of your SonarQube analyses with the SonarScanner. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose with which launcher to use to run the SonarQube analysis.
  54. 1. Log into Jenkins as an administrator and go to **Manage Jenkins > Global Tool Configuration**
  55. 1. Scroll down to the SonarScanner configuration section and click on Add SonarScanner. It is based on the typical Jenkins tool auto-installation. You can either choose to point to an already installed version of SonarScanner (uncheck 'Install automatically') or tell Jenkins to grab the installer from a remote location (check 'Install automatically')
  56. If you don't see a drop down list with all available SonarScanner versions but instead see an empty text field then this is because Jenkins still hasn't downloaded the required update center file (default period is 1 day). You may force this refresh by clicking 'Check Now' button in Manage Plugins > Advanced tab.
  57. **Job Configuration**
  58. 1. **Configure** the project, and go to the **Build** section.
  59. 1. Add the SonarScanner build step to your build.
  60. 1. Configure the SonarQube analysis properties. You can either point to an existing sonar-project.properties file or set the analysis properties directly in the **Analysis properties** field
  61. ## Using a Jenkins pipeline
  62. We provide a `withSonarQubeEnv` block that allows you to select the SonarQube server you want to interact with. Connection details you have configured in Jenkins global configuration will be automatically passed to the scanner.
  63. If needed you can override the `credentialId` if you don't want to use the one defined in global configuration (for example if you define credentials at folder level).
  64. Here are a some examples for every scanner, assuming you run on Unix slaves and you have configured a server named "My SonarQube Server" as well as required tools. If you run on Windows slaves, just replace `sh` with `bat`.
  65. SonarScanner:
  66. ```
  67. node {
  68. stage('SCM') {
  69. git 'https://github.com/foo/bar.git'
  70. }
  71. stage('SonarQube analysis') {
  72. def scannerHome = tool 'SonarScanner 4.0';
  73. withSonarQubeEnv('My SonarQube Server') { // If you have configured more than one global server connection, you can specify its name
  74. sh "${scannerHome}/bin/sonar-scanner"
  75. }
  76. }
  77. }
  78. ```
  79. SonarScanner for Gradle:
  80. ```
  81. node {
  82. stage('SCM') {
  83. git 'https://github.com/foo/bar.git'
  84. }
  85. stage('SonarQube analysis') {
  86. withSonarQubeEnv() { // Will pick the global server connection you have configured
  87. sh './gradlew sonarqube'
  88. }
  89. }
  90. }
  91. ```
  92. SonarScanner for Maven:
  93. ```
  94. node {
  95. stage('SCM') {
  96. git 'https://github.com/foo/bar.git'
  97. }
  98. stage('SonarQube analysis') {
  99. withSonarQubeEnv(credentialsId: 'f225455e-ea59-40fa-8af7-08176e86507a', installationName: 'My SonarQube Server') { // You can override the credential to be used
  100. sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar'
  101. }
  102. }
  103. }
  104. ```
  105. SonarScanner for MSBuild:
  106. ```
  107. node {
  108. stage('SCM') {
  109. git 'https://github.com/foo/bar.git'
  110. }
  111. stage('Build + SonarQube analysis') {
  112. def sqScannerMsBuildHome = tool 'Scanner for MSBuild 4.6'
  113. withSonarQubeEnv('My SonarQube Server') {
  114. bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe begin /k:myKey"
  115. bat 'MSBuild.exe /t:Rebuild'
  116. bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe end"
  117. }
  118. }
  119. }
  120. ```
  121. ## Pause pipeline until quality gate is computed
  122. The `waitForQualityGate` step will pause the pipeline until SonarQube analysis is completed and returns quality gate status.
  123. ### Pre-requisites:
  124. * Configure a webhook in your SonarQube server pointing to `<your Jenkins instance>/sonarqube-webhook/`
  125. * Use `withSonarQubeEnv` step in your pipeline (so that SonarQube taskId is correctly attached to the pipeline context).
  126. Scripted pipeline example:
  127. ```
  128. node {
  129. stage('SCM') {
  130. git 'https://github.com/foo/bar.git'
  131. }
  132. stage('SonarQube analysis') {
  133. withSonarQubeEnv('My SonarQube Server') {
  134. sh 'mvn clean package sonar:sonar'
  135. } // submitted SonarQube taskId is automatically attached to the pipeline context
  136. }
  137. }
  138. // No need to occupy a node
  139. stage("Quality Gate"){
  140. timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout
  141. def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
  142. if (qg.status != 'OK') {
  143. error "Pipeline aborted due to quality gate failure: ${qg.status}"
  144. }
  145. }
  146. }
  147. ```
  148. Thanks to the webhook, the step is implemented in a very lightweight way: no need to occupy a node doing polling, and it doesn't prevent Jenkins to restart (step will be restored after restart). Note that to prevent race conditions, when the step starts (or is restarted) a direct call is made to the server to check if the task is already completed.
  149. Declarative pipeline example:
  150. ```
  151. pipeline {
  152. agent any
  153. stages {
  154. stage('SCM') {
  155. steps {
  156. git url: 'https://github.com/foo/bar.git'
  157. }
  158. }
  159. stage('build && SonarQube analysis') {
  160. steps {
  161. withSonarQubeEnv('My SonarQube Server') {
  162. // Optionally use a Maven environment you've configured already
  163. withMaven(maven:'Maven 3.5') {
  164. sh 'mvn clean package sonar:sonar'
  165. }
  166. }
  167. }
  168. }
  169. stage("Quality Gate") {
  170. steps {
  171. timeout(time: 1, unit: 'HOURS') {
  172. // Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
  173. // true = set pipeline to UNSTABLE, false = don't
  174. waitForQualityGate abortPipeline: true
  175. }
  176. }
  177. }
  178. }
  179. }
  180. ```
  181. If you want to run multiple analysis in the same pipeline and use waitForQualityGate you have to do everything in order:
  182. ```
  183. pipeline {
  184. agent any
  185. stages {
  186. stage('SonarQube analysis 1') {
  187. steps {
  188. sh 'mvn clean package sonar:sonar'
  189. }
  190. }
  191. stage("Quality Gate 1") {
  192. steps {
  193. waitForQualityGate abortPipeline: true
  194. }
  195. }
  196. stage('SonarQube analysis 2') {
  197. steps {
  198. sh 'gradle sonarqube'
  199. }
  200. }
  201. stage("Quality Gate 2") {
  202. steps {
  203. waitForQualityGate abortPipeline: true
  204. }
  205. }
  206. }
  207. }
  208. ```
  209. ### Configuring a webhook secret
  210. If you want to verify the webhook payload that is sent to Jenkins, you can add a secret to your webhook on SonarQube.
  211. To set the secret:
  212. 1. In Jenkins, navigate to **Manage Jenkins > Configure System > SonarQube Server > Advanced > Webhook Secret** and click the **Add** button.
  213. 1. Select **Secret text** and give the secret an ID.
  214. 1. Select the secret from the dropdown menu.
  215. If you want to override the webhook secret on a project level, you can add the secret to Jenkins and then reference the secret ID when calling `waitForQualityGate`.
  216. waitForQualityGate(webhookSecretId: 'yourSecretID')
  217. if your pipeline is declarative or
  218. waitForQualityGate webhookSecretId: 'yourSecretID'
  219. if your pipeline is scripted.