-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
138 lines (122 loc) · 4.16 KB
/
Jenkinsfile
File metadata and controls
138 lines (122 loc) · 4.16 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
pipeline {
agent any
tools{
jdk 'jdk'
maven 'maven3'
}
environment {
SCANNER_HOME= tool 'SonarScanner'
}
stages {
stage('Git Checkout') {
steps {
git 'https://github.com/ashenjay/Java-App.git'
}
}
stage('Compile') {
steps {
sh "mvn compile"
}
}
stage('Test') {
steps {
sh "mvn test"
}
}
stage('Trivy FS Scan') {
steps {
sh "trivy fs --format table -o fs.html . "
}
}
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('Sonar-Scanner') {
sh'''$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=SMS-app -Dsonar.projectKey=SMS-app \
-Dsonar.java.binaries=target'''
}
}
}
stage('Build') {
steps {
sh "mvn package"
}
}
stage('Publish Artifacts') {
steps {
withMaven(globalMavenSettingsConfig: 'maven-settings', jdk: 'jdk', maven: 'maven3', mavenSettingsConfig: '', traceability: true) {
sh "mvn deploy"
}
}
}
stage('Docker Build') {
steps {
script{
withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
sh "docker build -t ashenjay/sms-app:latest ."
}
}
}
}
stage('Trivy Image Scan') {
steps {
sh "trivy image --format table -o image.html ashenjay/sms-app:latest "
}
}
stage('Docker Push Image') {
steps {
script{
withDockerRegistry(credentialsId: 'docker-cred', toolName: 'docker') {
sh "docker push ashenjay/sms-app:latest"
}
}
}
}
stage('K8-Deploy') {
steps {
withKubeConfig(caCertificate: '', clusterName: 'terraform-cluster', contextName: '', credentialsId: 'k8-cred', namespace: 'webapps', restrictKubeConfigAccess: false, serverUrl: 'https://A0262F4B6742037BA3499192324E1FEA.gr7.us-east-1.eks.amazonaws.com') {
sh "kubectl apply -f deployment-service.yml"
sleep 20
}
}
}
stage('K8-Verify the Deployment') {
steps {
withKubeConfig(caCertificate: '', clusterName: 'terraform-cluster', contextName: '', credentialsId: 'k8-cred', namespace: 'webapps', restrictKubeConfigAccess: false, serverUrl: 'https://A0262F4B6742037BA3499192324E1FEA.gr7.us-east-1.eks.amazonaws.com') {
sh "kubectl get pods"
sh "kubectl get svc"
}
}
}
}
post {
always {
script {
def jobName = env.JOB_NAME
def buildNumber = env.BUILD_NUMBER
def pipelineStatus = currentBuild.result ?: 'UNKNOWN'
def bannerColor = pipelineStatus.toUpperCase() == 'SUCCESS' ? 'green' : 'red'
def body = """
<html>
<body>
<div style="border: 4px solid ${bannerColor}; padding: 10px;">
<h2>${jobName} - Build ${buildNumber}</h2>
<div style="background-color: ${bannerColor}; padding: 10px;">
<h3 style="color: white;">Pipeline Status: ${pipelineStatus.toUpperCase()}</h3>
</div>
<p>Check the <a href="${BUILD_URL}">console output</a>.</p>
</div>
</body>
</html>
"""
emailext(
subject: "${jobName} - Build ${buildNumber} - ${pipelineStatus.toUpperCase()}",
body: body,
to: 'ashenjanath@gmail.com',
from: 'jenkins@example.com',
replyTo: 'jenkins@example.com',
mimeType: 'text/html'
)
}
}
}
}