-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
217 lines (206 loc) · 6.57 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
def getFailureCount() {
def curBuild = currentBuild.getPreviousBuild()
def count = 0
while (curBuild != null && curBuild.result != 'SUCCESS'){
count++
curBuild = curBuild.getPreviousBuild()
}
return count
}
def sendSlackFailureMessage(String stage) {
def message =
"<http://192.168.11.49:8080/job/JelloServers|jello-file-manager> » stage `${stage}` failed " +
"<${env.JOB_URL}|${env.GIT_BRANCH}> » " +
"<${env.RUN_DISPLAY_URL}|#${env.BUILD_NUMBER}>."
def failCount = getFailureCount()
if (failCount > 0) {
message += " This job has failed `${failCount+1}` times."
}
slackSend channel: '#backend-ci', color: 'danger', message: message
if (env.gitlabActionType == "PUSH") {
message += " <!here>"
slackSend channel: '#jello_backend', color: 'danger', message: message
}
}
def sendSlackFixedMessage() {
def message =
"<http://192.168.11.49:8080/job/JelloServers|jello-file-manager> » " +
"<${env.JOB_URL}|${env.GIT_BRANCH}> » " +
"<${env.RUN_DISPLAY_URL}|#${env.BUILD_NUMBER}> is fixed " +
"after ${getFailureCount()} failed builds."
slackSend channel: '#jello_backend_ci', color: 'good', message: message
}
def setEnv(String env) {
switch (env) {
case "origin/develop":
return "dev"
case "origin/master":
return "rc"
default:
return "local"
}
}
def setKubeConfig(String env) {
switch (env) {
case "origin/develop":
return "/Users/jello/.jenkins/workspace/JelloServers/jello-kubernetes/ansible/configs/config.jello.dev"
case "origin/master":
return "/Users/jello/.jenkins/workspace/JelloServers/jello-kubernetes/ansible/configs/config.jello.rc"
default:
return "/Users/jello/.jenkins/workspace/JelloServers/jello-kubernetes/ansible/configs/config.jello.minikube"
}
}
def setGCRAccount(String env) {
switch (env) {
case "origin/develop":
return "[email protected]"
case "origin/master":
return "[email protected]"
default:
return ""
}
}
def setGKEAccount(String env) {
switch (env) {
case "origin/develop":
return "[email protected]"
case "origin/master":
return "[email protected]"
default:
return ""
}
}
pipeline {
agent any
options {
gitLabConnection('Gitlab')
}
environment {
ENV = setEnv(env.GIT_BRANCH)
KUBECONFIG = setKubeConfig(env.GIT_BRANCH)
GCR_ACCOUNT = setGCRAccount(env.GIT_BRANCH)
GKE_ACCOUNT = setGKEAccount(env.GIT_BRANCH)
}
post {
fixed {
sendSlackFixedMessage()
}
always {
script {
if (env.gitlabActionType == 'MERGE') {
junit '*.xml'
}
}
}
}
stages {
stage('Prepare') {
when {
expression { -> env.gitlabActionType == 'MERGE'}
}
steps {
gitlabCommitStatus('Prepare') {
script {
sh "go get -u github.com/tebeka/go2xunit"
}
}
}
post {
failure {
sendSlackFailureMessage 'Prepare'
}
}
}
stage('Testing') {
parallel {
stage('Unit Test') {
when {
expression { -> env.gitlabActionType == 'MERGE'}
}
steps {
gitlabCommitStatus('Unit Test') {
script {
sh 'make src.test | /Users/jello/go/bin/go2xunit -output junit.xml'
}
}
}
post {
failure {
sendSlackFailureMessage 'Unit Test'
}
}
}
stage('Build Test') {
when {
expression { -> env.gitlabActionType == 'MERGE'}
}
steps {
gitlabCommitStatus('Build Test') {
script {
sh 'eval $(minikube docker-env) && make dockerfiles.build-local'
}
}
}
post {
failure {
sendSlackFailureMessage 'Build Test'
}
}
}
}
}
stage('Deploy Local') {
when {
expression { -> env.gitlabActionType == 'MERGE'}
}
steps {
gitlabCommitStatus('Deploy Local') {
script {
sh 'make apps.install-local'
}
}
}
post {
failure {
sendSlackFailureMessage 'Deploy Local'
}
}
}
stage('Build GCR Image') {
when {
expression { -> env.gitlabActionType == 'PUSH'}
}
steps {
gitlabCommitStatus('Build GCR Image') {
script {
sh "export PATH=$PATH:/Users/jello/google-cloud-sdk/bin && gcloud config set account ${env.GCR_ACCOUNT}"
sh "export PATH=$PATH:/Users/jello/google-cloud-sdk/bin && make dockerfiles.build-${env.ENV}"
}
}
}
post {
failure {
sendSlackFailureMessage 'Build GCR Image'
}
}
}
stage('Deploy GKE') {
when {
expression { -> env.gitlabActionType == 'PUSH'}
}
steps {
gitlabCommitStatus('Deploy GKE') {
script {
sh "export PATH=$PATH:/Users/jello/google-cloud-sdk/bin && gcloud config set account ${env.GKE_ACCOUNT}"
sh "export PATH=$PATH:/Users/jello/google-cloud-sdk/bin && make apps.install-${env.ENV}"
}
}
}
post {
failure {
sendSlackFailureMessage 'Deploy'
}
}
}
}
}