Ravi Srinivasan
2019-07-03 2ca6c30d94f675a75a57e8dc6c820ef6ff5bc3c7
commit | author | age
f3b017 1 pipeline {
RS 2     options {
3         // set a timeout of 30 minutes for this pipeline
4         timeout(time: 30, unit: 'MINUTES')
5     }
6     agent {
7       node {
835453 8         //TODO: Add label for Node.js jenkins agent
f3b017 9       }
RS 10     }
11
12     environment {
835453 13         //TODO: Edit these vars as per your env
f3b017 14         DEV_PROJECT = "youruser-books-dev"
RS 15         STAGE_PROJECT = "youruser-books-stage"
16         APP_GIT_URL = "https://github.com/youruser/DO288-apps"
17         NEXUS_SERVER = "http://nexus-common.apps.cluster.domain.example.com/repository/nodejs"
18
19         // DO NOT CHANGE THE GLOBAL VARS BELOW THIS LINE
20         APP_NAME = "books"
21     }
22
23
24     stages {
25
26         stage('NPM Install') {
27             steps {
28                 echo '### Installing NPM dependencies ###'
29                 sh '''
30                         npm config set registry ${NEXUS_SERVER}
31                         cd books
32                         npm install
33                    '''
34             }
35         }
36
37         stage('Run Unit Tests') {
38             steps {
39                 echo '### Running unit tests ###'
40                 sh 'cd books; npm test'
41             }
42         }
43
44         stage('Run Linting Tools') {
45             steps {
46                 echo '### Running eslint on code ###'
47                 sh 'cd books; npm run lint'
48             }
49         }
50
51         stage('Launch new app in DEV env') {
52             steps {
53                 echo '### Cleaning existing resources in DEV env ###'
54                 sh '''
55                         oc project ${DEV_PROJECT}
56                         oc delete all -l app=${APP_NAME}
57                         sleep 5
58                    '''
59
60                 echo '### Creating a new app in DEV env ###'
835453 61                 //TODO: Create a new app and expose the service
f3b017 62             }
RS 63         }
64
65         stage('Wait for S2I build to complete') {
66             steps {
67                 script {
68                     openshift.withCluster() {
69                         openshift.withProject( "${DEV_PROJECT}" ) {
70                             def bc = openshift.selector("bc", "${APP_NAME}")
71                             bc.logs('-f')
72                             def builds = bc.related('builds')
73                             builds.untilEach(1) {
74                                 return (it.object().status.phase == "Complete")
75                             }
76                         }
77                     }
78                 }
79             }
80         }
81
82         stage('Wait for deployment in DEV env') {
83             steps {
84                 script {
85                     openshift.withCluster() {
86                         openshift.withProject( "${DEV_PROJECT}" ) {
87                             def deployment = openshift.selector("dc", "${APP_NAME}").rollout()
88                             openshift.selector("dc", "${APP_NAME}").related('pods').untilEach(1) {
89                                 return (it.object().status.phase == "Running")
90                             }
91                         }
92                     }
93                 }
94             }
95         }
96
97         stage('Promote to Staging Env') {
98             steps {
99                 timeout(time: 60, unit: 'MINUTES') {
100                     input message: "Promote to Staging?"
101                 }
835453 102                 //TODO: Tag the books:latest image stream as books:stage
f3b017 103             }
RS 104         }
105
106         stage('Deploy to Staging Env') {
107             steps {
108                 echo '### Cleaning existing resources in Staging ###'
109                 sh '''
110                         oc project ${STAGE_PROJECT}
111                         oc delete all -l app=${APP_NAME}
112                         sleep 5
113                    '''
114
115                 echo '### Creating a new app in Staging ###'
835453 116                 //TODO: Create a new app in stage using the books:stage image stream and expose the service
f3b017 117             }
RS 118         }
119
120         stage('Wait for deployment in Staging') {
121             steps {
122                 sh "oc get route ${APP_NAME} -n ${STAGE_PROJECT} -o jsonpath='{ .spec.host }' --loglevel=4 > routehost"
123
124                 script {
125                     routeHost = readFile('routehost').trim()
126
127                     openshift.withCluster() {
835453 128                         //TODO: Watch deployment until pod is in 'Running' state
RS 129
f3b017 130                         echo "Deployment to Staging env is complete. Access the app at the URL http://${routeHost}."
RS 131                     }
132                 }
133             }
134         }
135     }
136 }