-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
110 lines (110 loc) · 2.43 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
pipeline {
agent any
tools {
maven 'MAVEN_HOME'
}
environment {
// DPR = Docker Private Repo
DPR_CRED = credentials('nexus-creds')
}
stages {
// stage 1
stage('Unit Testing') {
steps{
sh 'mvn test'
}
}
// stage 2
stage('Integration Testing'){
steps{
sh 'mvn verify -DskipUnitTests'
}
}
// stage 3
stage('Maven Building'){
steps{
sh 'mvn clean install'
}
}
// stage 4
stage('SonarQube Analysis'){
steps{
withSonarQubeEnv(credentialsId: 'sonarqube-key', installationName: 'sonarqube-server') {
sh 'mvn clean package sonar:sonar'
}
}
}
// stage 5
stage('Quality Gate Status'){
steps{
waitForQualityGate abortPipeline: false, credentialsId: 'sonarqube-key'
}
}
// stage 6
stage('Upload jar file to Nexus'){
steps{
script{
def readPom = readMavenPom file: 'pom.xml'
def nexusRepo = readPom.version.endsWith("SNAPSHOT") ? "demo-counter-app-snapshot" : "demo-counter-app-release"
nexusArtifactUploader artifacts:
[
[
artifactId: 'springboot',
classifier: '',
file: 'target/counterApp.jar',
type: 'jar'
]
],
credentialsId: 'nexus-creds',
groupId: 'com.example',
nexusUrl: '192.168.18.8:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: nexusRepo,
version: "${readPom.version}"
}
}
}
// stage 7
stage('Docker Image Build'){
steps{
sh 'docker build -t $JOB_NAME:v1.$BUILD_ID .'
}
}
// stage 8
stage('Tag Image with Nexus Repository'){
steps{
script{
sh 'docker image tag $JOB_NAME:v1.$BUILD_ID 192.168.18.8:8082/$JOB_NAME:v1.$BUILD_ID'
sh 'docker image tag $JOB_NAME:v1.$BUILD_ID 192.168.18.8:8082/$JOB_NAME:latest'
}
}
}
// stage 9
stage('Push Docker Image To Private Nexus Repository'){
steps{
script{
sh 'docker login -u ${DPR_CRED_USR} -p ${DPR_CRED_PSW} 192.168.18.8:8082'
sh 'docker image push 192.168.18.8:8082/$JOB_NAME:v1.$BUILD_ID'
sh 'docker image push 192.168.18.8:8082/$JOB_NAME:latest'
}
}
}
// stage 10
stage('Remove Old Docker Image'){
steps{
script{
sh "docker rmi \$(docker images | grep demo-counter-app | awk '{print \$3}') --force"
}
}
}
// stage 11
stage('Deploy Project Container'){
steps{
script{
sh 'docker run -d -p 8888:8888 --name counterApp 192.168.18.8:8082/demo-counter-app:latest'
}
}
}
}
}