Ravi Srinivasan
2019-02-13 5239ecd7d117cb9ffbab67ae898f1bb69a131d31
commit | author | age
13c5f6 1 pipeline {
D 2
3     agent {
4         // label "" also could have been 'agent any' - that has the same meaning.
5         label "master"
6     }
7
8     environment {
8770ad 9         // GLobal Vars
D 10         PIPELINES_NAMESPACE = "<YOUR_NAME>-ci-cd"
13c5f6 11         APP_NAME = "todolist-api"
D 12
13         JENKINS_TAG = "${JOB_NAME}.${BUILD_NUMBER}".replace("/", "-")
8770ad 14         JOB_NAME = "${JOB_NAME}".replace("/", "-")
13c5f6 15
D 16         GIT_SSL_NO_VERIFY = true
17         GIT_CREDENTIALS = credentials('jenkins-git-creds')
8751cf 18         GITLAB_DOMAIN = "gitlab.apps.lader.rht-labs.com"
DS 19         GITLAB_PROJECT = "<GIT_USERNAME>"
13c5f6 20     }
D 21
22     // The options directive is for configuration that applies to the whole job.
23     options {
24         buildDiscarder(logRotator(numToKeepStr:'10'))
25         timeout(time: 15, unit: 'MINUTES')
26         ansiColor('xterm')
27         timestamps()
28     }
29
30     stages {
31         stage("prepare environment for master deploy") {
32             agent {
33                 node {
34                     label "master"
35                 }
36             }
df4122 37             when {
D 38               expression { GIT_BRANCH ==~ /(.*master)/ }
39             }
13c5f6 40             steps {
D 41                 script {
8770ad 42                     // Arbitrary Groovy Script executions can do in script tags
D 43                     env.PROJECT_NAMESPACE = "<YOUR_NAME>-test"
44                     env.NODE_ENV = "test"
df4122 45                     env.E2E_TEST_ROUTE = "oc get route/${APP_NAME} --template='{{.spec.host}}' -n ${PROJECT_NAMESPACE}".execute().text.minus("'").minus("'")
8770ad 46                 }
D 47             }
48         }
49         stage("prepare environment for develop deploy") {
50             agent {
51                 node {
52                     label "master"
53                 }
54             }
df4122 55             when {
D 56               expression { GIT_BRANCH ==~ /(.*develop)/ }
57             }
8770ad 58             steps {
D 59                 script {
60                     // Arbitrary Groovy Script executions can do in script tags
61                     env.PROJECT_NAMESPACE = "<YOUR_NAME>-dev"
62                     env.NODE_ENV = "dev"
df4122 63                     env.E2E_TEST_ROUTE = "oc get route/${APP_NAME} --template='{{.spec.host}}' -n ${PROJECT_NAMESPACE}".execute().text.minus("'").minus("'")
13c5f6 64                 }
D 65             }
66         }
67         stage("node-build") {
68             agent {
69                 node {
70                     label "jenkins-slave-npm"  
71                 }
72             }
73             steps {
8770ad 74                 // git branch: 'develop',
D 75                 //     credentialsId: 'jenkins-git-creds',
76                 //     url: 'https://gitlab-<YOUR_NAME>-ci-cd.apps.somedomain.com/<YOUR_NAME>/todolist-api.git'
13c5f6 77                 sh 'printenv'
D 78
79                 echo '### Install deps ###'
df4122 80                 sh 'npm install'
13c5f6 81
D 82                 echo '### Running tests ###'
df4122 83                 sh 'npm run test:ci'
13c5f6 84
D 85                 echo '### Running build ###'
df4122 86                 sh 'npm run build:ci'
13c5f6 87
D 88
89                 echo '### Packaging App for Nexus ###'
df4122 90                 sh 'npm run package'
D 91                 sh 'npm run publish'
13c5f6 92             }
D 93             // Post can be used both on individual stages and for the entire build.
94             post {
95                 always {
96                     archive "**"
97                     junit 'reports/server/mocha/test-results.xml'
98                     // publish html
99
100                     // Notify slack or some such
101                 }
102                 success {
103                     echo "Git tagging"
8770ad 104                     sh'''
D 105                         git tag -a ${JENKINS_TAG} -m "JENKINS automated commit"
106                         git push https://${GIT_CREDENTIALS_USR}:${GIT_CREDENTIALS_PSW}@${GITLAB_DOMAIN}/${GITLAB_PROJECT}/${APP_NAME}.git --tags
107                     '''
13c5f6 108                 }
D 109                 failure {
110                     echo "FAILURE"
111                 }
112             }
113         }
114
115         stage("node-bake") {
116             agent {
117                 node {
118                     label "master"  
119                 }
120             }
6dd39c 121             when {
df4122 122                 expression { GIT_BRANCH ==~ /(.*master|.*develop)/ }
6dd39c 123             }
13c5f6 124             steps {
D 125                 echo '### Get Binary from Nexus ###'
126                 sh  '''
127                         rm -rf package-contents*
128                         curl -v -f http://admin:admin123@${NEXUS_SERVICE_HOST}:${NEXUS_SERVICE_PORT}/repository/zip/com/redhat/todolist/${JENKINS_TAG}/package-contents.zip -o package-contents.zip
129                         unzip package-contents.zip
130                     '''
131                 echo '### Create Linux Container Image from package ###'
132                 sh  '''
8770ad 133                         oc project ${PIPELINES_NAMESPACE} # probs not needed
13c5f6 134                         oc patch bc ${APP_NAME} -p "{\\"spec\\":{\\"output\\":{\\"to\\":{\\"kind\\":\\"ImageStreamTag\\",\\"name\\":\\"${APP_NAME}:${JENKINS_TAG}\\"}}}}"
D 135                         oc start-build ${APP_NAME} --from-dir=package-contents/ --follow
136                     '''
137             }
138             post {
139                 always {
140                     archive "**"
141                 }
142             }
143         }
144
145         stage("node-deploy") {
146             agent {
147                 node {
148                     label "master"  
149                 }
150             }
6dd39c 151             when {
df4122 152                 expression { GIT_BRANCH ==~ /(.*master|.*develop)/ }
6dd39c 153             }
13c5f6 154             steps {
D 155                 echo '### tag image for namespace ###'
156                 sh  '''
157                     oc project ${PROJECT_NAMESPACE}
158                     oc tag ${PIPELINES_NAMESPACE}/${APP_NAME}:${JENKINS_TAG} ${PROJECT_NAMESPACE}/${APP_NAME}:${JENKINS_TAG}
159                     '''
160                 echo '### set env vars and image for deployment ###'
161                 sh '''
8770ad 162                     oc set env dc ${APP_NAME} NODE_ENV=${NODE_ENV}
13c5f6 163                     oc set image dc/${APP_NAME} ${APP_NAME}=docker-registry.default.svc:5000/${PROJECT_NAMESPACE}/${APP_NAME}:${JENKINS_TAG}
D 164                     oc rollout latest dc/${APP_NAME}
165                 '''
166                 echo '### Verify OCP Deployment ###'
167                 openshiftVerifyDeployment depCfg: env.APP_NAME, 
168                     namespace: env.PROJECT_NAMESPACE, 
169                     replicaCount: '1', 
170                     verbose: 'false', 
171                     verifyReplicaCount: 'true', 
172                     waitTime: '',
173                     waitUnit: 'sec'
174             }
175         }
176     }
177 }