Ravi Srinivasan
2019-07-03 2ca6c30d94f675a75a57e8dc6c820ef6ff5bc3c7
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
pipeline {
    options {
        // set a timeout of 30 minutes for this pipeline
        timeout(time: 30, unit: 'MINUTES')
    }
    agent {
      node {
        //TODO: Add label for Node.js jenkins agent
      }
    }
 
    environment {
        //TODO: Edit these vars as per your env
        DEV_PROJECT = "youruser-books-dev"
        STAGE_PROJECT = "youruser-books-stage"
        APP_GIT_URL = "https://github.com/youruser/DO288-apps"
        NEXUS_SERVER = "http://nexus-common.apps.cluster.domain.example.com/repository/nodejs"
 
        // DO NOT CHANGE THE GLOBAL VARS BELOW THIS LINE
        APP_NAME = "books"
    }
 
 
    stages {
 
        stage('NPM Install') {
            steps {
                echo '### Installing NPM dependencies ###'
                sh '''
                        npm config set registry ${NEXUS_SERVER}
                        cd books
                        npm install
                   '''
            }
        }
 
        stage('Run Unit Tests') {
            steps {
                echo '### Running unit tests ###'
                sh 'cd books; npm test'
            }
        }
 
        stage('Run Linting Tools') {
            steps {
                echo '### Running eslint on code ###'
                sh 'cd books; npm run lint'
            }
        }
 
        stage('Launch new app in DEV env') {
            steps {
                echo '### Cleaning existing resources in DEV env ###'
                sh '''
                        oc project ${DEV_PROJECT}
                        oc delete all -l app=${APP_NAME}
                        sleep 5
                   '''
 
                echo '### Creating a new app in DEV env ###'
                //TODO: Create a new app and expose the service
            }
        }
 
        stage('Wait for S2I build to complete') {
            steps {
                script {
                    openshift.withCluster() {
                        openshift.withProject( "${DEV_PROJECT}" ) {
                            def bc = openshift.selector("bc", "${APP_NAME}")
                            bc.logs('-f')
                            def builds = bc.related('builds')
                            builds.untilEach(1) {
                                return (it.object().status.phase == "Complete")
                            }
                        }
                    }
                }
            }
        }
 
        stage('Wait for deployment in DEV env') {
            steps {
                script {
                    openshift.withCluster() {
                        openshift.withProject( "${DEV_PROJECT}" ) {
                            def deployment = openshift.selector("dc", "${APP_NAME}").rollout()
                            openshift.selector("dc", "${APP_NAME}").related('pods').untilEach(1) {
                                return (it.object().status.phase == "Running")
                            }
                        }
                    }
                }
            }
        }
 
        stage('Promote to Staging Env') {
            steps {
                timeout(time: 60, unit: 'MINUTES') {
                    input message: "Promote to Staging?"
                }
                //TODO: Tag the books:latest image stream as books:stage
            }
        }
 
        stage('Deploy to Staging Env') {
            steps {
                echo '### Cleaning existing resources in Staging ###'
                sh '''
                        oc project ${STAGE_PROJECT}
                        oc delete all -l app=${APP_NAME}
                        sleep 5
                   '''
 
                echo '### Creating a new app in Staging ###'
                //TODO: Create a new app in stage using the books:stage image stream and expose the service
            }
        }
 
        stage('Wait for deployment in Staging') {
            steps {
                sh "oc get route ${APP_NAME} -n ${STAGE_PROJECT} -o jsonpath='{ .spec.host }' --loglevel=4 > routehost"
 
                script {
                    routeHost = readFile('routehost').trim()
 
                    openshift.withCluster() {
                        //TODO: Watch deployment until pod is in 'Running' state
 
                        echo "Deployment to Staging env is complete. Access the app at the URL http://${routeHost}."
                    }
                }
            }
        }
    }
}