-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
386 lines (379 loc) · 14.5 KB
/
Jenkinsfile
File metadata and controls
386 lines (379 loc) · 14.5 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Define the detectJavaVersion function outside of the pipeline block
def detectJavaVersion() {
def javaVersionOutput = sh(script: 'java -version 2>&1', returnStatus: false, returnStdout: true).trim()
def javaVersionMatch = javaVersionOutput =~ /openjdk version "(\d+\.\d+)/
if (javaVersionMatch) {
def javaVersion = javaVersionMatch[0][1]
if (javaVersion.startsWith("1.8")) {
return '8'
} else if (javaVersion.startsWith("11")) {
return '11'
} else if (javaVersion.startsWith("17")) {
return '17'
} else {
error("Unsupported Java version detected: ${javaVersion}")
}
} else {
error("Java version information not found in output.")
}
}
pipeline {
agent any
environment {
SONARCLOUD = 'Sonarcloud'
SNYK_INSTALLATION = 'snyk@latest'
SNYK_TOKEN = 'Snyk'
DOCKER_REGISTRY_CREDENTIALS = 'Docker_Server'
DOCKER_IMAGE = 'docker:latest' // Must name Docker image here. This is based on the format for repo/project:latest
DOCKER_TOOL = 'Docker' // Mention same name in Jenkins in ID field
DOCKER_URL = 'https://index.docker.io/v1/' // Change not mandatory
KUBE_CONFIG = 'kubernetes'
}
stages {
stage('Clean Workspace') {
steps {
cleanWs()
}
}
stage('Git-Checkout') {
steps {
checkout scm
}
}
// /opt/sonar-scanner-5.0.1.3006-linux/bin/sonar-scanner
stage('Compile and Run Sonar Analysis') {
steps {
script {
withSonarQubeEnv(credentialsId: SONARCLOUD, installationName: 'Sonarcloud') {
try {
if (fileExists('pom.xml')) {
sh 'mvn verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectkey=bugflow'
} else if (fileExists('package.json')) {
sh "${sonarscanner} -Dsonar.organization=jenkeen -Dsonar.projectKey=jenkeen_testjs -Dsonar.sources=. -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=b93f1a1d762901a17859d64d23dcf8221570b2b2"
} else if (fileExists('go.mod')) {
sh "${sonarscanner} -Dsonar.organization=jenkeen -Dsonar.projectKey=jenkeen_go -Dsonar.sources=. -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=b93f1a1d762901a17859d64d23dcf8221570b2b2"
} else if (fileExists('Gemfile')) {
sh "${sonarscanner} -Dsonar.organization=jenkeen -Dsonar.projectKey=jenkeen_ruby -Dsonar.sources=. -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=b93f1a1d762901a17859d64d23dcf8221570b2b2"
} else if (fileExists('requirements.txt')) {
sh "${sonarscanner} -Dsonar.organization=jenkeen -Dsonar.projectKey=jenkeen_python -Dsonar.sources=. -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=b93f1a1d762901a17859d64d23dcf8221570b2b2"
} else {
currentBuild.result = 'FAILURE'
pipelineError = true
error("Unsupported application type: No compatible build steps available.")
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
pipelineError = true
error("Error during Sonar analysis: ${e.message}")
}
}
}
}
}
stage('snyk_analysis') {
steps {
script {
echo 'Testing...'
try {
snykSecurity(
snykInstallation: SNYK_INSTALLATION,
snykTokenId: SNYK_TOKEN,
failOnIssues: false,
monitorProjectOnBuild: true,
additionalArguments: '--all-projects --d'
)
} catch (Exception e) {
currentBuild.result = 'FAILURE'
pipelineError = true
error("Error during snyk_analysis: ${e.message}")
}
}
}
}
stage('Detect and Set Java') {
steps {
script {
try {
def javaVersion = detectJavaVersion()
tool name: "Java_${javaVersion}", type: 'jdk'
sh 'java --version'
} catch (Exception e) {
currentBuild.result = 'FAILURE'
pipelineError = true
error("Error during Java version detection: ${e.message}")
}
}
}
}
stage('Frontend Build and Test') {
steps {
script {
try {
if (fileExists('package.json')) {
//sh 'npm install --force'
//sh 'npm test'
} else {
echo 'No package.json found, skipping Frontend build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
pipelineError = true
error("Error during Frontend build and test: ${e.message}")
}
}
}
}
stage('Java Spring Boot Build and Test') {
steps {
script {
try {
if (fileExists('pom.xml')) {
sh 'mvn clean package'
sh 'mvn test'
} else {
// If pom.xml doesn't exist, print a message and continue
echo 'No pom.xml found, skipping Java Spring Boot build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during Java Spring Boot build and test: ${e.message}")
}
}
}
}
stage('.NET Build and Test') {
steps {
script {
try {
if (fileExists('YourSolution.sln')) {
sh 'dotnet build'
sh 'dotnet test'
} else {
// If YourSolution.sln doesn't exist, print a message and continue
echo 'No YourSolution.sln found, skipping .NET build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during .NET build and test: ${e.message}")
}
}
}
}
stage('PHP Build and Test') {
steps {
script {
try {
if (fileExists('composer.json')) {
sh 'composer install'
sh 'phpunit'
} else {
// If composer.json doesn't exist, print a message and continue
echo 'No composer.json found, skipping PHP build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during PHP build and test: ${e.message}")
}
}
}
}
stage('iOS Build and Test') {
steps {
script {
try {
if (fileExists('YourProject.xcodeproj')) {
xcodebuild(buildDir: 'build', scheme: 'YourScheme')
} else {
// If YourProject.xcodeproj doesn't exist, print a message and continue
echo 'No YourProject.xcodeproj found, skipping iOS build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during iOS build and test: ${e.message}")
}
}
}
}
stage('Android Build and Test') {
steps {
script {
try {
if (fileExists('build.gradle')) {
sh './gradlew build'
sh './gradlew test'
} else {
// If build.gradle doesn't exist, print a message and continue
echo 'No build.gradle found, skipping Android build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during Android build and test: ${e.message}")
}
}
}
}
stage('Ruby on Rails Build and Test') {
steps {
script {
try {
// Check if Gemfile.lock exists
if (fileExists('Gemfile.lock')) {
sh 'bundle install' // Install Ruby gem dependencies
sh 'bundle exec rake db:migrate' // Run database migrations
sh 'bundle exec rails test' // Run Rails tests (adjust as needed)
} else {
// If Gemfile.lock doesn't exist, print a message and continue
echo 'No Gemfile.lock found, skipping Ruby on Rails build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during Ruby on Rails build and test: ${e.message}")
}
}
}
}
stage('Flask Build and Test') { // To build and run a Python Flask Framework Application
steps {
script {
try {
if (fileExists('app.py')) {
sh 'pip install -r requirements.txt' // Install dependencies
sh 'python -m unittest discover' // Run Flask unit tests
} else {
// If app.py doesn't exist, print a message and continue
echo 'No app.py found, skipping Flask build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during Flask build and test: ${e.message}")
}
}
}
}
stage('Django Build and Test') { // To build and run a Python Django Framework Application
steps {
script {
try {
if (fileExists('manage.py')) {
sh 'pip install -r requirements.txt' // Install dependencies
sh 'python manage.py migrate' // Run Django migrations
sh 'python manage.py test' // Run Django tests
} else {
// If manage.py doesn't exist, print a message and continue
echo 'No manage.py found, skipping Django build and test.'
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during Django build and test: ${e.message}")
}
}
}
}
stage('Rust Build and Test') { //To build and run a Rust Application
steps {
script {
try {
if (fileExists('Cargo.toml')) { // Check if Cargo.toml file exists
env.RUST_BACKTRACE = 'full' // Set the RUST_BACKTRACE environment variable to full for better error messages
sh 'cargo build' // Build the Rust project
sh 'cargo test' // Run the Rust tests
} else {
// If Cargo.toml doesn't exist, print a message and continue
echo "No Cargo.toml file found. Skipping Rust build and test."
}
} catch (Exception e) {
// Set the build result to FAILURE and print an error message
currentBuild.result = 'FAILURE'
error("Error during Rust build and test: ${e.message}")
}
}
}
}
stage('Ruby Sinatra Build and Test') { //To build and run a Ruby Application
steps {
script {
try {
if (fileExists('app.rb')) { // Check if app.rb file exists
sh 'gem install bundler' // Install Bundler
sh 'bundle install' // Use bundle exec to ensure gem dependencies are isolated
sh 'bundle exec rake test' // Run the Sinatra tests using Rake
} else {
// If app.rb doesn't exist, print a message and continue
echo "No app.rb file found. Skipping Ruby Sinatra build and test."
}
} catch (Exception e) {
// Set the build result to FAILURE and print an error message
currentBuild.result = 'FAILURE'
error("Error during Ruby Sinatra build and test: ${e.message}")
}
}
}
}
stage('Build and Push Docker Image') {
steps {
script {
try {
if (fileExists('Dockerfile')) {
withDockerRegistry(credentialsId: DOCKER_REGISTRY_CREDENTIALS, toolName: DOCKER_TOOL, url: DOCKER_URL) {
def dockerImage = docker.build(DOCKER_IMAGE, ".")
// Push the built Docker image
dockerImage.push()
}
} else {
echo "Dockerfile not found. Skipping Docker image build and push."
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
pipelineError = true
echo "Error during Docker image build and push: ${e.message}"
}
}
}
}
stage('Trivy Scan') {
steps {
script {
def trivyInstalled = sh(script: 'command -v trivy', returnStatus: true) == 0
def imageName = DOCKER_IMAGE
if (trivyInstalled) {
sh "trivy image --format table ${imageName}"
} else {
// Run trivy using Docker
sh "docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image --format table ${imageName}"
}
}
}
}
stage('Kubernetes Deployment') {
steps {
script {
def configFile = 'deployment.yaml'
def namespace = 'anacart' // Replace 'your-namespace' with your actual namespace
if (fileExists(configFile)) {
kubernetesDeploy(configs: configFile, kubeconfigId: KUBE_CONFIG, namespace: namespace)
} else {
error("Error: $configFile does not exist")
currentBuild.result = 'FAILURE'
pipelineError = true
}
}
}
}
stage('Run DAST Using ZAP') {
steps {
script {
try {
def targetURL = "http://192.168.58.2:32765" // Use the obtained service URL as the target URL. Can be modified in deployment.yaml file
def zapCommand = "zaproxy -cmd -quickurl ${targetURL}"
//sh(zapCommand)
sh("echo zap_report.html")
//archiveArtifacts artifacts: 'zap_report.html'
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Error during ZAP DAST: ${e.message}")
}
}
}
}
}
}