-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
302 lines (219 loc) · 9.35 KB
/
Jenkinsfile
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Powered by Femi Patrician
pipeline {
agent any
environment {
STAGING_INSTANCE_USER = credentials('DEV_INSTANCE_USER')
STAGING_INSTANCE_IP = credentials('DEV_INSTANCE_IP')
INSTANCE_USER = credentials('LIVE_INSTANCE_USER')
INSTANCE_IP = credentials('LIVE_INSTANCE_IP')
BITBUCKET_USER = credentials('BITBUCKET_USER')
BITBUCKET_PASS = credentials('BITBUCKET_PASS')
REPO = credentials('PATRICIA_CMS_SERVER_REPO')
REPO_SLUG = "patricia-cms-server"
PATRICIA_DEVOPS_SLACK = credentials('PATRICIA_DEVOPS_SLACK')
PATRICIA_UNIVERSE_SLACK = credentials('PATRICIA_UNIVERSE_SLACK')
REPO_TITLE = "PATRICIA-CMS-SERVER"
DEPLOY_TITLE = "patricia-cms-server-deploy"
BUILD_TITLE = "patricia-cms-server-build"
DEPLOY_DIR = "patricia-cms-server"
CHECKOUT_BRANCH = getBitbucketBranch()
}
stages {
// stage('Clear workspace') {
// steps {
// script {
// sh 'git clean -fdx'
// }
// }
// }
stage ('Checkout') {
steps {
script {
def scmVars = checkout([$class: 'GitSCM', branches: [[name: "${env.CHECKOUT_BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '87e13238-e989-4f9c-8196-43d16d23a3eb', url: 'https://bitbucket.org/patricia_tech/patricia-cms-server.git']]])
env.GIT_COMMIT = scmVars.GIT_COMMIT[0..7]
env.GIT_COMMIT_FULL = scmVars.GIT_COMMIT
env.GIT_BRANCH =scmVars.GIT_BRANCH
}
}
}
// stage('Setup environment') {
// steps {
// sh 'npm install'
// }
// }
stage('Run Build Test') {
steps {
notifyBuild('STARTED')
script {
try {
// sh './vendor/bin/phpunit'
notifyBuild('SUCCESSFUL')
}
catch(err) {
notifyBuild('SUCCESSFUL')
//error('Build test failed')
}
}
}
}
stage('Deploy to dev server') {
when {
expression {
return env.GIT_BRANCH == 'origin/dev';
}
}
steps {
script {
try {
sshagent (credentials: ['staging_private_key']) {
sh 'ssh -o StrictHostKeyChecking=no -v ${STAGING_INSTANCE_USER}@${STAGING_INSTANCE_IP} "cd /var/www/${DEPLOY_DIR} && sudo git pull https://${BITBUCKET_USER}:${BITBUCKET_PASS}@${REPO} dev --no-edit && sudo chown -R www-data:www-data * && sudo pm2 restart cms-server"'
notifyDeploy('SUCCESSFUL', 'staging')
}
} catch(all) {
notifyDeploy('FAILURE', 'staging')
error('Deploy failed')
}
}
}
}
stage('Notify Slack channel Staging') {
when {
expression {
return env.GIT_BRANCH == 'origin/dev';
}
}
steps {
//post devops slack
sh label: 'post to slack', script: 'curl -X POST -H "Content-type: application/json" --data "{\\"text\\":\\"@channel [${REPO_TITLE}] has been deployed to Staging Server. Deployment completed - jenkins\\"}" ${PATRICIA_DEVOPS_SLACK}'
// post to patricia universe
sh label: 'post to slack 2', script: 'curl -X POST -H "Content-type: application/json" --data "{\\"text\\":\\"@channel [${REPO_TITLE}] has been deployed to Staging Server. Deployment completed - jenkins\\"}" ${PATRICIA_UNIVERSE_SLACK}'
}
}
stage('Deploy to Production server') {
when {
expression {
return env.GIT_BRANCH == 'origin/master';
}
}
steps {
script {
try {
sshagent (credentials: ['live_private_key']) {
sh 'ssh -o StrictHostKeyChecking=no -v ${INSTANCE_USER}@${INSTANCE_IP} "cd /var/www/${DEPLOY_DIR} && sudo git pull https://${BITBUCKET_USER}:${BITBUCKET_PASS}@${REPO} master --no-edit && sudo npm run build && sudo cp -r dist/* live && sudo chown -R www-data:www-data *"'
notifyDeploy('SUCCESSFUL', 'production')
}
} catch(all) {
notifyDeploy('FAILED', 'production')
error('failed to deploy')
}
}
}
}
stage('Notify Slack channel Production') {
when {
expression {
return env.GIT_BRANCH == 'origin/master';
}
}
steps {
//post devops slack
sh label: 'post to slack', script: 'curl -X POST -H "Content-type: application/json" --data "{\\"text\\":\\"@channel [${REPO_TITLE}] has been deployed to Production Server. Deployment completed - jenkins\\"}" ${PATRICIA_DEVOPS_SLACK}'
// post to patricia universe
sh label: 'post to slack 2', script: 'curl -X POST -H "Content-type: application/json" --data "{\\"text\\":\\"@channel [${REPO_TITLE}] has been deployed to Production Server. Deployment completed - jenkins\\"}" ${PATRICIA_UNIVERSE_SLACK}'
}
}
}
}
def notifyDeploy(String deployStatus = 'SUCCESSFUL', String environment = 'staging') {
if(deployStatus == 'SUCCESSFUL') {
bitbucketStatusNotify(
buildState: "${deployStatus}",
buildKey: 'deploy',
buildName: "ci/jenkinsci: ${environment}-deploy",
buildDescription: 'Your deployment was successful via JenkinsCI!',
repoSlug: "${REPO_SLUG}",
commitId: "${env.GIT_COMMIT_FULL}"
)
}
else {
bitbucketStatusNotify(
buildState: "${deployStatus}",
buildKey: 'deploy',
buildName: "ci/jenkinsci: ${environment}-deploy",
buildDescription: 'Your deployment failed via JenkinsCI!',
repoSlug: "${REPO_SLUG}",
commitId: "${env.GIT_COMMIT_FULL}"
)
}
}
def getBitbucketBranch() {
if (checkout_branch != "test") {
//repo push of new branch
return "${checkout_branch}"
}
if (source_branch != "test") {
//pull request
//should check if its merged
if(pr_state == "MERGED") {
return "${dest_branch}"
}
}
if (old_branch != "test") {
return "${old_branch}"
}
return "no-branch"
}
def notifyBuild(String buildStatus = 'STARTED') {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
def dontNotify = false
// Default values
def colorName = 'RED'
def colorCode = '#FF0000'
def subject = "${buildStatus}: Job '${REPO_TITLE} Build' on -> ${env.CHECKOUT_BRANCH} [Build No: ${env.BUILD_NUMBER}]"
def summary = "${subject}"
def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>"""
// Override default values based on build status
if (buildStatus == 'STARTED') {
bitbucketStatusNotify(
buildState: 'INPROGRESS',
buildKey: 'build',
buildName: 'ci/jenkinsci: build',
buildDescription: 'Your tests started on JenkinsCI!',
repoSlug: "${REPO_SLUG}",
commitId: "${env.GIT_COMMIT_FULL}"
)
color = 'YELLOW'
colorCode = '#FFFF00'
} else if (buildStatus == 'SUCCESSFUL') {
dontNotify = true
bitbucketStatusNotify(
buildState: 'SUCCESSFUL',
buildKey: 'build',
buildName: 'ci/jenkinsci: build',
buildDescription: 'Your tests passed on JenkinsCI!',
repoSlug: "${REPO_SLUG}",
commitId: "${env.GIT_COMMIT_FULL}"
)
summary = ":white_check_mark: <${env.RUN_DISPLAY_URL}|ci/jenkinsci: ${BUILD_TITLE}> *passed* \nYour test passed on JenkinsCI!\n>`${env.GIT_COMMIT}` Merged in ${env.GIT_BRANCH}"
color = 'GREEN'
colorCode = '#00FF00'
} else {
bitbucketStatusNotify(
buildState: 'FAILED',
buildKey: 'build',
buildName: 'ci/jenkinsci: build',
buildDescription: 'Your tests failed on JenkinsCI!',
repoSlug: "${REPO_SLUG}",
commitId: "${env.GIT_COMMIT_FULL}"
)
summary = ":no_entry: <${env.RUN_DISPLAY_URL}|ci/jenkinsci: ${BUILD_TITLE}> *failed* \nYour tests failed on JenkinsCI!\n>`${env.GIT_COMMIT}` Merged in ${env.GIT_BRANCH}"
color = 'RED'
colorCode = '#FF0000'
}
// Send notifications
if(!dontNotify) {
slackSend (color: colorCode, message: summary)
}
}