-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile3
More file actions
177 lines (128 loc) · 4.38 KB
/
Copy pathJenkinsfile3
File metadata and controls
177 lines (128 loc) · 4.38 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
pipeline {
agent {
docker {
image 'docker:20.10.7-dind'
image 'maven:3.9.8-eclipse-temurin-21'
args '--privileged -v /var/run/docker.sock:/var/run/docker.sock'
}
}
tools{
maven 'maven'
}
environment {
SONAR_TOKEN = credentials('SONAR_TOKEN')
}
stages {
stage('Install Docker CLI') {
steps {
script {
// Installing Docker CLI inside the Jenkins agent container
sh '''
apk update && \
apk add docker-cli
'''
}
}
}
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/mekaizen/sdtest.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('SAST Scan') {
steps {
sh '''
# mvn -Dmaven.test.failure.ignore verify sonar:sonar \
mvn -Dmaven.test.failure.ignore verify org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar \
-Dsonar.login=$SONAR_TOKEN \
-Dsonar.projectKey=sdlc-test \
-Dsonar.host.url=http://172.18.0.3:9000 \
-Dsonar.ws.timeout=180
'''
}
}
stage('Start Application') {
steps {
script {
// Start the application for testing locally on port 8081
sh '''
nohup java -jar target/sdlc-test-0.0.1-SNAPSHOT.jar --server.port=8084 > app.log 2>&1 &
'''
// Increase sleep time to ensure application fully starts
sleep 10
}
}
}
stage('Verify Application Running') {
steps {
script {
// Check if the application is running and accessible on port 8081
sh '''
curl -I http://localhost:8084 || {
echo "Application is not running!"
exit 1
}
'''
}
}
}
stage('Check Application Logs') {
steps {
script {
// Output the application logs
sh 'cat app.log'
}
}
}
stage('ZAP Security Scan') {
steps {
script {
//Run ZAP as a Docker container
sh '''
docker run --network host zaproxy/zap-stable zap-baseline.py -t http://localhost:8084 -r zap-report.html
'''
}
}
}
stage('Post ZAP Scan Actions') {
steps {
// Archive the ZAP report
archiveArtifacts artifacts: 'zap-report.html', allowEmptyArchive: true
//Optionally, you can use warnings plugins or parse the report for vulnerabilities.
}
}
}
post {
success {
echo 'Build, test, and ZAP scan passed'
}
failure {
echo 'Build failed'
}
always {
script {
sh '''
# docker container prune -f
# Check if the process exists before killing
pid=$(ps aux | grep 'sdlc-test.jar' | grep -v 'grep' | awk '{print $2}')
if [ -n "$pid" ]; then
kill $pid
else
echo "Process not found, skipping kill."
fi
# kill $(ps aux | grep 'sdlc-test.jar' | awk '{print $2}')
'''
}
}
}
}