-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
165 lines (149 loc) · 5.12 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
openshift.withCluster() {
env.NAMESPACE = openshift.project()
env.POM_FILE = env.BUILD_CONTEXT_DIR ? "${env.BUILD_CONTEXT_DIR}/pom.xml" : "pom.xml"
env.APP_NAME = "${env.JOB_NAME}".replaceAll(/-?pipeline-?/, '').replaceAll(/-?${env.NAMESPACE}-?/, '').replaceAll("/", '')
echo "Starting Pipeline for ${APP_NAME}..."
def projectBase = "${env.NAMESPACE}".replaceAll(/-build/, '')
env.STAGE0 = "${projectBase}-build"
env.STAGE1 = "${projectBase}-dev"
env.STAGE2 = "${projectBase}-stage"
env.STAGE3 = "${projectBase}-prod"
}
pipeline {
// Use Jenkins Maven slave/node/agent
// Jenkins will dynamically provision this as OpenShift Pod
// All the stages and steps of this Pipeline will be executed on this Pod
// After Pipeline completes the Pod is killed so every run will have clean
// workspace
agent {
label 'maven'
}
// Pipeline Stages start here and requires at least one stage
stages {
// Checkout source code
// This is required as Pipeline code is originally checkedout to
// Jenkins Master but this will also pull this same code to this slave
//stage('Git Checkout') {
// steps {
// // Turn off Git's SSL cert check, uncomment if needed
// // sh 'git config --global http.sslVerify false'
// git url: "${APPLICATION_SOURCE_REPO}"
// }
//}
// Run Maven build, skipping tests
stage('Build'){
steps {
sh "./mvnw clean package -DskipTests=true -f ${POM_FILE}"
}
}
// Run Maven unit tests
stage('Unit Test'){
steps {
sh "echo 'Skipping unit tests (for now)'"
//sh "mvn test -f ${POM_FILE}"
}
}
// Build Container Image using the artifacts produced in previous stages
stage('Build Container Image'){
steps {
// Copy the resulting artifacts into common directory
sh """
ls target/*
mkdir -p oc-build/deployments
rm -rf oc-build && mkdir -p oc-build/deployments
for t in \$(echo "jar;war;ear" | tr ";" "\\n"); do
cp -rfv ./target/*.\$t oc-build/deployments/ 2> /dev/null || echo "No \$t files"
done
"""
// Build container image using local Openshift cluster
// Giving all the artifacts to OpenShift Binary Build
// This places your artifacts into right location inside your S2I image
// if the S2I image supports it.
script {
openshift.withCluster() {
openshift.withProject("${STAGE0}") {
openshift.selector("bc", "${APP_NAME}").startBuild("--from-dir=oc-build").logs("-f")
}
}
}
}
}
stage('Promote from Build to Dev') {
steps {
script {
openshift.withCluster() {
openshift.tag("${env.STAGE0}/${env.APP_NAME}:latest", "${env.STAGE1}/${env.APP_NAME}:latest")
}
}
}
}
stage ('Verify Deployment to Dev') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${STAGE1}") {
def dcObj = openshift.selector('dc', env.APP_NAME).object()
def podSelector = openshift.selector('pod', [deployment: "${APP_NAME}-${dcObj.status.latestVersion}"])
podSelector.untilEach {
echo "pod: ${it.name()}"
return it.object().status.containerStatuses[0].ready
}
}
}
}
}
}
// stage('Promote from Dev to Stage') {
// steps {
// script {
// openshift.withCluster() {
// openshift.tag("${env.STAGE1}/${env.APP_NAME}:latest", "${env.STAGE2}/${env.APP_NAME}:latest")
// }
// }
// }
// }
//
// stage ('Verify Deployment to Stage') {
// steps {
// script {
// openshift.withCluster() {
// openshift.withProject("${STAGE2}") {
// def dcObj = openshift.selector('dc', env.APP_NAME).object()
// def podSelector = openshift.selector('pod', [deployment: "${APP_NAME}-${dcObj.status.latestVersion}"])
// podSelector.untilEach {
// echo "pod: ${it.name()}"
// return it.object().status.containerStatuses[0].ready
// }
// }
// }
// }
// }
// }
// stage('Promote from Stage to Prod') {
// steps {
// script {
// openshift.withCluster() {
// openshift.tag("${env.STAGE2}/${env.APP_NAME}:latest", "${env.STAGE3}/${env.APP_NAME}:latest")
// }
// }
// }
// }
//
// stage ('Verify Deployment to Prod') {
// steps {
// script {
// openshift.withCluster() {
// openshift.withProject("${STAGE3}") {
// def dcObj = openshift.selector('dc', env.APP_NAME).object()
// def podSelector = openshift.selector('pod', [deployment: "${APP_NAME}-${dcObj.status.latestVersion}"])
// podSelector.untilEach {
// echo "pod: ${it.name()}"
// return it.object().status.containerStatuses[0].ready
// }
// }
// }
// }
// }
// }
}
}