Skip to content

Commit 180a417

Browse files
Merge pull request #101 from diegopereiraeng/main
Add support for SonarQube configuration file and customizable quality gate exit code
2 parents 1e20e6f + 6256d8f commit 180a417

File tree

9 files changed

+391
-291
lines changed

9 files changed

+391
-291
lines changed

.drone.yml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ steps:
1919
when:
2020
ref:
2121
- "refs/pull/**" # Only run for pull requests
22-
- name: publish-2.4.1
22+
- name: publish-2.4.2
2323
image: plugins/docker:20
2424
settings:
2525
# auto_tag: true
2626
# auto_tag_suffix: v2.0.2-java17
2727
tags:
28-
- v2.4.1
29-
# - latest
30-
# - stable-java17
28+
- v2.4.2
29+
- latest
30+
- stable-java17
3131
daemon_off: false
3232
dockerfile: Dockerfile
3333
password:
@@ -39,6 +39,26 @@ steps:
3939
ref:
4040
- refs/heads/main
4141
- refs/tags/**
42+
- name: publish-2.4.2-java11
43+
image: plugins/docker:20
44+
settings:
45+
# auto_tag: true
46+
# auto_tag_suffix: v2.0.2-java17
47+
tags:
48+
- v2.4.2
49+
- latest-java11
50+
- stable-java11
51+
daemon_off: false
52+
dockerfile: Dockerfile_Deprecated
53+
password:
54+
from_secret: docker_password
55+
repo: plugins/sonarqube-scanner
56+
username:
57+
from_secret: docker_username
58+
when:
59+
ref:
60+
- refs/heads/main
61+
- refs/tags/**
4262
trigger:
4363
ref:
4464
- refs/heads/main

DOCS.md

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,55 @@ steps:
2525
2626
Customized parameters could be specified:
2727
28-
```diff
29-
steps:
30-
- name: code-analysis
31-
image: drone-plugins/sonarqube-scanner
32-
settings:
33-
sonar_host:
34-
from_secret: sonar_host
35-
sonar_token:
36-
from_secret: sonar_token
37-
sonar_name: project-harness
38-
sonar_key: project-harness
39-
+ build_number: 1.0
40-
+ timeout: 20
41-
+ sources: .
42-
+ level: DEBUG
43-
+ showProfiling: true
44-
+ exclusions: **/static/**/*,**/dist/**/*.js
45-
+ usingProperties: false
46-
+ binaries: .
28+
```yaml
29+
steps:
30+
- name: code-analysis
31+
image: drone-plugins/sonarqube-scanner
32+
settings:
33+
sonar_host:
34+
from_secret: sonar_host
35+
sonar_token:
36+
from_secret: sonar_token
37+
sonar_name: project-harness
38+
sonar_key: project-harness
39+
build_number: 1.0
40+
timeout: 20
41+
sources: .
42+
level: DEBUG
43+
showProfiling: true
44+
exclusions: **/static/**/*,**/dist/**/*.js
45+
usingProperties: false
46+
binaries: .
47+
sonar_config_file: true
48+
sonar_config_file_override: false
49+
quality_gate_error_exit_code: 5
50+
sonar_qualitygate_timeout: 300
51+
branch: master
52+
inclusions: "*.go, *.java"
53+
sonar_quality_enabled: true
54+
artifact_file: artifact.json
55+
javascript_icov_reportPath: /path/to/icov/report
56+
java_coverage_plugin: jacoco
57+
jacoco_report_path: /path/to/jacoco/report
58+
ssl_keystore_pwd: your-keystore-password
59+
cacerts_location: /path/to/cacerts
60+
junit_reportpaths: /path/to/junit/reports
61+
source_encoding: UTF-8
62+
tests: /path/to/tests
63+
java_test: /path/to/java/test
64+
pr_key: 123
65+
pr_branch: your-pr-branch
66+
pr_base: your-pr-base
67+
coverage_exclusion: **/test/**/*.*,**/*.test.go
68+
java_source: 1.8
69+
java_libraries: /path/to/libraries
70+
surefire_reportsPath: /path/to/surefire/reports
71+
typescript_lcov_reportPaths: /path/to/typescript/lcov/reports
72+
verbose: true
73+
custom_jvm_params: "-Dsonar.java.source='value_you_want'"
74+
taskid: your-task-id
75+
skip_scan: true
76+
SONAR_SCANNER_OPTS: "--add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED"
4777
```
4878
4979
# Secret Reference
@@ -52,48 +82,37 @@ Safety first, the host and token are stored in Drone Secrets.
5282
* `sonar_host`: Host of SonarQube with schema(http/https).
5383
* `sonar_token`: User token used to post the analysis report to SonarQube Server. Click User -- My Account -- Security -- Generate Tokens.
5484

