Student User
2019-08-08 7be4a5f7fc140d1495e739d8a02107879f2b9647
commit | author | age
fe9006 1 pipeline {
RS 2     options {
3         // set a timeout of 60 minutes for this pipeline
4         timeout(time: 60, unit: 'MINUTES')
5     }
6     agent {
7       node {
8         //TODO: Add label for the Maven jenkins agent
9       }
10     }
11
12     environment {
13         //TODO: Customize these variables for your environment
14         DEV_PROJECT = "youruser-movies-dev"
15         STAGE_PROJECT = "youruser-movies-stage"
16         APP_GIT_URL = "https://github.com/youruser/DO288-apps"
17         NEXUS_SERVER = "http://nexus-common.apps.cluster.domain.example.com/repository/java"
18
19         // DO NOT CHANGE THE GLOBAL VARS BELOW THIS LINE
20         APP_NAME = "movies"
21     }
22
23
24     stages {
25
26         stage('Compilation Check') {
27             steps {
28                 echo '### Checking for compile errors ###'
29                 sh '''
30                         cd ${APP_NAME}
31                         mvn -s settings.xml -B clean compile
32                    '''
33             }
34         }
35
36         stage('Run Unit Tests') {
37             steps {
38                 echo '### Running unit tests ###'
39                 sh '''
40                         cd ${APP_NAME}
41                         mvn -s settings.xml -B clean test
42                    '''
43             }
44         }
45
46         stage('Static Code Analysis') {
47             steps {
48                 echo '### Running pmd on code ###'
49                 sh '''
50                         cd ${APP_NAME}
51                         mvn -s settings.xml -B clean pmd:check
52                    '''
53             }
54         }
55
56         stage('Create fat JAR') {
57             steps {
58                 echo '### Creating fat JAR ###'
59                 sh '''
60                         cd ${APP_NAME}
61                         mvn -s settings.xml -B clean package -DskipTests=true
62                    '''
63             }
64         }
65
66         stage('Launch new app in DEV env') {
67             steps {
68                 echo '### Cleaning existing resources in DEV env ###'
69                 sh '''
70                         oc delete all -l app=${APP_NAME} -n ${DEV_PROJECT}
71                         oc delete all -l build=${APP_NAME} -n ${DEV_PROJECT}
72                         sleep 5
73                         oc new-build java:8 --name=${APP_NAME} --binary=true -n ${DEV_PROJECT}
74                    '''
75
76                 echo '### Creating a new app in DEV env ###'
77                 script {
78                     openshift.withCluster() {
79                       openshift.withProject(env.DEV_PROJECT) {
80                         openshift.selector("bc", "${APP_NAME}").startBuild("--from-file=${APP_NAME}/target/${APP_NAME}.jar", "--wait=true", "--follow=true")
81                       }
82                     }
83                 }
84                 // TODO: Create a new OpenShift application based on the ${APP_NAME}:latest image stream
85                 // TODO: Expose the ${APP_NAME} service for external access
86             }
87         }
88
89         stage('Wait for deployment in DEV env') {
90             //TODO: Watch deployment until pod is in 'Running' state
91         }
92
93         stage('Promote to Staging Env') {
94             steps {
95                 timeout(time: 60, unit: 'MINUTES') {
96                     input message: "Promote to Staging?"
97                 }
98                 script {
99                     openshift.withCluster() {
100                     // TODO: Tag the ${APP_NAME}:latest image stream in the dev env as ${APP_NAME}:stage in staging
101                     }
102                 }
103             }
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 ###'
702686 116                 // TODO: Create a new app in staging and expose the service
fe9006 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() {
128                         openshift.withProject( "${STAGE_PROJECT}" ) {
129                             def deployment = openshift.selector("dc", "${APP_NAME}").rollout()
130                             openshift.selector("dc", "${APP_NAME}").related('pods').untilEach(1) {
131                                 return (it.object().status.phase == "Running")
132                             }
133                         }
134                         echo "Deployment to Staging env is complete. Access the API endpoint at the URL http://${routeHost}/movies."
135                     }
136                 }
137             }
138         }
139     }
140 }