-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
273 lines (245 loc) · 8.81 KB
/
Jenkinsfile
File metadata and controls
273 lines (245 loc) · 8.81 KB
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
node {
def clusters = [
"playground": "play-eth-sync-sidechain",
"staging": "stag-eth-sync-sidechain",
"production": "prod-eth-sync-sidechain"
]
def repository = "props-eth-sync"
// The name of the service
def serviceName = "props-eth-sync"
// The active deployments on kubernetes
def deployments = ["api"]
def error = null
def environment = null
try {
timeout(120) {
cloneRepository()
setupEnvironment(repository)
if (isMasterBranch()) {
environment = "production"
if (isLastCommitFixingIssue()) {
announceFlow("Emergency flow, targeting ${environment}")
installDependencies()
buildReport()
publishReport()
deployServices(serviceName, environment, clusters[environment], deployments)
slackSend color: 'good', message: "@${env.AUTHOR} → *${env.SLACK_NAME}* was deployed to *${environment}*."
} else {
announceFlow("Full flow, targeting ${environment}")
installDependencies()
buildReport()
publishReport()
deployServices(serviceName, environment, clusters[environment], deployments)
slackSend color: 'good', message: "@${env.AUTHOR} → *${env.SLACK_NAME}* was deployed to *${environment}*."
}
} else {
environment = getEnvironment()
if (isCurrentBranchFixingIssue()) {
announceFlow("Emergency flow, targeting ${environment}")
installDependencies()
buildReport()
publishReport()
deployServices(serviceName, environment, clusters[environment], deployments)
slackSend color: 'good', message: "@${env.AUTHOR} → *${env.SLACK_NAME}* was deployed to *${environment}*."
} else {
announceFlow("Full flow, targeting ${environment}")
installDependencies()
buildReport()
publishReport()
deployServices(serviceName, environment, clusters[environment], deployments)
slackSend color: 'good', message: "@${env.AUTHOR} → *${env.SLACK_NAME}* was deployed to *${environment}*."
}
}
currentBuild.result = "SUCCESS"
}
} catch (e) {
error = e
stage('Notifying failure') {
currentBuild.result = "FAILURE"
slackSend color: 'danger', message: "@${env.AUTHOR} → *${env.SLACK_NAME}* failed.\nDetails here: ${e}\n${env.BlUE_OCEAN_URL}."
}
} finally {
cleanArtifacts()
if (error) {
throw error
}
}
}
def setupEnvironment(repository) {
def node = tool name: 'node-10', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'
env.PATH = "${node}/bin:${env.PATH}"
env.SLACK_NAME = "${repository}:${env.BRANCH_NAME}"
env.REPOSITORY = "${repository}"
env.JOB_SHORT_NAME = env.JOB_NAME.replaceFirst(/YouNow\/${repository}\//, "")
env.BlUE_OCEAN_URL = "${env.JENKINS_URL}blue/organizations/jenkins/YouNow%2F${repository}/detail/${env.JOB_SHORT_NAME}/${env.BUILD_NUMBER}/pipeline"
env.AUTHOR = sh(
script: "git --no-pager show -s --format='%an' HEAD",
returnStdout: true
).trim().split(' ')[0].toLowerCase()
}
def deployServices(serviceName, environment, cluster, deployments) {
def stepsForParallel = [:]
def stepName = "Deploying ${serviceName}"
stepsForParallel[stepName] = {
buildImage(serviceName, environment)
pushImage(serviceName, environment)
deployImage(serviceName, environment, cluster, deployments)
}
return stage('Deploying Services') {
parallel stepsForParallel
}
}
def cloneRepository() {
return stage('Cloning Repository') {
checkout scm
}
}
def isMasterBranch() {
return env.BRANCH_NAME == 'master'
}
def getEnvironment() {
if (env.BRANCH_NAME == 'playground') {
return "playground"
}
return "staging"
}
def isCurrentBranchFixingIssue() {
return env.BRANCH_NAME.startsWith('fix/')
}
def isLastCommitFixingIssue() {
def lastCommit = sh (script: "git log -1 | grep 'fix/'", returnStatus: true)
return lastCommit == 1
}
def announceFlow(flow) {
return stage('Computing Flow') {
echo "Selected Flow: ${flow}"
slackSend color: 'good', message: "@${env.AUTHOR} → processing *${env.SLACK_NAME}*. ${flow}.\n${env.BlUE_OCEAN_URL}."
}
}
def installDependencies() {
return stage('Fetching Dependencies') {
sh 'yarn install'
}
}
def mergeInto(branch) {
return stage("Merging into ${branch}") {
sh 'git checkout staging'
sh "git merge ${env.BRANCH_NAME}"
sh "git push"
}
}
def buildReport() {
return stage('Building Report') {
parallel "Checking tests": {
try {
sh 'npm run test'
} catch (e) {
} finally {
}
}, "Checking code quality": {
try {
sh 'npm run linter'
} catch (e) {
} finally {
}
}, "Checking test coverage": {
sh 'npm run cover'
}, "Transpiling to Plain Old Javascript": {
sh 'npm run transpile'
}, "Generating Documentation": {
sh 'npm run doc'
}, failFast: true|false
}
}
def publishReport() {
return stage('Publishing Report') {
// publishHTML target: [
// allowMissing: false,
// alwaysLinkToLastBuild: false,
// keepAll: true,
// reportDir: 'reports',
// reportFiles: 'linter.html',
// reportName: 'Linter Report'
// ]
// publishHTML target: [
// allowMissing: false,
// alwaysLinkToLastBuild: false,
// keepAll: true,
// reportDir: 'coverage/lcov-report',
// reportFiles: 'index.html',
// reportName: 'Coverage Report'
// ]
}
}
def buildImage(service, environment) {
return stage('Building Image') {
def localTag = "${env.BUILD_NUMBER}"
def repo = "${service}-${environment}-sidechain"
sh "docker build -t ${repo}:${localTag} ."
}
}
def pushImage(service, environment) {
return stage("Pushing Image to ${environment}") {
def localTag = "${env.BUILD_NUMBER}"
def remoteTag = "${env.BUILD_NUMBER}"
def repo = "${service}-${environment}-sidechain"
// sh "docker tag ${repo}:${localTag} ${service}:latest"
// sh "docker tag ${repo}:${localTag} 774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:${remoteTag}"
// sh "docker tag ${repo}:${localTag} 774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:latest"
// sh 'eval $(aws ecr get-login --region us-east-1 --no-include-email)'
// sh "docker push 774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:${remoteTag}"
// sh "docker push 774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:latest"
if (environment == 'production') {
sh "docker tag ${repo}:${localTag} propsprojectservices/props-ethsync:latest"
withDockerRegistry([ credentialsId: "propsdockerhub", url: "" ]) {
sh "docker push propsprojectservices/props-ethsync:latest"
}
} else {
sh "docker tag ${repo}:${localTag} propsprojectservices/props-ethsync:${environment}"
withDockerRegistry([ credentialsId: "propsdockerhub", url: "" ]) {
sh "docker push propsprojectservices/props-ethsync:${environment}"
}
}
}
}
def deployImage(serviceName, environment, cluster, deployments) {
return stage("Deploying Image to ${environment}") {
def initialTag = "latest"
def newTag = "${env.BUILD_NUMBER}"
def repo = "${serviceName}-${environment}-sidechain"
// for (int i = 0; i < deployments.size(); i++) {
// def deployment = deployments[i]
// sh "kubectl --kubeconfig=/var/lib/jenkins/kubeconfig set image cronjob/submit-rewards submit-rewards=774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:${env.BUILD_NUMBER}"
// sh "kubectl --kubeconfig=/var/lib/jenkins/kubeconfig set image cronjob/sync-transfers sync-transfers=774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:${env.BUILD_NUMBER}"
// sh "kubectl --kubeconfig=/var/lib/jenkins/kubeconfig set image cronjob/state-delta-catchup state-delta-catchup=774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:${env.BUILD_NUMBER}"
// sh "kubectl --kubeconfig=/var/lib/jenkins/kubeconfig set image deployment/${serviceName}-${deployment} ${serviceName}-${deployment}=774122189772.dkr.ecr.us-east-1.amazonaws.com/${repo}:${env.BUILD_NUMBER}"
// }
}
}
def waitForValidation() {
return stage('Waiting for Validation') {
def userInput = input(
id: 'userInput',
message: 'Deploy to Production?',
parameters: [[
$class: 'TextParameterDefinition',
defaultValue: '',
description: 'Note/Comment',
name: 'shouldDeployToProduction'
]]
)
echo ("Env: "+userInput)
}
}
def cleanArtifacts() {
return stage('Cleaning Artifacts') {
// Delete current workspace
sh('rm -rf *')
// Clean all the previous images but the latest, to keep a cache
try {
sh "docker images -q -a | grep -vE \"\$(docker images -a | grep \".*latest\" | awk \"{print \$3}\")\" | xargs docker rmi -f"
} catch (e) {
} finally {
}
}
}