donal
2018-04-17 6dd39cd15a33eac466fc126f0db215de3a214f66
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')
8770ad 18         GITLAB_DOMAIN = "gitlab-<YOUR_NAME>-ci-cd.apps.somedomain.com"
D 19         GITLAB_PROJECT = "<YOUR_NAME>"
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             }
37             when { branch 'master' }
38             steps {
39                 script {
8770ad 40                     // Arbitrary Groovy Script executions can do in script tags
D 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
13c5f6 60                 }
D 61             }
62         }
63         stage("node-build") {
64             agent {
65                 node {
66                     label "jenkins-slave-npm"  
67                 }
68             }
69             steps {
8770ad 70                 // git branch: 'develop',
D 71                 //     credentialsId: 'jenkins-git-creds',
72                 //     url: 'https://gitlab-<YOUR_NAME>-ci-cd.apps.somedomain.com/<YOUR_NAME>/todolist-api.git'
13c5f6 73                 sh 'printenv'
D 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"
8770ad 100                     sh'''
D 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                     '''
13c5f6 104                 }
D 105                 failure {
106                     echo "FAILURE"
107                 }
108             }
109         }
110
111         stage("node-bake") {
112             agent {
113                 node {
114                     label "master"  
115                 }
116             }
6dd39c 117             when {
D 118                 expression { BRANCH_NAME ==~ /(master|develop)/ }
119             }
13c5f6 120             steps {
D 121                 echo '### Get Binary from Nexus ###'
122                 sh  '''
123                         rm -rf package-contents*
124                         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
125                         unzip package-contents.zip
126                     '''
127                 echo '### Create Linux Container Image from package ###'
128                 sh  '''
8770ad 129                         oc project ${PIPELINES_NAMESPACE} # probs not needed
13c5f6 130                         oc patch bc ${APP_NAME} -p "{\\"spec\\":{\\"output\\":{\\"to\\":{\\"kind\\":\\"ImageStreamTag\\",\\"name\\":\\"${APP_NAME}:${JENKINS_TAG}\\"}}}}"
D 131                         oc start-build ${APP_NAME} --from-dir=package-contents/ --follow
132                     '''
133             }
134             post {
135                 always {
136                     archive "**"
137                 }
138             }
139         }
140
141         stage("node-deploy") {
142             agent {
143                 node {
144                     label "master"  
145                 }
146             }
6dd39c 147             when {
D 148                 expression { BRANCH_NAME ==~ /(master|develop)/ }
149             }
13c5f6 150             steps {
D 151                 echo '### tag image for namespace ###'
152                 sh  '''
153                     oc project ${PROJECT_NAMESPACE}
154                     oc tag ${PIPELINES_NAMESPACE}/${APP_NAME}:${JENKINS_TAG} ${PROJECT_NAMESPACE}/${APP_NAME}:${JENKINS_TAG}
155                     '''
156                 echo '### set env vars and image for deployment ###'
157                 sh '''
8770ad 158                     oc set env dc ${APP_NAME} NODE_ENV=${NODE_ENV}
13c5f6 159                     oc set image dc/${APP_NAME} ${APP_NAME}=docker-registry.default.svc:5000/${PROJECT_NAMESPACE}/${APP_NAME}:${JENKINS_TAG}
D 160                     oc rollout latest dc/${APP_NAME}
161                 '''
162                 echo '### Verify OCP Deployment ###'
163                 openshiftVerifyDeployment depCfg: env.APP_NAME, 
164                     namespace: env.PROJECT_NAMESPACE, 
165                     replicaCount: '1', 
166                     verbose: 'false', 
167                     verifyReplicaCount: 'true', 
168                     waitTime: '',
169                     waitUnit: 'sec'
170             }
171         }
172     }
173 }