-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
150 lines (144 loc) · 5.45 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
#!groovy
@Library('sample-petstore-declarative-libs') _
pipeline {
agent any
environment {
IMAGE = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
BUILD_DATE=sh (returnStdout: true,
script: 'date -u +"%Y-%m-%dT%H:%M:%SZ"'
).trim()
MAJOR_VERSION="0.0"
DOCKER_REPO="leftshiftit"
}
stages {
stage('Setup Env vars') {
steps {
script {
try {
env.VCS_REF = sh (
returnStdout: true,
script: 'git rev-parse HEAD'
).trim()
env.STAGING_VERSION = "${MAJOR_VERSION}." +
sh (returnStdout: true,
script: 'git rev-list --count HEAD'
).trim()
currentBuild.displayName="${env.STAGING_VERSION}"
currentBuild.description="${env.VCS_REF}"
} catch (Exception e) {
echo "Exception caught: ${e}"
currentBuild.result = 'FAILURE'
}
}
sendNotifications 'STARTED'
echo "VCS_REF: ${env.VCS_REF}"
echo "BUILD_DATE: ${env.BUILD_DATE}"
echo "STAGING_VERSION: ${env.STAGING_VERSION}"
}
}
stage('Build WAR') {
agent {
docker {
reuseNode true //reuse the workspace on the agent defined at top-level\
image 'maven:3.5.0-jdk-8'
args '-v $HOME/.m2:/root/.m2'
}
}
steps {
//sh "mvn versions:update-parent -DallowSnapshots=true -DparentVersion=[17.5.0-SNAPSHOT,17.6.0-SNAPSHOT] -B -U"
sh "mvn versions:set versions:update-child-modules -DnewVersion=${env.STAGING_VERSION}-SNAPSHOT -B -U"
sh 'mvn versions:use-latest-versions -DallowSnapshots=true -Dincludes=com.fanniemae.amtm -B -U'
sh 'mvn -B -f pom.xml clean package'
}
post {
success {
junit(allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml')
}
}
}
stage('Quality Analysis') {
steps {
sh 'echo I will do some analysis one day!'
}
}
stage('Build Image') {
steps {
script {
try {
// Build the image. Create a global reference to the Image Name
env.DOCKER_IMAGE_NAME="${env.DOCKER_REPO}/${env.IMAGE}:${env.STAGING_VERSION}"
def myImage = docker.build("${env.DOCKER_IMAGE_NAME}", "--build-arg VCS_REF=${env.VCS_REF.take(6)} --build-arg BUILD_DATE=${env.BUILD_DATE} .")
echo "Built image ${myImage.id}"
} catch (Exception e) {
echo "Exception caught: ${e}"
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
sendNotifications "New Image built - ${env.DOCKER_IMAGE_NAME}"
}
}
}
stage('Test Image') {
steps {
script {
try {
docker.image("${env.DOCKER_IMAGE_NAME}").inside {
sh 'echo "Tests passed"'
}
} catch (Exception e) {
echo "Exception caught: ${e}"
currentBuild.result = 'FAILURE'
}
}
}
}
stage('Push Image') {
steps {
timeout(time:2, unit:'DAYS') {
input message: 'Image look good?', ok: 'Push to Docker Hub', submitter: 'admin'
}
script {
try {
docker.withRegistry("${env.DOCKER_HUB_REGISTRY_URL}", 'dockerhub-creds') {
docker.image("${env.DOCKER_IMAGE_NAME}").push()
}
} catch (Exception e) {
echo "Exception caught: ${e}"
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
sendNotifications "New Image pushed - ${env.DOCKER_HUB_REGISTRY_URL}/${env.DOCKER_IMAGE_NAME}"
}
}
}
}
post {
// Always runs. And it runs before any of the other post conditions.
always {
sendNotifications currentBuild.result
// Let's wipe out the workspace before we finish!
deleteDir()
}
success {
echo "Success"
// mail(from: "[email protected]",
// to: "[email protected]",
// subject: "Build ${STAGING_VERSION} passed.",
// body: "Nothing to see here")
}
failure {
echo "Failure"
// mail(from: "[email protected]",
// to: "[email protected]",
// subject: "Build ${STAGING_VERSION} failed.",
// body: "Nothing to see here")
}
}
}