-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathJenkinsfile
executable file
·137 lines (125 loc) · 4.62 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
pipeline {
// run on jenkins nodes tha has java 8
agent { label 'jdk8' }
// global env variables
environment {
EMAIL_RECIPIENTS = '[email protected]'
}
stages {
stage('Build With Unit Testing') {
steps {
// Run the maven build
script {
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
echo 'Pulling...' + env.BRANCH_NAME
def mvnHome = tool 'Maven 3.3.9'
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
def pom = readMavenPom file: 'pom.xml'
print pom.version
junit '**/target/surefire-reports/TEST-*.xml'
archive 'target/*.jar'
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
def pom = readMavenPom file: 'pom.xml'
print pom.version
junit '**/target/surefire-reports/TEST-*.xml'
archive 'target/*.jar'
}
}
}
}
stage('Integration Tests') {
// Run the maven build
steps {
script {
def mvnHome = tool 'Maven 3.3.9'
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' verify -Dunit-tests.skip=true"
} else {
bat(/"${mvnHome}\bin\mvn" verify -Dunit-tests.skip=true/)
}
}
}
}
stage('Sonar Check') {
// Run the maven build
steps {
script {
def mvnHome = tool 'Maven 3.3.9'
// replace it with your sonar server
sh "'${mvnHome}/bin/mvn' verify sonar:sonar -Dsonar.host.url=http://romehjava.bc/sonar/ -Dmaven.test.failure.ignore=true"
}
}
}
stage('ITT Deploy Approval and deployment') {
when {
// check if the build was successful
expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' && env.BRANCH_NAME == 'master' }
}
steps {
timeout(time: 3, unit: 'MINUTES') {
//input message:'Approve deployment?', submitter: 'it-ops'
input message: 'Approve deployment?'
}
timeout(time: 2, unit: 'MINUTES') {
// call another jenkins job which do ssh deployment
build job: 'AlertManagerToITT'
echo 'the application is deployed !'
}
}
}
}
post {
// Always runs. And it runs before any of the other post conditions.
always {
// Let's wipe out the workspace before we finish!
deleteDir()
}
success {
sendEmail("Successful");
}
unstable {
sendEmail("Unstable");
}
failure {
sendEmail("Failed");
}
}
// The options directive is for configuration that applies to the whole job.
options {
// For example, we'd like to make sure we only keep 10 builds at a time, so
// we don't fill up our storage!
buildDiscarder(logRotator(numToKeepStr: '10'))
// And we'd really like to be sure that this build doesn't hang forever, so
// let's time it out after an hour.
timeout(time: 20, unit: 'MINUTES')
}
}
@NonCPS
def getChangeString() {
MAX_MSG_LEN = 100
def changeString = ""
echo "Gathering SCM changes"
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
changeString += " - ${truncated_msg} [${entry.author}]\n"
}
}
if (!changeString) {
changeString = " - No new changes"
}
return changeString
}
def sendEmail(status) {
mail(
to: "$EMAIL_RECIPIENTS",
subject: "Build $BUILD_NUMBER - " + status + " (${currentBuild.fullDisplayName})",
body: "Changes:\n " + getChangeString() + "\n\n Check console output at: $BUILD_URL/console" + "\n")
}