Student User
2020-07-07 d26619a9f7c6379ee791053aacdebf08c1acf713
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
pipeline {
    environment {
        DOMAIN='apps.ocp4.example.com'
        PRJ="hello-${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
        APP='nodeapp'
    }
    agent {
      node {
        label 'nodejs'
      }
    }
    stages {
        stage('create') {
            steps {
                script {
                    // Uncomment to get lots of debugging output
                    //openshift.logLevel(1)
                    openshift.withCluster() {
                        echo("Create project ${env.PRJ}") 
                        openshift.newProject("${env.PRJ}")
                        openshift.withProject("${env.PRJ}") {
                            echo('Grant to developer read access to the project')
                            openshift.raw('policy', 'add-role-to-user', 'view', 'developer')
                            echo("Create app ${env.APP}") 
                            openshift.newApp("${env.GIT_URL}#${env.BRANCH_NAME}", "--strategy source", "--name ${env.APP}")
                        }
                    }
                }
            }
        }
        stage('build') {
            steps {
                script {
                    openshift.withCluster() {
                        openshift.withProject("${env.PRJ}") {
                            def bc = openshift.selector('bc', "${env.APP}")
                            echo("Wait for build from bc ${env.APP} to finish") 
                            timeout(5) {
                                def builds = bc.related('builds').untilEach(1) {
                                    def phase = it.object().status.phase
                                    if (phase == "Failed" || phase == "Error" || phase == "Cancelled") {
                                        error 'OpenShift build failed or was cancelled'
                                    }
                                    return (phase == "Complete")
                                }
                            }
                        }
                    }
                }
            }
        }
        stage('deploy') {
            steps {
                script {
                    openshift.withCluster() {
                        openshift.withProject("${env.PRJ}") {
                            echo("Expose route for service ${env.APP}") 
                            // Default Jenkins settings to not allow to query properties of an object
                            // So we cannot query the widlcard domain of the ingress controller
                            // Nor the auto genereted host of a route
                            openshift.expose("svc/${env.APP}", "--hostname ${env.PRJ}.${env.DOMAIN}")
                            echo("Wait for deployment from dc ${env.APP} to finish") 
                            timeout(5) {
                                openshift.selector('dc', "${env.APP}").rollout().status()
                            }
                        }
                    }
                }
            }
        }
        stage('test') {
            input {
                message 'About to test the application'
                ok 'Ok'
            }
            steps {
                echo "Check that '${env.PRJ}.${env.DOMAIN}' returns HTTP 200"
                sh "curl -s --fail ${env.PRJ}.${env.DOMAIN}"
            }
        }
    }
    post {
        always {
            script {
                openshift.withCluster() {
                    echo("Delete project ${env.PRJ}") 
                    openshift.delete("project/${env.PRJ}")
                }
            }
        }
    }
}