-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathJenkinsfile
More file actions
258 lines (253 loc) · 10.1 KB
/
Jenkinsfile
File metadata and controls
258 lines (253 loc) · 10.1 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
/* This script will provide basic idea to create jenkins file and deploy application on deployment environment.
Deployment environment can be anything like Docker, kubernetes or cloud.
Create 'deploy.properties' file at same location and save all the build and deployment properties. This file will be refered by this pipeline for build and deployment. */
def workspace;
def props='';
def tagName="""${JOB_NAME}-${BUILD_TIMESTAMP}"""
def branchName;
def commit_username;
def commit_Email;
def appDeployProcess;
def imageName;
def envMessage='';
node{
stage('Checkout Code')
{
try
{
checkout scm
props = readProperties file: """deploy.properties"""
workspace = pwd ()
branchName=sh(returnStdout: true, script: 'git symbolic-ref --short HEAD').trim()
commit_username=sh(returnStdout: true, script: '''username=$(git log -1 --pretty=%ae)
echo ${username%@*} ''').trim();
commit_Email=sh(returnStdout: true, script: '''Email=$(git log -1 --pretty=%ae)
echo $Email''').trim();
echo commit_username
echo commit_Email
echo branchName
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Checkout Code", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Checkout Code", "", commit_Email)
throw e
}
catch (error) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Checkout Code", props['build.JIRAprojectid'], props['build.JIRAissuetype'], commit_Email, props['build.JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Checkout Code", "", commit_Email)
throw error
}
}
stage ('Check Environment')
{
try
{
//check if deployment environment is up and running
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Check Environment", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Check Environment", "", commit_Email)
throw e
}
}
stage ('Static Code Analysis')
{
try{
sh """echo ${workspace}"""
def scannerHome = tool 'sonar-runner';
withSonarQubeEnv('Dockersonar')
{
staticCodeAnalysis(scannerHome, """${Dockersonar}""")
}
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Static Code Analysis", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Static Code Analysis", "", commit_Email)
throw e
}
}
stage ('Build')
{
try
{
sh """mvn clean build"""
}
catch (e)
{
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Build", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Build", "", commit_Email)
throw e
}
}
stage ('Unit Test Execution')
{
try {
sh """mvn clean test"""
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Unit Testing", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Unit Testing", "", commit_Email)
throw e
}
}
stage ('Code Coverage')
{
try
{
def scannerHome = tool 'sonar-runner';
withSonarQubeEnv('Dockersonar')
{
codeCoverage(scannerHome, """${Dockersonar}""")
}
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Code Coverage", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Code Coverage", "", commit_Email)
throw e
}
}
stage ('Create Docker Image')
{
try {
imageName="""${props['docker.registry']}/${props['deploy.app']}:${props['api.version']}"""
sh """mvn package
sudo docker build -t ${imageName} ."""
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Create Package", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Create Package", "", commit_Email)
throw e
}
}
stage ('Push Image to Docker Registry')
{
try {
sh """sudo docker push ${imageName}"""
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Moving Image to Docker Registry", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Moving Image to Docker Registry", "", commit_Email)
throw e
}
}
stage ('Deploy to Environment')
{
try
{
def helmChartValue = readYaml file: "helmchart/${JOB_NAME}/values.yaml"
helmChartValue.microservice.port = props['app.port'].replaceAll("\'","");
helmChartValue.microservice.image = "$imageName"
helmChartValue.microservice.namespace = """${props['kubernetesnamespace']}"""
helmChartValue.microservice.configServerURI = """${props['ConfigserverURL']}"""
fileOperations([fileDeleteOperation(excludes: '', includes: "helmchart/${JOB_NAME}/values.yaml")])
writeYaml file: "helmchart/${JOB_NAME}/values.yaml", data: helmChartValue
//you can use any deployment tool here to deploy helm chart on kubernetes cluster
//or
//you deploy container on docker environment
sh """ssh user@deploymentserverhost
cd helmchart/${JOB_NAME}
helm install --name ${props['deploy.app']} . """
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Deploy", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Deploy", "", commit_Email)
throw e
}
}
stage ('Validate Microservice Deployment')
{
try {
sleep 120
def chkmicroservice=sh(returnStdout: true, script: """curl -s http://${props['environment.URL']}:${props['app.port']}/health | jq '.status' | tr -d '"' """).trim();
def chkDeployment='';
if(chkmicroservice != "UP")
{
chkDeployment = chkDeployment + """\n Microservice - ${JOB_NAME} connection failed (Status:${chkmicroservice})"""
}
if (chkDeployment != "")
{
error ("""\n Warning:\n Microservice deployment is unstable ${chkDeployment} \n """)
}
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Validate Microservice Deployment", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Validate Microservice Deployment", "", commit_Email)
throw e
}
catch (error) {
currentBuild.result='UNSTABLE'
//logJIRATicket(currentBuild.result, "At Stage Validate Microservice Deployment", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result, "At Stage Validate Microservice Deployment", "", commit_Email)
echo """${error.getMessage()}"""
//throw e
}
}
stage ('Log JIRA Ticket for Code Promotion')
{
try {
logJIRATicket('SUCCESS', "At Stage Log JIRA Ticket", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
}
catch (e) {
currentBuild.result='FAILURE'
logJIRATicket(currentBuild.result, "At Stage Log JIRA Ticket", props['JIRAprojectid'], props['JIRAissuetype'], commit_Email, props['JIRAissuereporter'])
notifyBuild(currentBuild.result ,"At Stage Log JIRA Ticket", """Version tag created with name '${tagName}'. but no JIRA ticket logged.""", commit_Email)
throw e
}
}
notifyBuild(currentBuild.result, "", """Version tag created with name '${tagName}' on '${branchName}' branch \n Build successfull, no JIRA ticket logged. """, commit_Email)
}
def notifyBuild(String buildStatus, String buildFailedAt, String bodyDetails, String commit_Email)
{
buildStatus = buildStatus ?: 'SUCCESS'
def details = """Please find attahcment for log and Check console output at ${BUILD_URL}\n \n "${bodyDetails}"
\n"""
emailext attachLog: true,
notifyEveryUnstableBuild: true,
recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
body: details,
subject: """${buildStatus}: Job ${JOB_NAME} [${BUILD_NUMBER}] ${buildFailedAt}""",
to: """[email protected],${commit_Email}"""
}
def logJIRATicket(String buildStatus, String buildFailedAt, String projectid, String issuetype, String assignTo, String issueReporter)
{
buildStatus = buildStatus ?: 'SUCCESS'
if (buildStatus == 'FAILURE' ){
String Title="""${buildStatus} ${buildFailedAt} OF ${JOB_NAME}[${BUILD_NUMBER}]"""
withEnv(['JIRA_SITE=Localhost']) {
// Look at IssueInput class for more information.
def Issue = [fields: [ project: [id: projectid],
summary: Title,
description: 'New JIRA Created from Jenkins.',
issuetype: [id: issuetype],
assignee: [name: assignTo],
reporter: [name: issueReporter]]]
def Issues = [issueUpdates: [Issue]]
response = jiraNewIssues issues: Issues
echo """${response}"""
}
}
else {
echo "Build is successfull, no JIRA ticket logged."
}
}
def staticCodeAnalysis(String scannerHome, String sonarHosturl)
{
sh """
${scannerHome}/bin/sonar-runner -D sonar.host.url=${sonarHosturl} -D sonar.login=admin -D sonar.password=admin"""
}
def codeCoverage(String scannerHome, String sonarHosturl)
{
sh """
${scannerHome}/bin/sonar-runner -D sonar.host.url=${sonarHosturl} -D sonar.login=admin -D sonar.password=admin -D sonar.java.binaries=target/classes -D sonar.jacoco.reportPaths=target/jacoco.exec"""
}