@@ -6,7 +6,6 @@ class Trivy implements Serializable {
6
6
private Docker docker
7
7
private String trivyVersion
8
8
private String trivyDirectory = " trivy"
9
- private String trivyReportFilenameWithoutExtension = trivyDirectory+ " /trivyReport"
10
9
11
10
Trivy (script , Docker docker = new Docker (script), String trivyVersion = " 0.57.1" ) {
12
11
this . script = script
@@ -30,20 +29,20 @@ class Trivy implements Serializable {
30
29
*/
31
30
boolean scanImage (
32
31
String imageName ,
33
- String trivyReportFilename = " ${ this.script.env.WORKSPACE} /trivy/trivyReport.json" ,
34
32
String additionalFlags = " " ,
35
33
String severityLevel = TrivySeverityLevel . CRITICAL ,
36
- String strategy = TrivyScanStrategy . FAIL
34
+ String strategy = TrivyScanStrategy . FAIL ,
35
+ String trivyReportFile = " trivy/trivyReport.json"
37
36
) {
38
37
int exitCode
39
38
docker. image(" aquasec/trivy:${ trivyVersion} " )
40
39
.mountJenkinsUser()
41
40
.mountDockerSocket()
42
41
.inside(" -v ${ script.env.WORKSPACE} /.trivy/.cache:/root/.cache/" ) {
43
- // Write result to $trivyReportFilename in json format (--format json), which can be converted in the saveFormattedTrivyReport function
42
+ // Write result to $trivyReportFile in json format (--format json), which can be converted in the saveFormattedTrivyReport function
44
43
// Exit with exit code 1 if vulnerabilities are found
45
44
script. sh(" mkdir -p " + trivyDirectory)
46
- exitCode = script. sh(script : " trivy image --exit-code 10 --exit-on-eol 10 --format ${ TrivyScanFormat.JSON} -o ${ trivyReportFilename } --severity ${ severityLevel} ${ additionalFlags} ${ imageName} " , returnStatus : true )
45
+ exitCode = script. sh(script : " trivy image --exit-code 10 --exit-on-eol 10 --format ${ TrivyScanFormat.JSON} -o ${ trivyReportFile } --severity ${ severityLevel} ${ additionalFlags} ${ imageName} " , returnStatus : true )
47
46
}
48
47
switch (exitCode) {
49
48
case 0 :
@@ -62,18 +61,22 @@ class Trivy implements Serializable {
62
61
* Save the Trivy scan results as a file with a specific format
63
62
*
64
63
* @param format The format of the output file (@see TrivyScanFormat)
64
+ * @param formattedTrivyReportFilename The file name your report files should get, without file extension. E.g. "ubuntu24report"
65
+ * @param trivyReportFile The "trivyReportFile" parameter you used in the "scanImage" function, if it was set
65
66
*/
66
- void saveFormattedTrivyReport (String format = TrivyScanFormat . HTML , String trivyReportFilename = " ${ script.env.WORKSPACE } / trivy/trivyReport.json" ) {
67
+ void saveFormattedTrivyReport (String format = TrivyScanFormat . HTML , String formattedTrivyReportFilename = " trivyReport " , String trivyReportFile = " trivy/trivyReport.json" ) {
67
68
String fileExtension
68
69
String formatString
70
+ String trivyDirectory = " trivy/"
69
71
switch (format) {
70
72
case TrivyScanFormat . HTML :
71
73
formatString = " template --template \" @/contrib/html.tpl\" "
72
74
fileExtension = " html"
73
75
break
74
76
case TrivyScanFormat . JSON :
75
- // Result file is already in JSON format
76
- return
77
+ formatString = " json"
78
+ fileExtension = " json"
79
+ break
77
80
case TrivyScanFormat . TABLE :
78
81
formatString = " table"
79
82
fileExtension = " txt"
@@ -84,8 +87,8 @@ class Trivy implements Serializable {
84
87
}
85
88
docker. image(" aquasec/trivy:${ trivyVersion} " )
86
89
.inside(" -v ${ script.env.WORKSPACE} /.trivy/.cache:/root/.cache/" ) {
87
- script. sh(script : " trivy convert --format ${ formatString} --output ${ trivyReportFilenameWithoutExtension } .${ fileExtension} ${ trivyReportFilename } " )
90
+ script. sh(script : " trivy convert --format ${ formatString} --output ${ trivyDirectory }${ formattedTrivyReportFilename } .${ fileExtension} ${ trivyReportFile } " )
88
91
}
89
- script. archiveArtifacts artifacts : " ${ trivyReportFilenameWithoutExtension } .*" , allowEmptyArchive : true
92
+ script. archiveArtifacts artifacts : " ${ trivyDirectory }${ formattedTrivyReportFilename } .*" , allowEmptyArchive : true
90
93
}
91
94
}
0 commit comments