-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathJenkinsfile
More file actions
242 lines (204 loc) · 8.25 KB
/
Jenkinsfile
File metadata and controls
242 lines (204 loc) · 8.25 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
def checkoutSourceCode() {
checkout([$class: 'GitSCM', branches: [[name: "refs/remotes/origin/feature/test"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'raghu-github', url: 'https://github.com/Raghupatik/java-test.git']]])
}
pipeline {
agent any
options {
//Disable concurrentbuilds for the same job
disableConcurrentBuilds()
// Add timestamps to console log
timestamps()
// skipDefaultCheckout true
}
parameters {
choice(
// choices are a string of newline separated values
choices: ['All', 'Checkout', 'Build', 'Artifactory', 'DeployTestServer', 'TestTestServer', 'RollbackTestServer', 'DeployProduction', 'TestProduction', 'RollbackProduction'],
description: 'Choose stage to run. All stages will be run by default.',
name: 'Stage',
)
string(name: 'RollbackVersion', defaultValue: '', description: 'Input the Rollback version to be deployed')
}
environment {
S3_BUCKET = 'raghu-jenkinsartifacts'
LAMBDA_FUNCTION = 'lambq2-test'
PROD_LAMBDA_FUNCTION = 'lambq2-prod'
}
stages {
stage('Build') {
agent { label 'jenkins-slave' }
when {
expression { params.Stage == 'All' || params.Stage == 'Build' }
}
steps {
echo "Stage: ${params.Stage}"
script {
echo 'Build'
sh 'mvn clean install'
}
}
}
stage('Push to S3') {
agent { label 'jenkins-slave' }
when {
expression { params.Stage == 'All' || params.Stage == 'Artifactory' }
}
steps {
script {
echo 'Push to S3'
ARTIFACTID = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
JARNAME = ARTIFACTID+'-'+VERSION+'.jar'
echo "JARNAME: ${JARNAME}"
//sh 'cp target/priceavailability-api*.war target/priceavailability-api.war'
withAWS(profile:'aviall', region:'us-east-1') {
s3Upload(file:"target/${JARNAME}", bucket:"${S3_BUCKET}", path:"${JARNAME}")
}
// sh "aws s3 cp target/${JARNAME} s3://$S3_BUCKET"
currentBuild.result = 'SUCCESS'
}
}
}
stage("SonarQube analysis") {
agent any
when {
anyOf {
branch 'feature/*'
branch 'fetaure/*'
}
}
steps {
withSonarQubeEnv('Sonar') {
sh 'mvn sonar:sonar'
}
}
}
stage("Quality Gate") {
steps {
script {
try {
timeout(time: 5, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
catch (Exception ex) {
}
}
}
}
stage('Clean Workspace') {
agent { label 'jenkins-slave'}
steps {
cleanWs()
}
}
stage ('Run postman test on Test Server') {
when {
expression { params.Stage == 'All' || params.Stage == 'TestTestServer' }
}
parallel {
stage ('Run postman test on Tomcat Test') {
agent { label 'master'}
steps {
//checkoutSourceCode()
script {
retry(2) {
try {
echo 'Run postman on Test server'
sh 'newman run src/test/java/com/example/resources/postman-testscript.json -d src/test/java/com/example/resources/tomcat-env.json -k'
currentBuild.result = 'SUCCESS'
} catch(Exception err) {
// currentBuild.result = 'FAILURE'
}
}
}
}
}
}
}
stage('Deploy to Lambda - Test') {
agent { label 'master' }
when {
expression { params.Stage == 'All' || params.Stage == 'DeployTestServer' }
}
steps {
script {
echo 'Deploy to Test'
sh "aws lambda update-function-code --function-name $LAMBDA_FUNCTION --region us-east-1 --s3-bucket $S3_BUCKET --s3-key ${JARNAME}"
currentBuild.result = 'SUCCESS'
}
}
}
stage ('Rollback Test if deployment failed') {
agent { label 'master'}
when {
expression { params.Stage == 'RollbackTestServer' && params.RollbackVersion != '' }
}
steps {
script {
if (currentBuild.result == 'FAILURE' || params.Stage == 'RollbackTestServer') {
echo "We need rollback to version: ${RollbackVersion}"
VERSION = "${RollbackVersion}"
ARTIFACTID = readMavenPom().getArtifactId()
JARNAME = ARTIFACTID+'-'+ "${RollbackVersion}"+'.jar'
sh "aws lambda update-function-code --function-name $LAMBDA_FUNCTION --region us-east-1 --s3-bucket $S3_BUCKET --s3-key ${JARNAME}"
}
}
}
}
stage('Release to Prod') {
agent none
when {
expression { params.Stage == 'All' || params.Stage == 'DeployProduction' }
}
steps {
echo 'Release to Prod'
script {
if (env.BRANCH_NAME == "main") {
timeout(time: 10) {
input(
message: "Approve for deployment version ${VERSION} on Production?",
ok: "Yes",
submitter: "admin"
)
}
}
else {
echo 'Prod Release Skipped'
}
}
}
}
stage('Deploy to Prod') {
agent { label 'jenkins-slave'}
when {
expression { params.Stage == 'All' || params.Stage == 'DeployProduction' }
}
steps {
script {
if (env.BRANCH_NAME == "main") {
echo 'Deploy to Prod'
retry(3) {
sleep 10
}
sh "aws lambda update-function-code --function-name $PROD_LAMBDA_FUNCTION --s3-bucket $S3_BUCKET --s3-key ${JARNAME}"
}
}
}
}
stage ('Notify when all stages are done') {
agent { label 'master' }
steps {
script {
if (currentBuild.result == 'FAILURE') {
mail (to: 'ragupathi.kommidi@gmail.com', subject: "ERROR: The job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) failed, rollback to last stable version", body: "Please go to ${env.BUILD_URL} for the detail")
echo "ERROR: The job ${env.JOB_NAME} (${env.BUILD_NUMBER}) failed, rollback to last stable version"
} else {
echo "The deployment process is done, new version is deployed successfully"
mail (to: 'ragupathi.kommidi@gmail.com', subject: "SUCCESS: The job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) is done, new version ${VERSION} is deployed successfully", body: "Please go to ${env.BUILD_URL} for the detail")
}
}
}
}
}
}