-
Notifications
You must be signed in to change notification settings - Fork 53
/
Jenkinsfile
188 lines (167 loc) · 5.14 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
def COMMIT
def BRANCH_NAME
def GIT_BRANCH
pipeline
{
agent any
environment
{
AWS_ACCOUNT_ID="930264708953"
AWS_DEFAULT_REGION="us-east-1"
IMAGE_REPO_NAME="mavenwebapp"
REPOSITORY_URI = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_REPO_NAME}"
}
tools
{
maven 'MAVEN_3.8.4'
}
options
{
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '4', daysToKeepStr: '', numToKeepStr: '4')
timestamps()
}
stages
{
stage('Code checkout')
{
steps
{
script
{
checkout([$class: 'GitSCM', branches: [[name: '*/development']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/dev1git/maven-web-application.git']]])
COMMIT = sh (script: "git rev-parse --short=10 HEAD", returnStdout: true).trim()
}
}
}
stage('Build')
{
steps
{
sh "mvn clean package"
}
}
stage('Execute Sonarqube Report')
{
steps
{
withSonarQubeEnv('Sonarqube-Server')
{
sh "mvn sonar:sonar"
}
}
}
stage('Quality Gate Check')
{
steps
{
timeout(time: 1, unit: 'HOURS')
{
waitForQualityGate abortPipeline: true, credentialsId: 'SONARQUBE-CRED'
}
}
}
stage('Nexus Upload')
{
steps
{
script
{
def readPom = readMavenPom file: 'pom.xml'
def nexusrepo = readPom.version.endsWith("SNAPSHOT") ? "wallmart-snapshot" : "wallmart-release"
nexusArtifactUploader artifacts:
[
[
artifactId: "${readPom.artifactId}",
classifier: '',
file: "target/${readPom.artifactId}-${readPom.version}.war",
type: 'war'
]
],
credentialsId: 'Nexus-Cred',
groupId: "${readPom.groupId}",
nexusUrl: '3.82.213.203:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: "${nexusrepo}",
version: "${readPom.version}"
}
}
}
stage('Login to AWS ECR')
{
steps
{
script
{
sh "/usr/local/bin/aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com"
}
}
}
stage('Building Docker Image')
{
steps
{
script
{
sh "docker build . -t ${REPOSITORY_URI}:mavenwebapp-${COMMIT}"
}
}
}
stage('Pushing Docker image into ECR')
{
steps
{
script
{
sh "docker push ${REPOSITORY_URI}:mavenwebapp-${COMMIT}"
}
}
}
stage('Update image in K8s manifest file')
{
steps
{
sh """#!/bin/bash
sed -i 's/VERSION/$COMMIT/g' deployment.yaml
"""
}
}
stage('Deploy to K8s cluster')
{
steps
{
sh '/usr/local/bin/kubectl apply -f deployment.yaml --record=true'
sh """#!/bin/bash
sed -i 's/$COMMIT/VERSION/g' deployment.yaml
"""
}
}
}
post
{
always
{
cleanWs()
}
success
{
slackSend channel: 'build-notifications',color: 'good', message: "started JOB : ${env.JOB_NAME} with BUILD NUMBER : ${env.BUILD_NUMBER} BUILD_STATUS: - ${currentBuild.currentResult} To view the dashboard (<${env.BUILD_URL}|Open>)"
emailext attachLog: true, body: '''BUILD IS SUCCESSFULL - $PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS:
Check console output at $BUILD_URL to view the results.
Regards,
Nithin John George
''', compressLog: true, replyTo: 'njdevops321@gmail.com',
subject: '$PROJECT_NAME - $BUILD_NUMBER - $BUILD_STATUS', to: 'njdevops321@gmail.com'
}
failure
{
slackSend channel: 'build-notifications',color: 'danger', message: "started JOB : ${env.JOB_NAME} with BUILD NUMBER : ${env.BUILD_NUMBER} BUILD_STATUS: - ${currentBuild.currentResult} To view the dashboard (<${env.BUILD_URL}|Open>)"
emailext attachLog: true, body: '''BUILD IS FAILED - $PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS:
Check console output at $BUILD_URL to view the results.
Regards,
Nithin John George
''', compressLog: true, replyTo: 'njdevops321@gmail.com',
subject: '$PROJECT_NAME - $BUILD_NUMBER - $BUILD_STATUS', to: 'njdevops321@gmail.com'
}
}
}