-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
189 lines (165 loc) · 6.23 KB
/
Jenkinsfile
File metadata and controls
189 lines (165 loc) · 6.23 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
#!groovy
timestamps {
def pennsieveNexusCreds = usernamePassword(
credentialsId: 'pennsieve-nexus-ci-login',
usernameVariable: 'PENNSIEVE_NEXUS_USER',
passwordVariable: 'PENNSIEVE_NEXUS_PW'
)
node('executor') {
checkout scm
def commitHash = sh(returnStdout: true, script: 'git rev-parse HEAD | cut -c-7').trim()
def imageTag = "${env.BUILD_NUMBER}-${commitHash}"
def remoteCache = "remote-cache-${imageTag}"
def sbt = "sbt -Dsbt.log.noformat=true -Dversion=$imageTag -Dremote-cache=$remoteCache"
try {
stage('Build DB Image') {
timeout(20) {
withCredentials([pennsieveNexusCreds]) {
sh "ENVIRONMENT=jenkins ./build-postgres.sh"
}
}
sh 'docker-compose down'
}
stage('Build') {
withCredentials([pennsieveNexusCreds]) {
sh "${sbt} clean compile pushRemoteCache"
}
stash name: "${remoteCache}", includes: "${remoteCache}/**/*"
}
if (env.BRANCH_NAME != 'main') {
stage('Test') {
unstash name: "${remoteCache}"
withCredentials([pennsieveNexusCreds]) {
try {
sh "${sbt} clean pullRemoteCache test"
} finally {
junit '**/target/test-reports/*.xml'
}
}
}
} else {
stage('Publish Jars') {
def jars = [
'bf-akka-http',
'bf-aws',
'core',
'core-clients',
'core-models',
'message-templates',
'migrations'
]
def publishJarSteps = jars.collectEntries {
["${it}" : generatePublishJarStep(it, sbt, pennsieveNexusCreds, remoteCache)]
}
publishJarSteps.failFast = true
parallel publishJarSteps
}
def services = [
'admin',
'api',
'authorization-service',
'etl-data-cli',
'jobs'
]
def containers = services + [
'migrations',
'organization-storage-migration',
'unused-organization-migration'
]
stage('Publish Containers') {
def publishContainerSteps = containers.collectEntries {
["${it}" : generatePublishContainerStep(it, sbt, imageTag, pennsieveNexusCreds, remoteCache)]
}
publishContainerSteps.failFast = true
parallel publishContainerSteps
}
stage('Run Migrations') {
build job: "Migrations/dev-migrations/dev-postgres-migrations",
parameters: [
string(name: 'IMAGE_TAG', value: imageTag)
]
}
stage('Deploy') {
def deploySteps = services.collectEntries {
["${it}" : generateDeployStep(it, imageTag)]
}
deploySteps.failFast = true
parallel deploySteps
}
// stage('Python Client Tests') {
// build job: 'python-client-ci'
// }
}
} catch (e) {
currentBuild.result = 'FAILED'
throw e
} finally {
notifyBuild(currentBuild.result)
}
}
}
// Generate parallel deploy steps
def generateDeployStep(String service, String imageTag) {
return {
build job: "service-deploy/pennsieve-non-prod/us-east-1/dev-vpc-use1/dev/${service}",
parameters: [
string(name: 'IMAGE_TAG', value: imageTag),
string(name: "TERRAFORM_ACTION", value: "apply")
]
}
}
// Generate parallel container publish steps
def generatePublishContainerStep(String service, String sbt, String imageTag, creds, String remoteCache) {
return {
node('executor') {
checkout scm
unstash name: "${remoteCache}"
// Handle exceptions to standard service deploys
def images, tag, buildPath
switch(service) {
default:
(images, tag) = [[service], imageTag]
break
}
withCredentials([creds]) {
sh "${sbt} clean pullRemoteCache ${service}/docker"
}
for (image in images) {
if (['clamd', 'discover-pgdump-postgres'].contains(image)) {
sh "docker build --no-cache --tag pennsieve/${image}:latest ${buildPath}"
}
sh "docker tag pennsieve/${image}:latest pennsieve/${image}:${tag}"
sh "docker push pennsieve/${image}:latest"
sh "docker push pennsieve/${image}:${tag}"
}
}
}
}
// Generate parallel jar publish steps
def generatePublishJarStep(String jar, String sbt, creds, String remoteCache) {
return {
node('executor') {
checkout scm
unstash name: "${remoteCache}"
withCredentials([creds]) {
retry(3) {
sh "${sbt} clean pullRemoteCache ${jar}/publish"
}
}
}
}
}
// Slack build status notifications
def notifyBuild(String buildStatus) {
// Build status of null means successful
buildStatus = buildStatus ?: 'SUCCESS'
def authorName = sh(returnStdout: true, script: 'git --no-pager show --format="%an" --no-patch').trim()
def color
def message = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL}) by ${authorName}"
if (buildStatus == 'SUCCESS') {
color = '#00FF00' // Green
} else {
color = '#FF0000' // Red
}
slackSend(color: color, message: message)
}