-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathJenkinsfile-python
153 lines (144 loc) · 6.13 KB
/
Jenkinsfile-python
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
openshift.withCluster() {
env.APP_NAME = 'hello-world'
env.NAMESPACE = openshift.project()
env.PROD_NAMESPACE = NAMESPACE.replaceAll(/cicd/, 'prod')
canaryWeight=10
}
pipeline {
agent any
stages {
stage("SCM Checkout") {
steps {
script {
checkout scm
}
}
}
stage("Build image") {
steps {
script {
openshift.withCluster() {
buildSelector = openshift.selector('bc', APP_NAME).startBuild("--from-dir .", "--wait", "--follow")
echo "Build complete: " + buildSelector.names()
}
}
}
}
stage("Canary deployment") {
steps {
script {
openshift.withCluster() {
openshift.tag("$NAMESPACE/$APP_NAME:latest", "$PROD_NAMESPACE/$APP_NAME:canary")
openshift.withProject("$PROD_NAMESPACE") {
def dc_selector = openshift.selector('dc', "${APP_NAME}-canary")
if (dc_selector.exists()) {
// if (dc_selector.object().status.availableReplicas == 0) {
def prod_image = openshift.selector('is', APP_NAME).object().status.dockerImageRepository
openshift.set('image', "dc/${APP_NAME}-canary", "hello-world=${prod_image}:canary")
openshift.selector('dc', "${APP_NAME}-canary").rollout().latest()
openshift.selector('dc', "${APP_NAME}-canary").rollout().status()
def latestVersion = openshift.selector('dc', "${APP_NAME}-canary").object().status.latestVersion
echo "Canary pod(s) are up: " + openshift.selector('pod',['deployment':"${APP_NAME}-canary-${latestVersion}"])
// }
}
}
}
}
}
}
stage("Configuring Mirror Traffic") {
steps {
sh 'oc apply -n ${PROD_NAMESPACE} -f k8s/istio/hello-world-virtual-service-mirror.yaml'
}
}
/*************
* Ask to release canary to 10% - when canaryDc.exists()
*************/
stage("Verify Mirror Traffic") {
steps {
script {
promoteOrRollback = input message: 'Release canary deployment to 10% of Traffic?',
parameters: [choice(name: "Promote or Rollback?", choices: 'Promote\nRollback', description: '')]
}
}
}
stage("Rollback Mirror"){
when{
expression {
return promoteOrRollback == 'Rollback'
}
}
steps{
echo "Rollback for canary deployment."
sh 'oc apply -n ${PROD_NAMESPACE} -f k8s/istio/hello-world-virtual-service-init.yaml'
}
}
stage("Release Canary to 10%") {
steps {
sh 'oc apply -n ${PROD_NAMESPACE} -f k8s/istio/hello-world-virtual-service-canary-90-10.yaml'
}
}
/*************
* Ask to release canary to 50% - when canaryDc.exists()
*************/
stage("Verify Canary") {
steps {
script {
promoteOrRollback = input message: 'Release canary deployment to 50% of Traffic?',
parameters: [choice(name: "Promote or Rollback?", choices: 'Promote\nRollback', description: '')]
}
}
}
stage("Rollback canary"){
when{
expression {
return promoteOrRollback == 'Rollback'
}
}
steps{
echo "Rollback for canary deployment."
sh 'oc apply -n ${PROD_NAMESPACE} -f k8s/istio/hello-world-virtual-service-init.yaml'
}
}
stage("Releasing Canary to 50%") {
steps {
sh 'oc apply -n ${PROD_NAMESPACE} -f k8s/istio/hello-world-virtual-service-canary-50-50.yaml'
}
}
/*************
* Ask to promote to production
*************/
stage("Complete Rollout?") {
steps {
script {
promoteOrRollback = input message: 'Complete Rollout?',
parameters: [choice(name: "Complete Rollout or Rollback?", choices: 'Promote\nRollback', description: '')]
}
}
}
stage("Production deployment") {
when {
expression {
return promoteOrRollback != 'Rollback'
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject("$PROD_NAMESPACE") {
openshift.tag("$NAMESPACE/$APP_NAME:latest", "$PROD_NAMESPACE/$APP_NAME:prod")
def prod_image = openshift.selector('is', APP_NAME).object().status.dockerImageRepository
openshift.set('image', "dc/${APP_NAME}-production", "hello-world=${prod_image}:prod")
openshift.selector('dc', "${APP_NAME}-production").rollout().latest()
openshift.selector('dc', "${APP_NAME}-production").rollout().status()
def latestVersion = openshift.selector('dc', "${APP_NAME}-production").object().status.latestVersion
echo "Production pod(s) are up: " + openshift.selector('pod',['deployment':"${APP_NAME}-production-${latestVersion}"])
// Remove Mirror
sh 'oc apply -n ${PROD_NAMESPACE} -f k8s/istio/hello-world-virtual-service-init.yaml'
}
}
}
}
}
}
}