-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
192 lines (176 loc) · 6.31 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
pipeline {
agent {
docker {
image 'docker:latest'
args '-v /var/run/docker.sock:/var/run/docker.sock'
}
}
environment {
VIRTUAL_ENV = 'venv'
ZAP_DOCKER_IMAGE = 'owasp/zap2docker-stable'
ZAP_CONTAINER_NAME = 'zap'
ZAP_WORK_DIR = '/zap/wrk'
REPORT_NAME = 'report.html'
}
parameters {
choice(name: 'SCAN_TYPE', choices: ['Baseline', 'APIS', 'Full'], description: 'Type of OWASP ZAP scan to perform')
string(name: 'TARGET', defaultValue: 'http://localhost:8000', description: 'Target URL to be scanned')
booleanParam(name: 'GENERATE_REPORT', defaultValue: true, description: 'Generate scan report')
}
stages {
stage('Start') {
steps {
echo 'Starting the pipeline...'
checkout scm
}
}
stage('Setup') {
steps {
echo 'Setting up the environment...'
sh 'apk add --no-cache python3 py3-pip'
sh 'python3 -m venv ${VIRTUAL_ENV}'
sh '. ${VIRTUAL_ENV}/bin/activate && pip install -r requirements.txt'
}
}
stage('Build Docker Image') {
steps {
echo 'Building Docker image...'
sh 'docker build -t reservespot .'
}
}
stage('Run Docker Container') {
steps {
echo 'Running Docker container...'
sh 'docker run -d --name django-container -p 8000:8000 reservespot'
sleep 10
}
}
stage('Test') {
steps {
echo 'Running Tests...'
sh 'docker exec django-container python manage.py test'
}
post {
always {
junit 'reports/*.xml'
}
}
}
stage('Deploy') {
steps {
echo 'Deploying to staging/production...'
// Add deployment commands here, if necessary
}
}
stage('Set up OWASP ZAP Docker Container') {
steps {
echo 'Setting up OWASP ZAP Docker container...'
sh "docker pull ${ZAP_DOCKER_IMAGE}"
sh "docker run -u root -d --name ${ZAP_CONTAINER_NAME} -p 8080:8080 ${ZAP_DOCKER_IMAGE} zap.sh -daemon -host 0.0.0.0 -port 8080 -config api.disablekey=true"
sleep 10
}
}
stage('Prepare Working Directory') {
when {
expression { return params.GENERATE_REPORT }
}
steps {
echo 'Preparing working directory in OWASP ZAP Docker container...'
sh "docker exec ${ZAP_CONTAINER_NAME} mkdir -p ${ZAP_WORK_DIR}"
}
}
stage('Scan Target with OWASP ZAP') {
steps {
script {
def scanCommand = ''
switch (params.SCAN_TYPE) {
case 'Baseline':
scanCommand = "docker exec ${ZAP_CONTAINER_NAME} zap-baseline.py -t ${params.TARGET} -r ${ZAP_WORK_DIR}/${REPORT_NAME}"
break
case 'APIS':
scanCommand = "docker exec ${ZAP_CONTAINER_NAME} zap-api-scan.py -t ${params.TARGET} -r ${ZAP_WORK_DIR}/${REPORT_NAME}"
break
case 'Full':
scanCommand = "docker exec ${ZAP_CONTAINER_NAME} zap-full-scan.py -t ${params.TARGET} -r ${ZAP_WORK_DIR}/${REPORT_NAME}"
break
}
sh scanCommand
}
}
}
stage('Copy Report to Workspace') {
when {
expression { return params.GENERATE_REPORT }
}
steps {
echo 'Copying scan report to Jenkins workspace...'
sh "docker cp ${ZAP_CONTAINER_NAME}:${ZAP_WORK_DIR}/${REPORT_NAME} ."
archiveArtifacts artifacts: REPORT_NAME, allowEmptyArchive: true
}
}
stage('SonarQube Analysis') {
steps {
script {
def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
withSonarQubeEnv('SonarQube') {
sh ". ${VIRTUAL_ENV}/bin/activate && ${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=SSDPipeline -Dsonar.sources=."
}
}
}
}
stage('Post Actions') {
steps {
echo 'Performing post actions...'
}
post {
always {
echo 'Stopping and removing OWASP ZAP Docker container...'
sh "docker stop ${ZAP_CONTAINER_NAME}"
sh "docker rm ${ZAP_CONTAINER_NAME}"
echo 'Stopping and removing Django Docker container...'
sh "docker stop django-container"
sh "docker rm django-container"
echo 'Cleaning up...'
cleanWs()
}
}
}
}
agent any
stage('OWASP DependencyCheck') {
steps {
dependencyCheck additionalArguments: '-n --noupdate --format HTML --format XML --suppression suppression.xml', odcInstallation: 'OWASP Dependency-Check Vulnerabilities'
}
}
agent any
stages {
stage('SCM Checkout') {
steps{
git branch: 'main', url: 'https://github.com/lily4499/lil-node-app.git'
}
}
// run sonarqube test
stage('Run Sonarqube') {
environment {
scannerHome = tool 'lil-sonar-tool';
}
steps {
withSonarQubeEnv(credentialsId: 'lil-sonar-credentials', installationName: 'SonarQube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}
post {
always {
echo 'Pipeline completed.'
}
success {
echo 'Pipeline succeeded.'
dependencyCheckPublisher pattern: 'dependency-check-report.xml'
}
failure {
echo 'Pipeline failed.'
}
}
}