forked from soul-reaper48/JavaVulnerableLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
221 lines (195 loc) · 6.07 KB
/
Jenkinsfile
File metadata and controls
221 lines (195 loc) · 6.07 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
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
@Library('my_shared_library')_
def workspace;
def dockerImage;
def props='';
def targetURL='';
def microserviceName;
def docImg;
def repoName;
def credentials = 'docker-credentials';
def commit_Email;
def archery='';
node {
stage('Checkout Code')
{
try{
checkout scm
workspace = pwd()
sh "ls -lat"
commit_Email=sh(returnStdout: true, script: '''Email=$(git log -1 --pretty=%ae)
echo $Email''').trim();
props = readProperties file: """deploy.properties"""
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage Checkout Code", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('Check-secrets')
{
try{
sh "rm trufflehog || true"
sh "docker run gesellix/trufflehog --json --regex ${props['deploy.gitURL']} > trufflehog"
sh "cat trufflehog"
/*File file = new File("./trufflehog");
if (file.length() == 0)
{
sh "echo Trufflehog didn't find any secrets. We are good to go !"
}*/
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage Check-secrets", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('Source Composition Analysis')
{
try{
snykSecurity projectName: "${props['deploy.microservice']}", severity: 'high', snykInstallation: 'SnykSec', snykTokenId: 'snyk-personal', targetFile: 'pom.xml'
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage Source Composition Analysis", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('SAST')
{
try{
sonarexec "${props['deploy.sonarqubeserver']}"
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage SAST", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('create war')
{
try{
mavenbuildexec "mvn build"
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage create war", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('Create Docker Image')
{
try{
echo 'creating an image'
docImg="${props['deploy.dockerhub']}/${props['deploy.microservice']}"
dockerImage = dockerexec "${docImg}"
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage create war", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('Scan Container Images')
{
try{
sh 'rm anchore_images || true'
sh """echo "docker.io/${docImg}" > anchore_images"""
anchore 'anchore_images'
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage Push Image to Docker Registry", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('Push Image to Docker Registry')
{
try{
docker.withRegistry('https://registry.hub.docker.com','docker-credentials') {
dockerImage.push("${BUILD_NUMBER}")
}
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage Push Image to Docker Registry", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('Config helm')
{
try{
def filename = 'helmchart/values.yaml'
def data = readYaml file: filename
data.image.repository = "${docImg}"
data.image.tag = "$BUILD_NUMBER"
data.service.appPort = "${props['deploy.port']}"
sh "rm -f helmchart/values.yaml"
writeYaml file: filename, data: data
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage Config helm", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('deploy to cluster')
{
try{
//helmdeploy "${props['deploy.microservice']}"
withKubeConfig(credentialsId: 'kubernetes-creds', serverUrl: 'https://35.225.92.51') {
sh """ helm delete --purge ${props['deploy.microservice']} | true"""
helmdeploy "${props['deploy.microservice']}"
sh """sleep 80"""
targetURL = sh(returnStdout: true, script: "kubectl get svc --namespace default ${props['deploy.microservice']} \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}'")
}
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage deploy to cluster", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
stage ('DAST')
{
try{
sh """
echo ${targetURL}
export ARCHERY_HOST=props['deploy.archery']
export TARGET_URL='http://${targetURL}/app'
bash /var/lib/jenkins/archery/zapscan.sh || true
"""
}
catch (error) {
currentBuild.result='FAILURE'
notifyBuild(currentBuild.result, "At Stage DAST", commit_Email, "",props['deploy.archery'])
echo """${error.getMessage()}"""
throw error
}
}
notifyBuild(currentBuild.result, "", commit_Email, "Build successful.",props['deploy.archery'])
}
def notifyBuild(String buildStatus, String buildFailedAt, String commit_Email, String bodyDetails,String archery)
{
buildStatus = buildStatus ?: 'SUCCESS'
def details = """Please find attahcment for archerysec report \"${archery}\" \n and log and Check console output at ${BUILD_URL}\n \n \"${bodyDetails}\"
\n"""
emailext attachLog: true,attachmentsPattern: 'owasp-dependency-check.sh',
notifyEveryUnstableBuild: true,
recipientProviders: [[$class: 'RequesterRecipientProvider']],
body: details,
subject: """${buildStatus}: [${BUILD_NUMBER}] ${buildFailedAt}""",
to: """${commit_Email}"""
slackSend message: """Build Result: ${buildStatus} \n ${BUILD_URL} """
}