-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathJenkinsfile
164 lines (147 loc) · 5.35 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
161
162
163
164
openshift.withCluster() {
env.NAMESPACE = openshift.project()
env.SKOPEO_IMAGE = openshift.selector( 'is/jenkins-slave-image-mgmt' ).object().status.dockerImageRepository
env.APP_NAME = "${env.JOB_NAME}".replaceAll(/-?pipeline-?/, '').replaceAll(/-?${env.NAMESPACE}-?/, '')
echo "Starting Pipeline for ${APP_NAME}..."
def projectBase = "${env.NAMESPACE}".replaceAll(/-dev/, '')
env.STAGE1 = "${projectBase}-dev"
env.STAGE2 = "${projectBase}-stage"
env.STAGE3 = "${projectBase}-prod"
}
pipeline {
agent { label 'maven' }
stages {
stage('Code Build') {
steps {
git url: "${SOURCE_CODE_URL}"
sh "mvn -B clean install -q"
}
}
stage('Image Build') {
steps {
echo 'Building Image from Jar File'
sh """
set +x
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
"""
script {
openshift.withCluster() {
openshift.startBuild("${APP_NAME}", "--from-dir=oc-build", "--wait", "--follow")
}
}
}
}
stage ('Verify Deployment to Dev') {
steps {
script {
openshift.withCluster() {
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 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('Promotion gate') {
steps {
script {
input message: 'Promote application to Production?'
}
}
}
stage('Promote to Prod') {
agent {
kubernetes {
label 'promotion-slave'
cloud 'openshift'
serviceAccount 'jenkins'
containerTemplate {
name 'jnlp'
image "docker-registry.default.svc:5000/${NAMESPACE}/jenkins-slave-image-mgmt"
alwaysPullImage true
workingDir '/tmp'
args '${computer.jnlpmac} ${computer.name}'
ttyEnabled false
}
}
}
steps {
script {
openshift.withCluster() {
def localToken = readFile('/var/run/secrets/kubernetes.io/serviceaccount/token').trim()
def secretData = openshift.selector('secret/prod-credentials').object().data
def encodedRegistry = secretData.registry
def encodedToken = secretData.token
def registry = sh(script:"set +x; echo ${encodedRegistry} | base64 --decode", returnStdout: true)
def token = sh(script:"set +x; echo ${encodedToken} | base64 --decode", returnStdout: true)
openshift.withProject("${STAGE2}") {
def imageRegistry = openshift.selector( 'is', "${APP_NAME}").object().status.dockerImageRepository
echo "Promoting ${imageRegistry} -> ${registry}/${STAGE3}/${APP_NAME}"
sh """
set +x
skopeo copy --remove-signatures \
--src-creds openshift:${localToken} --src-cert-dir=/run/secrets/kubernetes.io/serviceaccount/ \
--dest-creds openshift:${token} --dest-tls-verify=false \
docker://${imageRegistry} docker://${registry}/${STAGE3}/${APP_NAME}
"""
}
}
}
}
}
stage ('Verify Deployment to Prod') {
steps {
script {
openshift.withCluster() {
def secretData = openshift.selector('secret/prod-credentials').object().data
def encodedAPI = secretData.api
def encodedToken = secretData.token
env.API = sh(script:"set +x; echo ${encodedAPI} | base64 --decode", returnStdout: true).replaceAll(/https?/, 'insecure')
env.TOKEN = sh(script:"set +x; echo ${encodedToken} | base64 --decode", returnStdout: true)
}
openshift.withCluster( env.API, env.TOKEN ) {
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
}
}
}
}
}
}
}
}