-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
107 lines (95 loc) · 4.08 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
node (label: 'linux') {
checkout scm
withEnv([
"""COMMIT_AUTHOR=${sh(
returnStdout: true,
script: 'git show -s --pretty=%an'
).trim()}""",
"""COMMIT_HASH_SHORT=${sh(
returnStdout: true,
script: 'git rev-parse --short HEAD'
).trim()}""",
"""COMMIT_SUBJECT=${sh(
returnStdout: true,
script: 'git show -s --format=%B'
).trim()}""",
"CLOUDINARY_CLOUD_NAME=strawberryfair",
"CLOUDINARY_API_KEY=631814456675255",
"HOME=${WORKSPACE}"
]) {
try {
stage('Merge into prod') {
sh 'git checkout -f production'
sh 'git merge --no-ff -X theirs origin/master'
}
docker.image('node:12.13').inside {
dir('frontend'){
stage('Build') {
sh 'node --version'
sh 'npm ci'
}
stage('Test') {
withCredentials([string(credentialsId: 'CLOUDINARY_API_SECRET', variable: 'CLOUDINARY_API_SECRET')]) {
sh 'npm run lint'
sh 'npm run test'
sh 'npm run build'
}
}
}
}
stage('Deploy') {
echo 'Tests successful.'
dir('frontend') {
echo 'Committing build artefact.'
try {
sh 'git rm .gitignore'
} catch (e) {
echo 'No .gitignore file found'
}
sh 'git add public'
sh 'git commit --amend --no-edit'
sh 'git push -f origin HEAD:production'
}
}
} catch (e) {
currentBuild.result = 'FAILED'
} finally {
stage('Notify') {
currentBuild.result = currentBuild.result ?: 'SUCCESS'
// Notify via slack
if (currentBuild.result == 'SUCCESS') {
color = 'GREEN'
colorCode = '#00FF00'
echo 'Successfully executed!'
notifySlack(colorCode, 'Success! :)', COMMIT_AUTHOR, COMMIT_HASH_SHORT, COMMIT_SUBJECT)
} else {
def colorName = 'RED'
def colorCode = '#FF0000'
echo 'Unsuccessful'
notifySlack(colorCode, '@here Failure! :(', COMMIT_AUTHOR, COMMIT_HASH_SHORT, COMMIT_SUBJECT)
// Notify via emails
emailext body: """${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}.
\n${getCommitInfoMessage(COMMIT_AUTHOR, COMMIT_HASH_SHORT, COMMIT_SUBJECT)}
\nMore info at: ${env.BUILD_URL}""",
subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}",
to: "[email protected]"
}
}
stage('Clean') {
deleteDir()
}
}
}
}
def notifySlack(color, message, commitAuthor, commitHashShort, commitSubject) {
withCredentials([string(credentialsId: 'slack-token', variable: 'SLACKTOKEN')]) {
slackSend color: color,
teamDomain: "softwire",
channel: "#team-strawberryfair-build",
token: "$SLACKTOKEN",
message: """*${message}* - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>). \n${getCommitInfoMessage(commitAuthor, commitHashShort, commitSubject)} """
}
}
def getCommitInfoMessage(commitAuthor, commitHashShort, commitSubject) {
return "Build triggered by ${commitAuthor}'s commit ${commitHashShort}: ${commitSubject}"
}