-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
141 lines (131 loc) · 4.71 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
def version = "${env.BUILD_NUMBER}"
def app
def containerName = "mmp-backend${version}"
pipeline {
agent any
stages {
stage('Clone Repository') {
/* Let's make sure we have the repository cloned to our workspace */
steps {
cleanWs()
checkout scm
}
}
stage('Build Gradle') {
/* This builds the actual image; synonymous to
* docker build on the command line */
steps {
sh "./gradlew clean"
sh "./gradlew -Pversion=${version} build"
}
}
stage('Test Gradle') {
steps {
sh "./gradlew test"
}
}
stage('SonarQube analysis') {
steps {
withSonarQubeEnv('My SonarQube Server') {
// requires SonarQube Scanner for Gradle 2.1+
// It's important to add --info because of SONARJNKNS-281
sh './gradlew --info sonarqube -Dsonar.projectVersion=${version}'
}
}
}
stage('Publish JAR to Registry') {
steps {
uploadJarToNexus(version)
}
}
stage('Publish Docker to Registry') {
steps {
script {
docker.withServer('tcp://10.0.19.19:2375') {
docker.withRegistry('https://10.0.19.19:18444', 'adminnexus') {
docker.build("ipvs.as/mmp-backend", "--build-arg JAR_FILE=build/libs/mmp-backend-${version}-boot.jar .").push("${version}")
}
}
}
}
}
stage('Test Docker') {
steps {
script{
docker.withServer('tcp://10.0.19.19:2375') {
sh "docker run -d --name ${containerName} \
-p 8080:8080 10.0.19.19:18444/ipvs.as/mmp-backend:${version}"
}
try {
timeout(time: 2, unit: 'MINUTES') {
waitUntil {
try {
def statusCode = sh(script: "curl --max-time 30 -sL -w '%{http_code}' '10.0.19.19:8080/actuator/health' -o /dev/null", returnStdout: true).trim()
return (statusCode == '200')
} catch (exception) {
return false
}
}
}
} catch (err) {
echo "Caught Timeout: ${err}"
currentBuild.result = 'FAILURE'
}
}
}
}
stage('Run Docker') {
steps {
script {
docker.withServer('tcp://10.0.19.17:2375') {
docker.withRegistry('https://10.0.19.19:18444', 'adminnexus') {
sh "docker stop mmp-backend || true && docker rm mmp-backend || true"
sh "docker rmi \$(docker images |grep '10.0.19.19:18444/ipvs.as/mmp-backend') || true"
sh "docker pull 10.0.19.19:18444/ipvs.as/mmp-backend:${version}"
sh "docker run -d --name=mmp-backend --restart=always -p 8080:8080 10.0.19.19:18444/ipvs.as/mmp-backend:${version}"
}
}
}
}
}
stage("Quality Gate") {
steps {
timeout(time: 1, unit: 'HOURS') {
// Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
// true = set pipeline to UNSTABLE, false = don't
// Requires SonarQube Scanner for Jenkins 2.7+
waitForQualityGate abortPipeline: true
}
}
}
}
post('Send Messages and Clean Up') {
success {
slackSend message:"Build Success! Keep it up! - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)", color: 'good'
}
failure {
slackSend message:"Build Failed! You done fucked up! - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)", color: 'danger'
}
fixed {
slackSend message:"Build Fixed! Good job! - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)", color: '#439FE0'
}
cleanup {
script {
docker.withServer('tcp://10.0.19.19:2375') {
sh "docker rm -f ${containerName} || true"
sh "docker rmi -f 10.0.19.19:18444/ipvs.as/mmp-backend:${version} || true"
sh "docker rmi -f ipvs.as/mmp-backend:latest || true"
}
}
}
}
}
def uploadJarToNexus(version) {
nexusArtifactUploader artifacts: [
[artifactId: 'mmp-backend',
file: "build/libs/mmp-backend-${version}-boot.jar",
type: 'jar']
], credentialsId: 'adminnexus', groupId: 'ipvs.as', nexusUrl: '10.0.19.19:8081',
nexusVersion: 'nexus3', protocol: 'http', repository: 'maven-releases',
version: "${version}"
}