-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
183 lines (163 loc) · 4.77 KB
/
Copy pathJenkinsfile
File metadata and controls
183 lines (163 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: buildkit
image: moby/buildkit:rootless
args:
- --oci-worker-no-process-sandbox
resources:
requests:
ephemeral-storage: "4Gi"
securityContext:
runAsUser: 1000
runAsGroup: 1000
seccompProfile:
type: Unconfined
appArmorProfile:
type: Unconfined
volumeMounts:
- mountPath: /home/user/.local/share/buildkit
name: buildkit-cache
- name: jnlp
resources:
requests:
ephemeral-storage: "2Gi"
volumes:
- name: buildkit-cache
emptyDir: {}
'''
}
}
environment {
MAIN_BRANCH = 'main'
IMAGE_SCAN_REPORT = 'wiz-image-scan.json'
DIR_SCAN_REPORT = 'wiz-dir-scan.json'
}
stages {
stage('Checkout') {
steps {
git url: 'https://github.com/devpro/terraform-backend-mongodb.git',
branch: env.BRANCH_NAME ?: MAIN_BRANCH
script {
def props = readFile('Directory.Build.props')
def matcher = (props =~ /<VersionPrefix>(\d+\.\d+)\.\d+<\/VersionPrefix>/)
if (matcher.find()) {
env.VERSION = matcher.group(1)
} else {
env.VERSION = 'ci'
}
matcher = null
def commitHash = env.GIT_COMMIT ? env.GIT_COMMIT.take(7) : env.BRANCH_NAME
env.TARBALL = "tfbackend-mongodb-${env.VERSION}.${commitHash}.tar"
}
}
}
stage('Build Image') {
steps {
container('buildkit') {
sh """
buildctl build \
--frontend dockerfile.v0 \
--local context=. \
--local dockerfile=src/WebApi \
--output type=oci,dest=${TARBALL}
"""
}
}
}
stage('Download Wiz CLI') {
steps {
sh 'curl -o wizcli https://downloads.wiz.io/v1/wizcli/latest/wizcli-linux-amd64'
sh 'chmod +x wizcli'
}
}
stage('Wiz Image Scan') {
steps {
withWizCredentials {
sh "./wizcli scan container-image ${TARBALL} --json-output-file ${IMAGE_SCAN_REPORT}"
}
}
post {
always {
archiveArtifacts artifacts: IMAGE_SCAN_REPORT, allowEmptyArchive: true
}
}
}
stage('Post PR Comment') {
when { changeRequest() }
steps {
script {
def d = readJSON file: IMAGE_SCAN_REPORT
def verdict = d.status?.verdict ?: 'UNKNOWN'
def report = d.reportUrl ?: 'https://app.wiz.io'
def critical = 0
def high = 0
def medium = 0
def sbomArtifacts = d.result?.vulnerableSBOMArtifactsByNameVersion
if (sbomArtifacts) {
sbomArtifacts.each { artifact ->
def severities = artifact?.vulnerabilityFindings?.severities
if (severities) {
critical += (severities.criticalCount ?: 0)
high += (severities.highCount ?: 0)
medium += (severities.mediumCount ?: 0)
}
}
}
def prCommentBody = """## Wiz Image Scan — ${verdict}
| Category | Count |
|---|---|
| 🔴 Critical | ${critical} |
| 🟠 High | ${high} |
| 🟡 Medium | ${medium} |
[View Detail Report in Wiz](${report})
_[Jenkins build](${env.BUILD_URL})_"""
withCredentials([usernamePassword(
credentialsId: 'github-devpro-org-scan',
usernameVariable: 'GH_USER',
passwordVariable: 'GH_TOKEN'
)]) {
writeJSON file: 'comment.json', json: [body: prCommentBody]
def prNumber = env.CHANGE_ID
sh """
curl -s -u "\${GH_USER}:\${GH_TOKEN}" \
-H "Content-Type: application/json" \
-X POST \
-d @comment.json \
"https://api.github.com/repos/devpro/terraform-backend-mongodb/issues/${prNumber}/comments"
"""
}
}
}
}
stage('Wiz Dir Scan') {
when {
environment name: 'BRANCH_NAME', value: MAIN_BRANCH
}
steps {
withWizCredentials {
sh "./wizcli scan dir . --json-output-file ${DIR_SCAN_REPORT}"
}
}
post {
always {
archiveArtifacts artifacts: DIR_SCAN_REPORT, allowEmptyArchive: true
}
}
}
}
}
def withWizCredentials(Closure body) {
withCredentials([usernamePassword(
credentialsId: 'wiz-credentials',
usernameVariable: 'WIZ_CLIENT_ID',
passwordVariable: 'WIZ_CLIENT_SECRET'
)]) {
body()
}
}