donal
2018-06-14 aa93ed4dfa25ec1eb834c2aed59672773bc67128
commit | author | age
8770ad 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 {
9         // GLobal Vars
10         PIPELINES_NAMESPACE = "<YOUR_NAME>-ci-cd"
11         APP_NAME = "todolist-api"
12
13         JENKINS_TAG = "${JOB_NAME}.${BUILD_NUMBER}".replace("/", "-")
14         JOB_NAME = "${JOB_NAME}".replace("/", "-")
15
16         GIT_SSL_NO_VERIFY = true
17         GIT_CREDENTIALS = credentials('jenkins-git-creds')
18         GITLAB_DOMAIN = "gitlab-<YOUR_NAME>-ci-cd.apps.somedomain.com"
19         GITLAB_PROJECT = "<YOUR_NAME>"
20     }
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             }
37             when { branch 'master' }
38             steps {
39                 script {
40                     // Arbitrary Groovy Script executions can do in script tags
41                     env.PROJECT_NAMESPACE = "<YOUR_NAME>-test"
42                     env.NODE_ENV = "test"
43                     env.E2E_TEST_ROUTE = "oc get route/${APP_NAME} --template='{{.spec.host}}' -n ${PROJECT_NAMESPACE}".execute().text
44                 }
45             }
46         }
47         stage("prepare environment for develop deploy") {
48             agent {
49                 node {
50                     label "master"
51                 }
52             }
53             when { branch 'develop' }
54             steps {
55                 script {
56                     // Arbitrary Groovy Script executions can do in script tags
57                     env.PROJECT_NAMESPACE = "<YOUR_NAME>-dev"
58                     env.NODE_ENV = "dev"
59                     env.E2E_TEST_ROUTE = "oc get route/${APP_NAME} --template='{{.spec.host}}' -n ${PROJECT_NAMESPACE}".execute().text
60                 }
61             }
62         }
63         stage("node-build") {
64             agent {
65                 node {
66                     label "jenkins-slave-npm"  
67                 }
68             }
69             steps {
70                 // git branch: 'develop',
71                 //     credentialsId: 'jenkins-git-creds',
72                 //     url: 'https://gitlab-<YOUR_NAME>-ci-cd.apps.somedomain.com/<YOUR_NAME>/todolist-api.git'
73                 sh 'printenv'
74
75                 echo '### Install deps ###'
76                 sh 'scl enable rh-nodejs8 \'npm install\''
77
78                 echo '### Running tests ###'
79                 sh 'scl enable rh-nodejs8 \'npm run test:ci\''
80
81                 echo '### Running build ###'
82                 sh 'scl enable rh-nodejs8 \'npm run build:ci\''
83
84
85                 echo '### Packaging App for Nexus ###'
86                 sh 'scl enable rh-nodejs8 \'npm run package\''
87                 sh 'scl enable rh-nodejs8 \'npm run publish\''
88             }
89             // Post can be used both on individual stages and for the entire build.
90             post {
91                 always {
92                     archive "**"
93                     junit 'reports/server/mocha/test-results.xml'
94                     // publish html
95
96                     // Notify slack or some such
97                 }
98                 success {
99                     echo "Git tagging"
100                     sh'''
101                         git tag -a ${JENKINS_TAG} -m "JENKINS automated commit"
102                         git push https://${GIT_CREDENTIALS_USR}:${GIT_CREDENTIALS_PSW}@${GITLAB_DOMAIN}/${GITLAB_PROJECT}/${APP_NAME}.git --tags
103                     '''
104                 }
105                 failure {
106                     echo "FAILURE"
107                 }
108             }
109         }
110
111         stage("node-bake") {
112             agent {
113                 node {
114                     label "master"  
115                 }
116             }
117             steps {
118                 echo '### Get Binary from Nexus ###'
119                 sh  '''
120                         rm -rf package-contents*
121                         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
122                         unzip package-contents.zip
123                     '''
124                 echo '### Create Linux Container Image from package ###'
125                 sh  '''
126                         oc project ${PIPELINES_NAMESPACE} # probs not needed
127                         oc patch bc ${APP_NAME} -p "{\\"spec\\":{\\"output\\":{\\"to\\":{\\"kind\\":\\"ImageStreamTag\\",\\"name\\":\\"${APP_NAME}:${JENKINS_TAG}\\"}}}}"
128                         oc start-build ${APP_NAME} --from-dir=package-contents/ --follow
129                     '''
130             }
131             post {
132                 always {
133                     archive "**"
134                 }
135             }
136         }
137
138         stage("node-deploy") {
139             agent {
140                 node {
141                     label "master"  
142                 }
143             }
144             steps {
145                 echo '### tag image for namespace ###'
146                 sh  '''
147                     oc project ${PROJECT_NAMESPACE}
148                     oc tag ${PIPELINES_NAMESPACE}/${APP_NAME}:${JENKINS_TAG} ${PROJECT_NAMESPACE}/${APP_NAME}:${JENKINS_TAG}
149                     '''
150                 echo '### set env vars and image for deployment ###'
151                 sh '''
152                     oc set env dc ${APP_NAME} NODE_ENV=${NODE_ENV}
153                     oc set image dc/${APP_NAME} ${APP_NAME}=docker-registry.default.svc:5000/${PROJECT_NAMESPACE}/${APP_NAME}:${JENKINS_TAG}
154                     oc rollout latest dc/${APP_NAME}
155                 '''
156                 echo '### Verify OCP Deployment ###'
157                 openshiftVerifyDeployment depCfg: env.APP_NAME, 
158                     namespace: env.PROJECT_NAMESPACE, 
159                     replicaCount: '1', 
160                     verbose: 'false', 
161                     verifyReplicaCount: 'true', 
162                     waitTime: '',
163                     waitUnit: 'sec'
164             }
165         }
166     }
167 }