forked from ilkinabdullayev/api-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
325 lines (292 loc) · 12 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* The name of the master branch
*/
def MASTER_BRANCH = "master"
/**
* Is this a release branch? Temporary workaround that won't break everything horribly if we merge.
*/
def RELEASE_BRANCH = false
/**
* The result string for a successful build
*/
def BUILD_SUCCESS = 'SUCCESS'
/**
* The result string for an unstable build
*/
def BUILD_UNSTABLE = 'UNSTABLE'
/**
* The result string for a failed build
*/
def BUILD_FAILURE = 'FAILURE'
/**
* The user's name for git commits
*/
def GIT_USER_NAME = 'zowe-robot'
/**
* The user's email address for git commits
*/
def GIT_USER_EMAIL = '[email protected]'
/**
* The base repository url for github
*/
def GIT_REPO_URL = 'https://github.com/zowe/api-layer.git'
/**
* The credentials id field for the authorization token for GitHub stored in Jenkins
*/
def GIT_CREDENTIALS_ID = 'zowe-robot-github'
/**
* A command to be run that gets the current revision pulled down
*/
def GIT_REVISION_LOOKUP = 'git log -n 1 --pretty=format:%h'
/**
* The credentials id field for the artifactory username and password
*/
def ARTIFACTORY_CREDENTIALS_ID = 'zowe.jfrog.io'
/**
* The email address for the artifactory
*/
def ARTIFACTORY_EMAIL = GIT_USER_EMAIL
// Setup conditional build options. Would have done this in the options of the declarative pipeline, but it is pretty
// much impossible to have conditional options based on the branch :/
def opts = []
if (BRANCH_NAME == MASTER_BRANCH) {
// Only keep 20 builds
opts.push(buildDiscarder(logRotator(numToKeepStr: '20')))
// Concurrent builds need to be disabled on the master branch because
// it needs to actively commit and do a build. There's no point in publishing
// twice in quick succession
opts.push(disableConcurrentBuilds())
} else {
if (BRANCH_NAME.equals("1.0.0")){
RELEASE_BRANCH = true
}
// Only keep 5 builds on other branches
opts.push(buildDiscarder(logRotator(numToKeepStr: '5')))
}
properties(opts)
pipeline {
agent {
label 'apiml-jenkins-agent-swarm'
}
options {
timestamps ()
}
parameters {
string(name: 'CHANGE_CLASS', defaultValue: '', description: 'Override change class - for testing (empty, doc, full, api-catalog)', )
booleanParam(name: 'PUBLISH_PR_ARTIFACTS', defaultValue: 'false', description: 'If true it will publish the pull requests artifacts', )
}
stages {
stage('Classify changes') {
steps {
script {
if (params.CHANGE_CLASS != '') {
changeClass = params.CHANGE_CLASS
}
else {
ccOutput = sh(returnStdout: true, script: "python3 scripts/classify_changes.py")
echo "${ccOutput}"
changeClass = ccOutput.trim().tokenize().last()
}
allowEmptyArchive = changeClass in ['empty', 'doc'] && !BRANCH_NAME.equals(MASTER_BRANCH) && !RELEASE_BRANCH
}
echo "Change class: ${changeClass}"
sh "echo ${changeClass} > .change_class"
}
}
stage('Copy archives from last successful build') {
when { expression { changeClass != 'full' } }
steps {
script {
try {
copyArtifacts(projectName: JOB_NAME)
}
catch (error) {
copyArtifacts(projectName: 'API_Mediation/master')
}
}
sh "echo ${changeClass} > .change_class"
}
}
stage ('API Catalog build') {
when { expression { changeClass in ['api-catalog'] } }
stages {
stage('Package api-layer source code') {
steps {
sh "git archive --format tar.gz -9 --output api-layer.tar.gz HEAD"
}
}
stage ('Build API Catalog') {
steps {
timeout(time: 10, unit: 'MINUTES') {
sh './gradlew :api-catalog-services:build'
}
}
}
}
}
stage ('Full build') {
when { expression { changeClass in ['full'] } }
stages {
stage('Clean') {
when { expression { BRANCH_NAME.equals(MASTER_BRANCH) || RELEASE_BRANCH } }
steps {
sh "./gradlew clean"
}
}
stage('Package api-layer source code') {
steps {
sh "git archive --format tar.gz -9 --output api-layer.tar.gz HEAD"
}
}
stage('Build and unit test with coverage') {
steps {
timeout(time: 20, unit: 'MINUTES') {
sh './gradlew build coverage'
}
}
}
stage('Publish coverage reports') {
steps {
publishHTML(target: [
allowMissing : false,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : 'build/reports/jacoco/jacocoFullReport/html',
reportFiles : 'index.html',
reportName : "Java Coverage Report"
])
publishHTML(target: [
allowMissing : false,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : 'api-catalog-ui/frontend/coverage/lcov-report',
reportFiles : 'index.html',
reportName : "UI JavaScript Test Coverage"
])
}
}
stage('Publish snapshot version to Artifactory for master') {
when {
expression {
return BRANCH_NAME.equals(MASTER_BRANCH) || RELEASE_BRANCH;
}
}
steps {
withCredentials([usernamePassword(credentialsId: ARTIFACTORY_CREDENTIALS_ID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh '''
./gradlew publishAllVersions -Pzowe.deploy.username=$USERNAME -Pzowe.deploy.password=$PASSWORD
'''
}
}
}
stage('Publish snapshot version to Artifactory for Pull Request') {
when {
expression {
return BRANCH_NAME.contains("PR-") && params.PUBLISH_PR_ARTIFACTS;
}
}
steps {
withCredentials([usernamePassword(credentialsId: ARTIFACTORY_CREDENTIALS_ID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh '''
sudo sed -i '/version=/ s/-SNAPSHOT/-'"$BRANCH_NAME"'-SNAPSHOT/' ./gradle.properties
./gradlew publishAllVersions -Pzowe.deploy.username=$USERNAME -Pzowe.deploy.password=$PASSWORD -PpullRequest=$env.BRANCH_NAME
'''
}
}
}
}
}
/************************************************************************
* STAGE
* -----
* SonarQube Scanner
*
* EXECUTION CONDITIONS
* --------------------
* - SHOULD_BUILD is true
* - The build is still successful and not unstable
*
* DESCRIPTION
* -----------
* Runs the sonar-scanner analysis tool, which submits the source, test results,
* and coverage results for analysis in our SonarQube server.
* TODO: This step does not yet support branch or PR submissions properly.
***********************************************************************/
stage('sonar') {
steps {
withSonarQubeEnv('sonarcloud-server') {
// Per Sonar Doc - It's important to add --info because of SONARJNKNS-281
sh "./gradlew --info sonarqube -Psonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_AUTH_TOKEN}"
}
}
}
stage('Publish UI test results') {
steps {
publishHTML(target: [
allowMissing : false,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : 'api-catalog-ui/frontend/test-results',
reportFiles : 'test-report-unit.html',
reportName : "UI Unit Test Results"
])
}
}
stage ('Codecov') {
when { expression { changeClass in ['full', 'api-catalog'] } }
steps {
withCredentials([usernamePassword(credentialsId: 'Codecov', usernameVariable: 'CODECOV_USERNAME', passwordVariable: 'CODECOV_TOKEN')]) {
sh 'curl -s https://codecov.io/bash | bash -s'
}
}
}
}
post {
always {
junit allowEmptyResults: true, testResults: '**/test-results/**/*.xml'
archiveArtifacts '.change_class'
publishHTML (target: [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'gateway-service/build/reports/tests/test',
reportFiles: 'index.html',
reportName: "Unit Tests Report - gateway-service"
])
publishHTML (target: [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'discovery-service/build/reports/tests/test',
reportFiles: 'index.html',
reportName: "Unit Tests Report - discovery-service"
])
publishHTML (target: [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'api-catalog-services/build/reports/tests/test',
reportFiles: 'index.html',
reportName: "Unit Tests Report - api-catalog-services"
])
}
success {
archiveArtifacts artifacts: 'api-catalog-services/build/libs/**/*.jar'
archiveArtifacts artifacts: 'discoverable-client/build/libs/**/*.jar'
archiveArtifacts artifacts: 'discovery-service/build/libs/**/*.jar'
archiveArtifacts artifacts: 'gateway-service/build/libs/**/*.jar'
archiveArtifacts artifacts: 'integration-enabler-spring-v1-sample-app/build/libs/**/*.jar'
archiveArtifacts artifacts: 'api-layer.tar.gz'
withCredentials([usernamePassword(credentialsId: 'zowe-robot-github', usernameVariable: 'ZOWE_GITHUB_USERID', passwordVariable: 'ZOWE_GITHUB_APIKEY')]) {
sh """
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user
python3 -m pip install --user requests
python3 -m pip freeze
python3 -c 'import requests'
python3 scripts/post_actions.py $env.BRANCH_NAME $ZOWE_GITHUB_APIKEY $changeClass
"""
}
}
}
}