55-
5685
# Parameter Reference
5786

58-
* `sonar_name`: Sonar Project NAme.
87+
* `sonar_name`: Sonar Project Name.
5988
* `sonar_key`: Sonar Project Key.
6089
* `sonar_qualitygate_timeout`: Timeout in seconds for Sonar Scan.
61-
* `artifact_file`: Timeout in seconds for Sonar Scan.
62-
* `sonar_quality_enabled`: True to block pipeline if sonar quality gate conditions are not met.
90+
* `artifact_file`: Path to the artifact file that will be generated by the plugin.
91+
* `sonar_quality_enabled`: True to block the pipeline if Sonar quality gate conditions are not met.
6392
* `branch`: Branch for analysis. (-Dsonar.branch.name=)
6493
* `build_number`: Build Version.
65-
6694
* `build_version`: Code version, Default value `DRONE_BUILD_NUMBER`.
67-
* `timeout`: Default seconds `60`.
68-
* `sources`: Comma-separated paths to directories containing source files.
69-
* `inclusions`: Comma-delimited list of file path patterns to be included in analysis. When set, only files matching the paths set here will be included in analysis.
95+
* `timeout`: Timeout in seconds, default `60`.
96+
* `sources`: Comma-separated paths to directories containing source files.
97+
* `inclusions`: Comma-delimited list of file path patterns to be included in analysis. Example: `*.go, *.java`.
7098
* `exclusions`: Comma-delimited list of file path patterns to be excluded from analysis. Example: `**/static/**/*,**/dist/**/*.js`.
71-
* `level`: Control the quantity / level of logs produced during an analysis. Default value `INFO`.
72-
* DEBUG: Display INFO logs + more details at DEBUG level.
73-
* TRACE: Display DEBUG logs + the timings of all ElasticSearch queries and Web API calls executed by the SonarQube Scanner.
99+
* `level`: Control the quantity/level of logs produced during an analysis. Default value `INFO`. Options are `DEBUG` and `TRACE`.
74100
* `showProfiling`: Display logs to see where the analyzer spends time. Default value `false` (-Dsonar.showProfiling=)
75-
* `branchAnalysis`: Pass currently analysed branch to SonarQube. (Must not be active for initial scan!) Default value `false`
76-
77-
78-
* `usingProperties`: Using the `sonar-project.properties` file in root directory as sonar parameters. (Not include `sonar_host` and
79-
`sonar_token`.) Default value `false`
80-
81-
* `sonar_qualitygate_timeout`: Quality gate timeout. (-Dsonar.qualitygate.timeout=)
82-
83-
* `binaries`: app binaries (-Dsonar.java.binaries=)
84-
101+
* `branchAnalysis`: Pass currently analyzed branch to SonarQube. (Must not be active for initial scan!) Default value `false`.
102+
* `usingProperties`: Use the `sonar-project.properties` file in the root directory as sonar parameters. (Not including `sonar_host` and `sonar_token`.) Default value `false`.
103+
* `binaries`: Path to application binaries (-Dsonar.java.binaries=).
104+
* `sonar_config_file`: Use `sonar-project.properties` if available. Default value `false`.
105+
* `sonar_config_file_override`: Use `sonar-project.properties` if available and override host, login, or project key settings. Default value `false`.
106+
* `quality_gate_error_exit_code`: Specifies the "exit code" error when the quality gate fails. Default is `5`.
85107

86108
# Javascript Parameters
87109

88-
* `javascript_icov_reportPath`: Path to coverage report (-Dsonar.javascript.lcov.reportPath)
89-
110+
* `javascript_icov_reportPath`: Path to coverage report (-Dsonar.javascript.lcov.reportPath).
90111

91112
# Java Parameters
92113

93-
* `jacoco_report_path`: Path to Jacoco Report (if not default). (-Dsonar.jacoco.reportPath=)
94-
95-
* `java_coverage_plugin`: plugin to use as coverage. ex: jacoco (-Dsonar.java.coveragePlugin=)
96-
114+
* `jacoco_report_path`: Path to Jacoco Report (if not default). (-Dsonar.jacoco.reportPath=).
115+
* `java_coverage_plugin`: Plugin to use as coverage, e.g., jacoco (-Dsonar.java.coveragePlugin=).
97116

