Skip to content

Commit 34487a3

Browse files
version 2.0.0 - quality gate independency
1 parent 965adc0 commit 34487a3

File tree

5 files changed

+159
-32
lines changed

5 files changed

+159
-32
lines changed

README.md

Lines changed: 148 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
# Harness Drone/CIE SonarQube Plugin with Quality Gate
22

3-
The plugin of Harness Drone/CIE to integrate with SonarQube (previously called Sonar), which is an open source code quality management platform and check the report results for status OK.
3+
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.
44

5-
<img src="https://github.com/drone-plugins/sonarqube-scanner/blob/main/SonarResult.png" alt="Results" width="800"/>
5+
## Main Features - 2.0.0
66

7+
- Execute SonarQube scans and handle the results
8+
- Generate JUnit reports based on the scan results
9+
- Quality Gate status reporting + Metrics
10+
- Skip Scan and only check for quality Gate Status of a specific analysisId or last analysis
711

8-
Detail Informations/tutorials Parameteres: [DOCS.md](DOCS.md).
9-
10-
11-
### Build process
12-
build go binary file:
13-
`GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o sonarqube-scanner`
12+
<img src="https://github.com/drone-plugins/sonarqube-scanner/blob/main/sonar-result-v2.png" alt="Results" width="800"/>
1413

15-
build docker image
16-
`docker build -t plugins/sonarqube-scanner .`
17-
18-
19-
### Testing the docker image:
20-
```commandline
21-
docker run --rm \
22-
-e DRONE_REPO=test \
23-
-e PLUGIN_SOURCES=. \
24-
-e SONAR_HOST=http://localhost:9000 \
25-
-e SONAR_TOKEN=60878847cea1a31d817f0deee3daa7868c431433 \
26-
-e PLUGIN_SONAR_KEY=project-sonar \
27-
-e PLUGIN_SONAR_NAME=project-sonar \
28-
plugins/sonarqube-scanner
29-
```
3014

