forked from poornima4824/Java-CI-CD-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
215 lines (194 loc) · 7.65 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
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
pipeline
{
agent any
environment {
jmeter="/opt/jmeter/bin"
DOCKERHUB_CREDENTIALS=credentials('doc-pri')
AWS_CREDENTIALS= credentials('AWS-cred')
AWS_ACCOUNT_ID="435255528170"
AWS_DEFAULT_REGION="us-east-1"
IMAGE_REPO_NAME="java-app"
REPOSITORY_URI = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_REPO_NAME}"
}
tools
{
maven 'maven'
}
options
{
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '4', daysToKeepStr: '', numToKeepStr: '4')
timestamps()
}
stages
{
stage('Code checkout')
{
steps
{
script
{
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/poornima4824/Java-CI-CD-pipeline.git']]])
COMMIT = sh (script: "git rev-parse --short=10 HEAD", returnStdout: true).trim()
COMMIT_TAG = sh (script: "git tag --contains | head -1", returnStdout: true).trim()
}
}
}
stage('Build')
{
steps
{
sh "mvn clean package"
}
}
// stage('Execute Sonarqube Report')
// {
// steps
// {
// withSonarQubeEnv('sonar')
// {
// sh "mvn sonar:sonar"
// }
// }
// }
// stage('Quality Gate Check')
// {
// steps
// {
// timeout(time: 1, unit: 'HOURS')
// {
// waitForQualityGate abortPipeline: true
// }
// }
// }
stage('Jmeter test') {
steps {
sh "/opt/jmeter/bin/jmeter.sh -Jjmeter.save.saveservice.output_format=xml -n -t src/main/jmeter/Testing.jmx -l MyRun1.jtl"
step([$class: 'ArtifactArchiver', artifacts: '**/*.jtl'])
//sh "/opt/jmeter/bin/jmeter -Jjmeter.save.saveservice.output_format=xml -n -t src/main/jmeter/Testing.jmx -l src/main/jmeter/JMeter.jtl -e -o src/main/jmeter/report/output"
//sh "/opt/jmeter/bin/jmeter.sh -Jjmeter.save.saveservice.output_format=html -Jjmeter.save.saveservice.output_format=xml -n -t src/main/jmeter/Testing.jmx -l src/main/jmeter/MyRun1.jtl"
step([$class: 'ArtifactArchiver', artifacts: '**/*.jtl,**/*.html'])
//sh "mvn clean verify"
}
}
stage('Publish Report') {
steps {
perfReport filterRegex: '', sourceDataFiles: '**/*.jtl,**/*.html'
}
}
// stage("Publish to Nexus Repository Manager") {
// steps {
// script
// {
// def readPom = readMavenPom file: 'pom.xml'
// def nexusrepo = readPom.version.endsWith("SNAPSHOT") ? "maven-snapshots" : "maven-releases"
// nexusArtifactUploader artifacts:
// [
// [
// artifactId: "${readPom.artifactId}",
// classifier: '',
// file: "target/${readPom.artifactId}-${readPom.version}.war",
// type: 'war'
// ]
// ],
// credentialsId: 'nexus',
// groupId: "${readPom.groupId}",
// nexusUrl: '52.91.194.232:8081',
// nexusVersion: 'nexus3',
// protocol: 'http',
// repository: "${nexusrepo}",
// version: "${readPom.version}"
// }
// }
// }
// stage('Docker Build and Tag') {
// steps {
// // sh 'docker build -t java-app:latest .'
// // sh 'docker tag java-app nagapoornima/java-app:latest'
// sh 'docker build -t java-web-app:latest .'
// sh 'docker tag java-web-app boppanaaadhya/java-web-app:latest'
// }
// }
// stage('Login') {
// steps {
// sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
// }
// }
// stage('Push') {
// steps {
// // sh 'docker push nagapoornima/java-app:latest'
// sh 'docker push boppanaaadhya/java-web-app'
// }
// }
stage('Login to AWS ECR')
{
steps
{
script
{
sh "aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 435255528170.dkr.ecr.us-east-1.amazonaws.com"
//sh "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"
// sh "aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 440883647063.dkr.ecr.us-east-1.amazonaws.com"
}
}
}
stage('Building Docker Image')
{
steps
{
script
{
sh "docker build -t java-app ."
//sh "docker build . -t ${REPOSITORY_URI}:LoginWebApp"
}
}
}
stage('Pushing Docker image into ECR')
{
steps
{
script
{
sh "docker tag java-app:latest 435255528170.dkr.ecr.us-east-1.amazonaws.com/java-app:latest"
sh "docker push 435255528170.dkr.ecr.us-east-1.amazonaws.com/java-app:latest"
// sh "docker push ${REPOSITORY_URI}:sample-login-app"
}
}
}
// Stopping Docker containers for cleaner Docker run
stage('stop previous containers') {
steps {
sh 'docker ps -f name=myjavaapp -q | xargs --no-run-if-empty docker container stop'
sh 'docker container ls -a -fname=myjavaapp -q | xargs -r docker container rm'
}
}
stage('Deploy') {
steps{
script {
sh "docker run -d -p 8084:8080 --rm --name myjavaapp java-app:latest"
}
}
}
}
post
{
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. /n
Regards,
Team
''', compressLog: true, replyTo: '[email protected]',
subject: '$PROJECT_NAME - $BUILD_NUMBER - $BUILD_STATUS', to: '[email protected]'
}
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,
Team
''', compressLog: true, replyTo: '[email protected]',
subject: '$PROJECT_NAME - $BUILD_NUMBER - $BUILD_STATUS', to: '[email protected]'
}
}
}