forked from devops-with-openshift/bluegreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluegreen-pipeline.yaml
108 lines (100 loc) · 4.26 KB
/
bluegreen-pipeline.yaml
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
apiVersion: v1
kind: BuildConfig
metadata:
name: bluegreen-pipeline
namespace: bluegreen-mcb
selfLink: /oapi/v1/namespaces/bluegreen-mcb/buildconfigs/bluegreen-pipeline
uid: 3c3aa5a9-71e9-11e7-aecc-525400a1ff71
resourceVersion: '4277'
creationTimestamp: '2017-07-26T10:00:20Z'
labels:
app: bluegreen-pipeline
name: bluegreen-pipeline
annotations:
pipeline.alpha.openshift.io/uses: '[{"name": "bluegreen", "namespace": "", "kind": "DeploymentConfig"}]'
spec:
triggers:
- type: GitHub
github:
secret: secret101
- type: Generic
generic:
secret: secret101
runPolicy: Serial
source:
type: None
strategy:
type: JenkinsPipeline
jenkinsPipelineStrategy:
jenkinsfile: |
#!groovy
def appName="main"
def tag="blue"
def altTag="green"
node {
def project = getProjectName()
stage("Initialize") {
/* Inialization Stage does the following
- get current route to determine the active application version ie: blue or green
the active application is referenced as altTag, the new application is referenced as tag
- get current route to determine the URL for accessing the service
*/
sh "oc get route ${appName} -n ${project} -o jsonpath='{ .spec.to.name }' > activeservice"
activeService = readFile('activeservice').trim()
if (activeService == "blue") {
tag = "green"
altTag = "blue"
}
sh "oc get route ${tag} -n ${project} -o jsonpath='{ .spec.host }' > routehost"
routeHost = readFile('routehost').trim()
sh "oc get route main -n ${project} -o jsonpath='{ .spec.host }' > routehost"
routeHostMain = readFile('routehost').trim()
}
stage("Build") {
// The build stage will build a new image for the 'tag' application or application to be tested
echo "building tag ${tag}"
openshiftBuild buildConfig: "${tag}", showBuildLogs: "true"
}
stage("Deploy Test") {
// The Deploy Test stage will verify that the deployment of the new build of the 'tag' application has completed successfully
openshiftTag srcStream: "${tag}", srcTag: 'latest', destinationStream: "${tag}", destinationTag: 'latest'
openshiftVerifyDeployment deploymentConfig: "${tag}"
}
stage("Test") {
// The Test stage simply asks the user to confirm it is OK to change the service route over to the 'tag' application
input message: "Build of ${tag} is ready, select OK to route this build to ${routeHostMain} for testing.", id: "approval", ok: "Ok"
}
stage("Go Live") {
// The Go Live stage patches the 'main' route or application URL over the the 'tag' applicatoin in order to allow the user to test the 'tag' application
def patch1 = $/oc patch route/main -p $'{\"spec\":{\"to\":{\"name\":\"${tag}\"}}}$' -n ${project}/$
sh patch1
}
stage("Rollback") {
/* The Rollback stage prompts the user whether the user would like to revert the main route to the 'altTag' application or not and
therefore keeping the 'tag' application in place
*/
sh "oc get route ${altTag} -n ${project} -o jsonpath='{ .spec.host }' > routehost"
routeHost = readFile('routehost').trim()
def patch2 = $/oc patch route/main -p $'{\"spec\":{\"to\":{\"name\":\"${altTag}\"}}}$' -n ${project}/$
try {
input message: "Would you like Keep ${tag} or Abort to rollback the deployment to ${altTag} ? ", id: "approval", ok: "Keep"
echo "keeping ${tag}"
} catch (error) {
echo "rolling back to ${altTag}"
sh patch2
}
}
}
def getProjectName(){
def cmd1 = $/project=$(oc get project -o name);echo $${project##project*/} > projectName/$
sh cmd1
name = readFile('projectName').trim()
sh 'rm projectName'
return name
}
output: {}
resources: {}
postCommit: {}
nodeSelector: null
status:
lastVersion: 0