-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
253 lines (235 loc) · 8.41 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
#!groovy
@Library('jenkins-pipeline-shared') _
pipeline {
environment {
RELEASE_TYPE = "PATCH"
BRANCH_DEV = "develop"
BRANCH_TEST = "release"
BRANCH_PROD = "master"
DEPLOY_DEV = "dev"
DEPLOY_TEST = "test"
DEPLOY_PROD = "beta"
GIT_TYPE = "Github"
GIT_CREDS = "github-bi-user"
GITLAB_CREDS = "bi-gitlab-id"
ORGANIZATION = "ons"
TEAM = "bi"
MODULE_NAME = "business-index-api"
}
options {
skipDefaultCheckout()
buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30'))
timeout(time: 30, unit: 'MINUTES')
timestamps()
}
agent any
stages {
stage('Checkout'){
agent any
steps{
deleteDir()
checkout scm
stash name: 'app'
sh "$SBT version"
script {
version = '1.0.' + env.BUILD_NUMBER
currentBuild.displayName = version
env.NODE_STAGE = "Checkout"
}
}
}
stage('Build'){
agent any
steps {
colourText("info", "Building ${env.BUILD_ID} on ${env.JENKINS_URL} from branch ${env.BRANCH_NAME}")
script {
env.NODE_STAGE = "Build"
sh '''
$SBT clean compile universal:packageBin coverage test coverageReport
'''
stash name: 'compiled'
if (BRANCH_NAME == BRANCH_DEV) {
env.DEPLOY_NAME = DEPLOY_DEV
sh "cp target/universal/${ORGANIZATION}-${MODULE_NAME}-*.zip ${DEPLOY_DEV}-${ORGANIZATION}-${MODULE_NAME}.zip"
}
else if (BRANCH_NAME == BRANCH_TEST) {
env.DEPLOY_NAME = DEPLOY_TEST
sh "cp target/universal/${ORGANIZATION}-${MODULE_NAME}-*.zip ${DEPLOY_TEST}-${ORGANIZATION}-${MODULE_NAME}.zip"
}
else if (BRANCH_NAME == BRANCH_PROD) {
env.DEPLOY_NAME = DEPLOY_PROD
sh "cp target/universal/${ORGANIZATION}-${MODULE_NAME}-*.zip ${DEPLOY_PROD}-${ORGANIZATION}-${MODULE_NAME}.zip"
}
else {
colourText("info", "Not a deployable Git banch!")
}
}
}
}
stage('Static Analysis') {
agent any
steps {
parallel (
"Unit" : {
colourText("info","Running unit tests")
sh "$SBT test"
},
"Style" : {
colourText("info","Running style tests")
sh """
$SBT scalastyleGenerateConfig
$SBT scalastyle
"""
},
"Additional" : {
colourText("info","Running additional tests")
sh "$SBT scapegoat"
}
)
}
post {
always {
script {
env.NODE_STAGE = "Static Analysis"
}
}
success {
colourText("info","Generating reports for tests")
// junit '**/target/test-reports/*.xml'
step([$class: 'CoberturaPublisher', coberturaReportFile: '**/target/scala-2.11/coverage-report/*.xml'])
step([$class: 'CheckStylePublisher', pattern: 'target/scalastyle-result.xml, target/scala-2.11/scapegoat-report/scapegoat-scalastyle.xml'])
}
failure {
colourText("warn","Failed on Tests.")
}
}
}
// bundle all libs and dependencies
stage ('Bundle') {
agent any
when {
anyOf {
branch BRANCH_DEV
branch BRANCH_TEST
branch BRANCH_PROD
}
}
steps {
script {
env.NODE_STAGE = "Bundle"
}
colourText("info", "Bundling....")
dir('conf') {
git(url: "$GITLAB_URL/BusinessIndex/${MODULE_NAME}.git", credentialsId: GITLAB_CREDS, branch: "develop")
}
// stash name: "zip"
}
}
stage("Releases"){
agent any
when {
anyOf {
branch BRANCH_DEV
branch BRANCH_TEST
branch BRANCH_PROD
}
}
steps {
script {
env.NODE_STAGE = "Releases"
currentTag = getLatestGitTag()
colourText("info", "Found latest tag: ${currentTag}")
newTag = IncrementTag( currentTag, RELEASE_TYPE )
colourText("info", "Generated new tag: ${newTag}")
//push(newTag, currentTag)
}
}
}
stage ('Package and Push Artifact') {
agent any
when {
branch BRANCH_PROD
}
steps {
script {
env.NODE_STAGE = "Package and Push Artifact"
}
sh """
$SBT clean compile package
$SBT clean compile assembly
"""
colourText("success", 'Package.')
}
}
stage('Deploy'){
agent any
when {
anyOf {
branch BRANCH_DEV
branch BRANCH_TEST
branch BRANCH_PROD
}
}
steps {
script {
env.NODE_STAGE = "Deploy"
}
milestone(1)
lock('Deployment Initiated') {
colourText("info", 'deployment in progress')
deploy()
colourText("success", 'Deploy.')
}
}
}
stage('Integration Tests') {
agent any
when {
anyOf {
branch BRANCH_DEV
branch BRANCH_TEST
}
}
steps {
script {
env.NODE_STAGE = "Integration Tests"
}
unstash 'compiled'
sh "$SBT box:test"
colourText("success", 'Integration Tests - For Release or Dev environment.')
}
}
}
post {
always {
script {
colourText("info", 'Post steps initiated')
deleteDir()
}
}
success {
colourText("success", "All stages complete. Build was successful.")
sendNotifications currentBuild.result, "\$SBR_EMAIL_LIST"
}
unstable {
colourText("warn", "Something went wrong, build finished with result ${currentResult}. This may be caused by failed tests, code violation or in some cases unexpected interrupt.")
sendNotifications currentBuild.result, "\$SBR_EMAIL_LIST", "${env.NODE_STAGE}"
}
failure {
colourText("warn","Process failed at: ${env.NODE_STAGE}")
sendNotifications currentBuild.result, "\$SBR_EMAIL_LIST", "${env.NODE_STAGE}"
}
}
}
def push (String newTag, String currentTag) {
echo "Pushing tag ${newTag} to Gitlab"
GitRelease( GIT_CREDS, newTag, currentTag, "${env.BUILD_ID}", "${env.BRANCH_NAME}", GIT_TYPE)
}
def deploy () {
CF_SPACE = "${env.DEPLOY_NAME}".capitalize()
CF_ORG = "${TEAM}".toUpperCase()
echo "Deploying Api app to ${env.DEPLOY_NAME}"
// withCredentials([string(credentialsId: CF_CREDS, variable: 'APPLICATION_SECRET')]) {
deployToCloudFoundry("${TEAM}-${env.DEPLOY_NAME}-cf", "${CF_ORG}", "${CF_SPACE}", "${env.DEPLOY_NAME}-${MODULE_NAME}", "${env.DEPLOY_NAME}-${ORGANIZATION}-${MODULE_NAME}.zip", "conf/${env.DEPLOY_NAME}/manifest.yml")
// }
}