31-
### Pipeline example
15+
### Simple Pipeline example
3216
```yaml
3317
- step:
3418
type: Plugin
3519
name: "Check Sonar "
36-
identifier: Check_Sonar
20+
identifier: run_sonar
3721
spec:
3822
connectorRef: account.DockerHubDiego
39-
image: plugins/sonarqube-scanner:linux-amd64
23+
image: plugins/sonarqube-scanner:2.0.0
4024
reports:
4125
type: JUnit
4226
spec:
4327
paths:
44-
- "**/**/*.xml"
28+
- "**/**/*.xml"
4529
privileged: false
4630
settings:
4731
sonar_host: http://34.100.11.50
@@ -58,12 +42,12 @@ docker run --rm \
5842
type: Plugin
5943
spec:
6044
connectorRef: <+input>
61-
image: plugins/sonarqube-scanner:linux-amd64
45+
image: plugins/sonarqube-scanner:2.0.0
6246
reports:
6347
type: JUnit
6448
spec:
6549
paths:
66-
- "**/**/*.xml"
50+
- "**/**/*.xml"
6751
settings:
6852
sonar_key: <+input>
6953
sonar_name: <+input>
@@ -90,6 +74,141 @@ spec:
9074

9175
```
9276
77+
### Skip Scan - Pipeline example
78+
```yaml
79+
- step:
80+
type: Plugin
81+
name: "Check Sonar "
82+
identifier: run_sonar
83+
spec:
84+
connectorRef: account.DockerHubDiego
85+
image: plugins/sonarqube-scanner:2.0.0
86+
reports:
87+
type: JUnit
88+
spec:
89+
paths:
90+
- "**/**/*.xml"
91+
privileged: false
92+
settings:
93+
sonar_host: https://sonarcloud.io
94+
sonar_token: 66778345cea1a31d817f0deee3daa7868c431433
95+
sonar_name: sonarqube-scanner
96+
sonar_key: sonarqube-scanner
97+
skip_scan: true
98+
```
99+
100+
### Configuration Parameters
101+
102+
- `key`: The project key in SonarQube.
103+
- Example: `"key": "your-project-key"`
104+
- `name`: The project name in SonarQube.
105+
- Example: `"name": "your-project-name"`
106+
- `host`: The URL of the SonarQube server.
107+
- Example: `"host": "https://sonarqube.example.com"`
108+
- `token`: The token for authenticating with the SonarQube server.
109+
- Example: `"token": "your-sonarqube-token"`
110+
- `ver`: The version of the project.
111+
- Example: `"ver": "1.0.0"`
112+
- `branch`: The branch of the project.
113+
- Example: `"branch": "master"`
114+
- `timeout`: The timeout for the Sonar scanner.
115+
- Example: `"timeout": "300"`
116+
- `sources`: The paths for the source directories, separated by commas.
117+
- Example: `"sources": "src"`
118+
- `inclusions`: The files to be included in the analysis.
119+
- Example: `"inclusions": "*.go, *.java"`
120+
- `exclusions`: The files to be excluded from the analysis.
121+
- Example: `"exclusions": "*.test.go"`
122+
- `level`: The logging level.
123+
- Example: `"level": "INFO"`
124+
- `showProfiling`: Enable profiling during analysis.
125+
- Example: `"showProfiling": "true"`
126+
- `branchAnalysis`: Execute branch analysis.
127+
- Example: `"branchAnalysis": "true"`
128+
- `usingProperties`: Use `sonar-project.properties`.
129+
- Example: `"usingProperties": "true"`
130+
- `binaries`: Java binaries.
131+
- Example: `"binaries": "/path/to/binaries"`
132+
- `quality`: Quality Gate.
133+
- Example: `"quality": "OK"`
134+
- `quality_gate_enabled`: Stop pipeline if Sonar quality gate conditions are not met.
135+
- Example: `"quality_gate_enabled": "true"`
136+
- `qualitygate_timeout`: Number in seconds for timeout.
137+
- Example: `"qualitygate_timeout": "300"`
138+
- `artifact_file`: Artifact file location that will be generated by the plugin. This file will include information of Docker images that are uploaded by the plugin.
139+
- Example: `"artifact_file": "artifact.json"`
140+
- `output-file`: Output file location that will be generated by the plugin. This file will include information that is exported by the plugin.
141+
- Example: `"output-file": "/path/to/output/file"`
142+
- `javascript_icov_reportPath`: Sonar JavaScript Icov Report Path parameter.
143+
- Example: `"javascript_icov_reportPath": "/path/to/icov/report"`
144+
- `java_coverage_plugin`: Sonar Java Plugin parameter.
145+
- Example: `"java_coverage_plugin": "jacoco"`
146+
- `jacoco_report_path`: Sonar Jacoco Report Path parameter.
147+
- Example: `"jacoco_report_path": "/path/to/jacoco/report"`
148+
- `ssl_keystore_pwd`: Java Keystore Password.
149+
- Example: `"ssl_keystore_pwd": "your-keystore-password"`
150+
- `cacerts_location`: Java Truststore Location (cacerts).
151+
- Example: `"cacerts_location": "/path/to/cacerts"`
152+
- `junit_reportpaths`: JUnit Report Paths.
153+
- Example: `"junit_reportpaths": "/path/to/junit/reports"`
154+
- `source_encoding`: Source Encoding.
155+
- Example: `"source_encoding": "UTF-8"`
156+
- `tests`: Sonar Tests.
157+
- Example: `"tests": "/path/to/tests"`
158+
- `java_test`: Java Test.
159+
- Example: `"java_test": "/path/to/java/test"`
160+
- `pr_key`: PR Key.
161+
- Example: `"pr_key": "your-pr-key"`
162+
- `pr_branch`: PR Branch.
163+
- Example: `"pr_branch": "your-pr-branch"`
164+
- `pr_base`: PR Base.
165+
- Example: `"pr_base": "your-pr-base"`
166+
- `coverage_exclusion`: Sonar coverage exclusions.
167+
- Example: `"coverage_exclusion": "*.test.go"`
168+
- `java_source`: Sonar Java source.
169+
- Example: `"java_source": "1.8"`
170+
- `java_libraries`: Sonar Java libraries.
171+
- Example: `"java_libraries": "/path/to/libraries"`
172+
- `surefire_reportsPath`: Sonar surefire reportsPath.
173+
- Example: `"surefire_reportsPath": "/path/to/surefire/reports"`
174+
- `typescript_lcov_reportPaths`: Sonar TypeScript lcov reportPaths.
175+
- Example: `"typescript_lcov_reportPaths": "/path/to/typescript/lcov/reports"`
176+
- `verbose`: Sonar verbose.
177+
- Example: `"verbose": "true"`
178+
- `custom_jvm_params`: JVM parameters. Use comma for multiple parameters.
179+
- Example: `"custom_jvm_params": "-Dsonar.java.source='value_you_want'"`
180+
- `taskid`: Sonar analysis taskId.
181+
- Example: `"taskid": "your-task-id"`
182+
- `skip_scan`: Skip Sonar analysis scan - get last analysis automatically.
183+
- Example: `"skip_scan": true`
184+
185+
Detail Informations/tutorials Parameteres: [DOCS.md](DOCS.md).
186+
187+
### Build Process
188+
189+
build go binary file:
190+
```
191+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o sonarqube-scanner
192+
```
193+
194+
build docker image
195+
```
196+
docker build -t plugins/sonarqube-scanner .
197+
```
198+
199+
200+
### Testing the docker image:
201+
```commandline
202+
docker run --rm \
203+
-e DRONE_REPO=test \
204+
-e PLUGIN_SOURCES=. \
205+
-e SONAR_HOST=http://localhost:9000 \
206+
-e SONAR_TOKEN=60878847cea1a31d817f0deee3daa7868c431433 \
207+
-e PLUGIN_SONAR_KEY=project-sonar \
208+
-e PLUGIN_SONAR_NAME=project-sonar \
209+
plugins/sonarqube-scanner
210+
```
211+
93212
<img src="https://github.com/drone-plugins/sonarqube-scanner/blob/main/Sonar-CIE.png" alt="Plugin Configuration" width="400"/>
94213

