Jaime Ramírez
2020-06-11 d4efcf556bee5599b87a18da9420df2143e1c757
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
 
CONTAINER_IMAGE='quay.io/redhattraining/ossm-adopt-a-pup-webapp'
WEBAPP_DIR='adopt-a-pup/web-app'
 
pipeline {
    agent {
        label 'master'
    }
 
    environment {
        HOME = '${WORKSPACE}'
        NPM_CONFIG_CACHE = '${WORKSPACE}/.npm'
        CI = 'true'
    }
 
    stages {
        stage('adopt-a-pup web-app: Install') {
            agent {
                docker {
                    image 'node:14'
                }
            }
 
            steps {
                dir(WEBAPP_DIR) {
                    sh 'npm ci'
                }
            }
        }
 
        stage('adopt-a-pup web-app: Test') {
            agent {
                docker {
                    image 'node:14'
                }
            }
 
            steps {
                dir(WEBAPP_DIR) {
                    sh 'npm run lint'
                    sh 'npm test'
                }
            }
        }
 
        stage('adopt-a-pup web-app: NPM build') {
            agent {
                docker {
                    image 'node:14'
                }
            }
 
            steps {
                dir(WEBAPP_DIR) {
                    sh 'REACT_APP_VERSION=v1 npm run build'
                }
                stash includes: 'adopt-a-pup/web-app/build/**/*', name: 'build_v1'
 
                dir(WEBAPP_DIR) {
                    sh 'REACT_APP_VERSION=v2 npm run build'
                }
                stash includes: 'adopt-a-pup/web-app/build/**/*', name: 'build_v2'
            }
        }
 
        stage('adopt-a-pup web-app: DEPLOY?') {
            when {
                branch 'master'
            }
 
            steps {
                timeout(time: 10, unit: 'MINUTES') {
                    input 'Build and deploy image to quay?'
                }
            }
        }
 
        stage('adopt-a-pup web-app: Build and push image') {
            when {
                branch 'master'
            }
 
            steps {
                sh 'rm -rf build'
                unstash 'build_v1'
                dir(WEBAPP_DIR) {
                    sh "docker build -t ${CONTAINER_IMAGE} ."
                    sh "docker tag ${CONTAINER_IMAGE} ${CONTAINER_IMAGE}:1.0"
                }
 
                sh 'rm -rf build'
                unstash 'build_v2'
                dir(WEBAPP_DIR) {
                    sh "docker build -t ${CONTAINER_IMAGE} ."
                    sh "docker tag ${CONTAINER_IMAGE} ${CONTAINER_IMAGE}:2.0"
                }
 
                withDockerRegistry([url: 'https://quay.io/', credentialsId: 'rht_jenkins_quay']) {
                    sh "docker push ${CONTAINER_IMAGE}"
                }
            }
        }
    }
 
    post {
        fixed {
            googlechatnotification(
                url: "${gchat_webhook}",
                message: ":-) Branch ${env.BRANCH_NAME} build fixed! ${env.BUILD_URL}",
                notifyAborted: 'true', notifyFailure: 'true', notifyNotBuilt: 'true',
                notifySuccess: 'true', notifyUnstable: 'true', notifyBackToNormal: 'true',
                suppressInfoLoggers: 'true', sameThreadNotification: 'true')
        }
 
        failure {
            googlechatnotification(
                url: "${gchat_webhook}",
                message: ":-( Build failed in branch ${env.BRANCH_NAME}, see output here: ${env.BUILD_URL}console",
                notifyAborted: 'true', notifyFailure: 'true', notifyNotBuilt: 'true', notifySuccess: 'true',
                notifyUnstable: 'true', notifyBackToNormal: 'true', suppressInfoLoggers: 'true',
                sameThreadNotification: 'true')
        }
        always {
            sh "rm ${env.WORKSPACE}/adopt-a-pup/web-app/build -fr"
        }
    }
}