98117
# Notes
99118

@@ -102,7 +121,7 @@ Safety first, the host and token are stored in Drone Secrets.
102121
* You could also add a file named `sonar-project.properties` at the root of your project to specify parameters.
103122

104123
Code repository: [drone-plugins/sonarqube-scanner](https://github.com/drone-plugins/sonarqube-scanner).
105-
SonarQube Parameters: [Analysis Parameters](https://docs.sonarqube.org/display/SONAR/Analysis+Parameters)
124+
SonarQube Parameters: [Analysis Parameters](https://docs.sonarqube.org/display/SONAR/Analysis+Parameters).
106125

107126
# Test your SonarQube Server:
108127

Dockerfile_Deprecated

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o harness-sonar
1515

1616
FROM openjdk:11.0.16-jre
1717

18-
ARG SONAR_VERSION=5.0.1.3006
18+
ARG SONAR_VERSION=4.6.2.2472
1919
ARG SONAR_SCANNER_CLI=sonar-scanner-cli-${SONAR_VERSION}
2020
ARG SONAR_SCANNER=sonar-scanner-${SONAR_VERSION}
2121

README.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
1-
21
# Harness Drone/CIE SonarQube Plugin with Quality Gate
32

4-
This plugin is designed to run SonarQube scans and handle the results and convert it to JUnit Format. It's written in Go and check the report results for status OK.
3+
This plugin is designed to run SonarQube scans, handle the results, and convert them to JUnit format. It's written in Go and checks the report results for status OK.
4+
5+
## Main Features - v2.4.2
6+
7+
- **New Parameter: `sonar_config_file`**
8+
- **Type**: Boolean
9+
- **Description**: If set to true, the plugin will utilize the specified `sonar-project.properties` file for the SonarQube analysis, if it exists.
10+
- **Environment Variable**: `PLUGIN_SONAR_CONFIG_FILE`
11+
- **Allowed Values**: `"true"`, `"false"`
12+
13+
- **New Parameter: `sonar_config_file_override`**
14+
- **Type**: Boolean
15+
- **Description**: If set to true, the plugin will use the `sonar-project.properties` file and allow overriding of host, login, and/or project key settings.
16+
- **Environment Variable**: `PLUGIN_SONAR_CONFIG_FILE_OVERRIDE`
17+
- **Allowed Values**: `"true"`, `"false"`
18+
19+
- **New Parameter: `quality_gate_error_exit_code`**
20+
- **Type**: Integer
21+
- **Description**: Specifies the "exit code" error when the quality gate fails. Default is `5`.
22+
- **Environment Variable**: `PLUGIN_QUALITY_GATE_ERROR_EXIT_CODE`
23+
- **Default Value**: `5`
524

625
## Main Features - v2.4.1
726

827
- Sonar CLI downgraded from 6.0.0.4432 to 5.0.1.3006
928
- Execute SonarQube scans and handle the results
1029
- Generate JUnit reports based on the scan results
1130
- Quality Gate status reporting + Metrics
12-
- Skip Scan and only check for quality Gate Status of a specific analysisId or last analysis
13-
- Waiting for Analysis and QualityGate nows skip the wait if set to false - thanks @kangguru
14-
- Added SONAR_SCANNER_OPTS as param, so it transform into this env var during execution for Sonar JVM params - check for the param detail section below
31+
- Skip Scan and only check for Quality Gate status of a specific `analysisId` or last analysis
32+
- Waiting for Analysis and QualityGate now skips the wait if set to false - thanks @kangguru
33+
- Added `SONAR_SCANNER_OPTS` as a parameter, transforming into this env var during execution for Sonar JVM params - check the parameter detail section below
34+
35+
**Note**: Use `branch` and `pr_key` parameters for accurate results matching when skipping the scan.
1536

16-
Obs: USe branch and pr_key params for accuracy results matches when skiping the scan
37+
![Results](https://github.com/drone-plugins/sonarqube-scanner/blob/main/sonar-result-v2.png)
1738

18-
<img src="https://github.com/drone-plugins/sonarqube-scanner/blob/main/sonar-result-v2.png" alt="Results" width="800"/>
1939

2040
### Simple Pipeline example
2141

@@ -26,7 +46,7 @@ Obs: USe branch and pr_key params for accuracy results matches when skiping the
2646
identifier: run_sonar
2747
spec:
2848
connectorRef: account.DockerHubDiego
29-
image: plugins/sonarqube-scanner:v2.4.1
49+
image: plugins/sonarqube-scanner:v2.4.2
3050
reports:
3151
type: JUnit
3252
spec:
@@ -48,7 +68,7 @@ Obs: USe branch and pr_key params for accuracy results matches when skiping the
4868
type: Plugin
4969
spec:
5070
connectorRef: <+input>
51-
image: plugins/sonarqube-scanner:v2.4.1
71+
image: plugins/sonarqube-scanner:v2.4.2
5272
reports:
5373
type: JUnit
5474
spec:
@@ -88,7 +108,7 @@ spec:
88108
identifier: check_sonar
89109
spec:
90110
connectorRef: account.DockerHubDiego
91-
image: plugins/sonarqube-scanner:v2.4.1
111+
image: plugins/sonarqube-scanner:v2.4.2
92112
reports:
93113
type: JUnit
94114
spec:
@@ -110,7 +130,7 @@ name: default
110130

111131
steps:
112132
- name: perform-code-analysis
113-
image: plugins/sonarqube-scanner:v2.4.1
133+
image: plugins/sonarqube-scanner:v2.4.2
114134
settings:
115135
sonar_host: http://34.100.11.50
116136
sonar_token:
@@ -212,8 +232,32 @@ steps:
212232
- `SONAR_SCANNER_OPTS`: pass any Sonar JVM param as env var during execution.
213233
- Example: `"SONAR_SCANNER_OPTS": "--add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED"`
214234

235+
- **`sonar_config_file`**:
236+
- **Type**: Boolean
237+
- **Description**: Use `sonar-project.properties` if available.
238+
- **Environment Variable**: `PLUGIN_SONAR_CONFIG_FILE`
239+
- **Allowed Values**: `"true"`, `"false"`
240+
241+
- **`sonar_config_file_override`**:
242+
- **Type**: Boolean
243+
- **Description**: Use `sonar-project.properties` if available and allow overriding of host, login, and/or project key settings.
244+
- **Environment Variable**: `PLUGIN_SONAR_CONFIG_FILE_OVERRIDE`
245+
- **Allowed Values**: `"true"`, `"false"`
246+
247+
- **`quality_gate_error_exit_code`**:
248+
- **Type**: Integer
249+
- **Description**: Specifies the "exit code" error when the quality gate fails. Default is `5`.
250+
- **Environment Variable**: `PLUGIN_QUALITY_GATE_ERROR_EXIT_CODE`
251+
- **Default Value**: `5`
252+
215253
Detail Informations/tutorials Parameteres: [DOCS.md](DOCS.md).
216254

255+
### Sonar Token
256+
257+
Use Global analysis tokens
258+
259+
<img src="https://github.com/drone-plugins/sonarqube-scanner/blob/main/global-analysis-token.png" alt="Plugin Configuration" width="400"/>
260+
217261
### Build Process
218262

219263
build go binary file:

global-analysis-token.png

37.2 KB
Loading

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ go 1.18
55
require (
66
github.com/joho/godotenv v1.5.1
77
github.com/pelletier/go-toml/v2 v2.0.9
8-
github.com/pelletier/go-toml/v2 v2.0.9
98
github.com/sirupsen/logrus v1.9.3
109
github.com/urfave/cli/v2 v2.25.7
11-
github.com/urfave/cli/v2 v2.25.7
1210
)
1311

1412
require (
15-
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
13+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
14+
github.com/pelletier/go-toml v1.9.5 // indirect
1615
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1716
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
17+
github.com/urfave/cli v1.22.15 // indirect
1818
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
1919
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
44
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
55
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
66
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
7+
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
8+
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
79
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
810
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
911
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -29,16 +31,20 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
2931
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3032
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
3133
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
34+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
3235
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3336
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
3437
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3538
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3639
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
3740
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
41+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3842
github.com/urfave/cli v1.22.9 h1:cv3/KhXGBGjEXLC4bH0sLuJ9BewaAbpk5oyMOveu4pw=
3943
github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
4044
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
4145
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
46+
github.com/urfave/cli v1.22.15 h1:nuqt+pdC/KqswQKhETJjo7pvn/k4xMUxgW6liI7XpnM=
47+
github.com/urfave/cli v1.22.15/go.mod h1:wSan1hmo5zeyLGBjRJbzRTNk8gwoYa2B9n4q9dmRIc0=
4248
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
4349
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
4450
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=

0 commit comments

Comments
 (0)