forked from kodekloudhub/devsecops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
284 lines (247 loc) · 8.13 KB
/
Jenkinsfile
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@Library('slack') _
/////// ******************************* Code for fectching Failed Stage Name ******************************* ///////
import io.jenkins.blueocean.rest.impl.pipeline.PipelineNodeGraphVisitor
import io.jenkins.blueocean.rest.impl.pipeline.FlowNodeWrapper
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
import org.jenkinsci.plugins.workflow.actions.ErrorAction
// Get information about all stages, including the failure cases
// Returns a list of maps: [[id, failedStageName, result, errors]]
@NonCPS
List<Map> getStageResults( RunWrapper build ) {
// Get all pipeline nodes that represent stages
def visitor = new PipelineNodeGraphVisitor( build.rawBuild )
def stages = visitor.pipelineNodes.findAll{ it.type == FlowNodeWrapper.NodeType.STAGE }
return stages.collect{ stage ->
// Get all the errors from the stage
def errorActions = stage.getPipelineActions( ErrorAction )
def errors = errorActions?.collect{ it.error }.unique()
return [
id: stage.id,
failedStageName: stage.displayName,
result: "${stage.status.result}",
errors: errors
]
}
}
// Get information of all failed stages
@NonCPS
List<Map> getFailedStages( RunWrapper build ) {
return getStageResults( build ).findAll{ it.result == 'FAILURE' }
}
/////// ******************************* Code for fectching Failed Stage Name ******************************* ///////
pipeline {
agent any
environment {
deploymentName = "devsecops"
containerName = "devsecops-container"
serviceName = "devsecops-svc"
imageName = "siddharth67/numeric-app:${GIT_COMMIT}"
applicationURL="http://devsecops-demo.eastus.cloudapp.azure.com"
applicationURI="/increment/99"
}
stages {
// stage('Build Artifact - Maven') {
// steps {
// sh "mvn clean package -DskipTests=true"
// archive 'target/*.jar'
// }
// }
// stage('Unit Tests - JUnit and JaCoCo') {
// steps {
// sh "mvn test"
// }
// }
// stage('Mutation Tests - PIT') {
// steps {
// sh "mvn org.pitest:pitest-maven:mutationCoverage"
// }
// }
// stage('SonarQube - SAST') {
// steps {
// withSonarQubeEnv('SonarQube') {
// sh "mvn sonar:sonar \
// -Dsonar.projectKey=numeric-application \
// -Dsonar.host.url=http://devsecops-demo.eastus.cloudapp.azure.com:9000"
// }
// timeout(time: 2, unit: 'MINUTES') {
// script {
// waitForQualityGate abortPipeline: true
// }
// }
// }
// }
// stage('Vulnerability Scan - Docker') {
// steps {
// parallel(
// "Dependency Scan": {
// sh "mvn dependency-check:check"
// },
// "Trivy Scan":{
// sh "bash trivy-docker-image-scan.sh"
// },
// "OPA Conftest":{
// sh 'docker run --rm -v $(pwd):/project openpolicyagent/conftest test --policy opa-docker-security.rego Dockerfile'
// }
// )
// }
// }
// stage('Docker Build and Push') {
// steps {
// withDockerRegistry([credentialsId: "docker-hub", url: ""]) {
// sh 'printenv'
// sh 'sudo docker build -t siddharth67/numeric-app:""$GIT_COMMIT"" .'
// sh 'docker push siddharth67/numeric-app:""$GIT_COMMIT""'
// }
// }
// }
// stage('Vulnerability Scan - Kubernetes') {
// steps {
// parallel(
// "OPA Scan": {
// sh 'docker run --rm -v $(pwd):/project openpolicyagent/conftest test --policy opa-k8s-security.rego k8s_deployment_service.yaml'
// },
// "Kubesec Scan": {
// sh "bash kubesec-scan.sh"
// },
// "Trivy Scan": {
// sh "bash trivy-k8s-scan.sh"
// }
// )
// }
// }
// stage('K8S Deployment - DEV') {
// steps {
// parallel(
// "Deployment": {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "bash k8s-deployment.sh"
// }
// },
// "Rollout Status": {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "bash k8s-deployment-rollout-status.sh"
// }
// }
// )
// }
// }
// stage('Integration Tests - DEV') {
// steps {
// script {
// try {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "bash integration-test.sh"
// }
// } catch (e) {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "kubectl -n default rollout undo deploy ${deploymentName}"
// }
// throw e
// }
// }
// }
// }
// stage('OWASP ZAP - DAST') {
// steps {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh 'bash zap.sh'
// }
// }
// }
// stage('Prompte to PROD?') {
// steps {
// timeout(time: 2, unit: 'DAYS') {
// input 'Do you want to Approve the Deployment to Production Environment/Namespace?'
// }
// }
// }
// stage('K8S CIS Benchmark') {
// steps {
// script {
// parallel(
// "Master": {
// sh "bash cis-master.sh"
// },
// "Etcd": {
// sh "bash cis-etcd.sh"
// },
// "Kubelet": {
// sh "bash cis-kubelet.sh"
// }
// )
// }
// }
// }
// stage('K8S Deployment - PROD') {
// steps {
// parallel(
// "Deployment": {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "sed -i 's#replace#${imageName}#g' k8s_PROD-deployment_service.yaml"
// sh "kubectl -n prod apply -f k8s_PROD-deployment_service.yaml"
// }
// },
// "Rollout Status": {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "bash k8s-PROD-deployment-rollout-status.sh"
// }
// }
// )
// }
// }
// stage('Integration Tests - PROD') {
// steps {
// script {
// try {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "bash integration-test-PROD.sh"
// }
// } catch (e) {
// withKubeConfig([credentialsId: 'kubeconfig']) {
// sh "kubectl -n prod rollout undo deploy ${deploymentName}"
// }
// throw e
// }
// }
// }
// }
stage('Testing Slack - 1') {
steps {
sh 'exit 0'
}
}
stage('Testing Slack - Error Stage') {
steps {
sh 'exit 0'
}
}
}
post {
// always {
// junit 'target/surefire-reports/*.xml'
// jacoco execPattern: 'target/jacoco.exec'
// pitmutation mutationStatsFile: '**/target/pit-reports/**/mutations.xml'
// dependencyCheckPublisher pattern: 'target/dependency-check-report.xml'
// publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: 'owasp-zap-report', reportFiles: 'zap_report.html', reportName: 'OWASP ZAP HTML Report', reportTitles: 'OWASP ZAP HTML Report'])
// //Use sendNotifications.groovy from shared library and provide current build result as parameter
// //sendNotification currentBuild.result
// }
success {
script {
/* Use slackNotifier.groovy from shared library and provide current build result as parameter */
env.failedStage = "none"
env.emoji = ":white_check_mark: :tada: :thumbsup_all:"
sendNotification currentBuild.result
}
}
failure {
script {
//Fetch information about failed stage
def failedStages = getFailedStages( currentBuild )
env.failedStage = failedStages.failedStageName
env.emoji = ":x: :red_circle: :sos:"
sendNotification currentBuild.result
}
}
}
}