95214
<img src="https://github.com/drone-plugins/sonarqube-scanner/blob/main/SonarResultConsole.png" alt="Console Results" width="800"/>

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX
4040
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
4141
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
4242
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
43+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
4344
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
4445
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4546
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ func main() {
244244
Value: "",
245245
EnvVar: "PLUGIN_TASKID",
246246
},
247+
cli.BoolFlag{
248+
Name: "skip_scan",
249+
Usage: "Skip the SonarQube scan",
250+
EnvVar: "PLUGIN_SKIP_SCAN",
251+
},
247252
}
248253
app.Run(os.Args)
249254
}
@@ -289,6 +294,7 @@ func run(c *cli.Context) {
289294
Verbose: c.String("verbose"),
290295
CustomJvmParams: c.String("custom_jvm_params"),
291296
TaskId: c.String("taskid"),
297+
SkipScan: c.Bool("skip_scan"),
292298
},
293299
Output: Output{
294300
OutputFile: c.String("output-file"),

plugin.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type (
7575
Verbose string
7676
CustomJvmParams string
7777
TaskId string
78+
SkipScan bool
7879
}
7980
Output struct {
8081
OutputFile string // File where plugin output are saved
@@ -569,8 +570,8 @@ func (p Plugin) Exec() error {
569570
fmt.Printf("\n")
570571

571572
status := ""
572-
573-
if p.Config.TaskId != "" {
573+
574+
if p.Config.TaskId != "" or !p.Config.SkipScan {
574575
fmt.Printf("Skipping Scan...")
575576
fmt.Printf("\n")
576577
fmt.Printf("\n")
@@ -692,7 +693,7 @@ func displayQualityGateStatus(status string, qualityEnabled bool) {
692693
fmt.Printf("| QUALITY GATE STATUS REPORT |\n")
693694
fmt.Println("----------------------------------------------")
694695

695-
if status == "SUCCESS" {
696+
if status == "OK" {
696697
fmt.Printf("| STATUS | \033[32m%s\033[0m |\n", status)
697698
} else {
698699
fmt.Printf("| STATUS | \033[31m%s\033[0m |\n", status)

sonar-result-v2.png

516 KB
Loading

0 commit comments

Comments
 (0)