-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
199 lines (174 loc) · 4.92 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
@Library('slack') _
pipeline {
agent any
environment {
deploymentName = "devsecops"
containerName = "devsecops-container"
serviceName = "devsecops-svc"
imageName = "shaykube/numeric-app:${GIT_COMMIT}"
applicationURL = "http://devsecops-demodns.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: Unit & Jacoco') {
steps {
sh "mvn test"
}
}
stage('Mutation Tests: PIT') {
steps {
sh "mvn org.pitest:pitest-maven:mutationCoverage"
}
}
stage('SonarQube: SAST') {
environment {
SONARQUBE_TOKEN = credentials('sonarqube_token')
}
steps {
withSonarQubeEnv('SonarQube') {
sh "mvn sonar:sonar -Dsonar.projectKey=numeric-application -Dsonar.host.url=http://devsecops-demodns.eastus.cloudapp.azure.com:9000 -Dsonar.login=${SONARQUBE_TOKEN}"
}
timeout(time: 2, unit: 'MINUTES') {
script {
waitForQualityGate abortPipeline: true
}
}
}
}
stage('Vulnerability Scan: Docker') {
steps {
parallel(
"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 shaykube/numeric-app:""$GIT_COMMIT"" .'
sh 'docker push shaykube/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('Kubernetes Deployment: DEV') {
steps {
parallel(
"Deployment": {
withKubeConfig([credentialsId: 'kubeconfig']) {
sh "bash k8s-deployment.sh"
}
},
"Rollout Status": {
withKubeConfig([credentialsId: 'kubeconfig']) {
sh "bash k8s-deployment.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 'Would you like to give your approval for the deployment to the 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"
}
}
)
}
}
}
post {
always {
junit 'target/surefire-reports/*.xml'
jacoco execPattern: 'target/jacoco.exec'
pitmutation mutationStatsFile: '**/target/pit-reports/**/mutations.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', useWrapperFileDirectly: true])
sendNotification currentBuild.result
}
// success {
// }
// failure {
// }
}
}