-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJenkinsfile
More file actions
335 lines (292 loc) · 12 KB
/
Jenkinsfile
File metadata and controls
335 lines (292 loc) · 12 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
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
326
327
328
329
330
331
332
333
334
335
// Copyright (c) 2024-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
/* groovylint-disable CompileStatic, LineLength, VariableTypeRequired */
// This Jenkinsfile defines internal MarkLogic build pipeline.
//Shared library definitions: https://github.com/marklogic/MarkLogic-Build-Libs/tree/1.0-declarative/vars
@Library('shared-libraries@1.0-declarative')
import groovy.json.JsonSlurperClassic
emailList = 'vitaly.korolev@progress.com, sumanth.ravipati@progress.com, peng.zhou@progress.com, barkha.choithani@progress.com, romain.winieski@progress.com'
emailSecList = 'Mahalakshmi.Srinivasan@progress.com'
gitCredID = 'marklogic-builder-github'
operatorRegistry = 'ml-marklogic-operator-dev.bed-artifactory.bedford.progress.com'
JIRA_ID = ''
JIRA_ID_PATTERN = /(?i)(MLE)-\d{3,6}/
operatorRepo = 'marklogic-kubernetes-operator'
timeStamp = new Date().format('yyyyMMdd')
branchNameTag = env.BRANCH_NAME.replaceAll('/', '-')
// Define local funtions
void preBuildCheck() {
// Initialize parameters as env variables as workaround for https://issues.jenkins-ci.org/browse/JENKINS-41929
evaluate """${ def script = ''; params.each { k, v -> script += "env.${k } = '''${v}'''\n" }; return script}"""
JIRA_ID = extractJiraID()
echo 'Jira ticket number: ' + JIRA_ID
if (env.GIT_URL) {
githubAPIUrl = GIT_URL.replace('.git', '').replace('github.com', 'api.github.com/repos')
echo 'githubAPIUrl: ' + githubAPIUrl
} else {
echo 'Warning: GIT_URL is not defined'
}
if (env.CHANGE_ID) {
if (prDraftCheck()) { sh 'exit 1' }
if (getReviewState().equalsIgnoreCase('CHANGES_REQUESTED')) {
echo 'PR changes requested. (' + reviewState + ') Aborting.'
sh 'exit 1'
}
}
// our VMs sometimes disable bridge traffic. this should help to restore it.
sh 'sudo modprobe br_netfilter'
sh 'sudo sh -c "echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables"'
}
@NonCPS
def extractJiraID() {
// Extract Jira ID from one of the environment variables
def match
if (env.CHANGE_TITLE) {
match = env.CHANGE_TITLE =~ JIRA_ID_PATTERN
}
else if (env.BRANCH_NAME) {
match = env.BRANCH_NAME =~ JIRA_ID_PATTERN
}
else if (env.GIT_BRANCH) {
match = env.GIT_BRANCH =~ JIRA_ID_PATTERN
}
else {
echo 'Warning: No Git title or branch available.'
return ''
}
try {
return match[0][0]
} catch (any) {
echo 'Warning: Jira ticket number not detected.'
return ''
}
}
def prDraftCheck() {
withCredentials([usernameColonPassword(credentialsId: gitCredID, variable: 'Credentials')]) {
PrObj = sh(returnStdout: true, script:'''
curl -s -u $Credentials -X GET ''' + githubAPIUrl + '''/pulls/$CHANGE_ID
''')
}
def jsonObj = new JsonSlurperClassic().parseText(PrObj.toString().trim())
return jsonObj.draft
}
def getReviewState() {
def reviewResponse
def commitHash
withCredentials([usernameColonPassword(credentialsId: gitCredID, variable: 'Credentials')]) {
reviewResponse = sh(returnStdout: true, script:'''
curl -s -u $Credentials -X GET ''' + githubAPIUrl + '''/pulls/$CHANGE_ID/reviews
''')
commitHash = sh(returnStdout: true, script:'''
curl -s -u $Credentials -X GET ''' + githubAPIUrl + '''/pulls/$CHANGE_ID
''')
}
def jsonObj = new JsonSlurperClassic().parseText(commitHash.toString().trim())
def commitId = jsonObj.head.sha
println(commitId)
def reviewState = getReviewStateOfPR reviewResponse, 2, commitId
echo reviewState
return reviewState
}
void resultNotification(status) {
def author, authorEmail, emailList
//add author of a PR to email list if available
if (env.CHANGE_AUTHOR) {
author = env.CHANGE_AUTHOR.toString().trim().toLowerCase()
authorEmail = getEmailFromGITUser author
emailList = params.emailList + ',' + authorEmail
} else {
emailList = params.emailList
}
jira_link = "https://progresssoftware.atlassian.net/browse/${JIRA_ID}"
email_body = "<b>Jenkins pipeline for</b> ${env.JOB_NAME} <br><b>Build Number: </b>${env.BUILD_NUMBER} <br><br><b>Build URL: </b><br><a href='${env.BUILD_URL}'>${env.BUILD_URL}</a>"
jira_email_body = "${email_body} <br><br><b>Jira URL: </b><br><a href='${jira_link}'>${jira_link}</a>"
if (JIRA_ID) {
def comment = [ body: "Jenkins pipeline build result: ${status}" ]
jiraAddComment site: 'JIRA', idOrKey: JIRA_ID, failOnError: false, input: comment
mail charset: 'UTF-8', mimeType: 'text/html', to: "${emailList}", body: "${jira_email_body}", subject: "🥷 ${status}: ${env.JOB_NAME} #${env.BUILD_NUMBER} - ${JIRA_ID}"
} else {
mail charset: 'UTF-8', mimeType: 'text/html', to: "${emailList}", body: "${email_body}", subject: "🥷 ${status}: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
}
}
void publishTestResults() {
junit allowEmptyResults:true, testResults: '**/test/test_results/*.xml'
archiveArtifacts artifacts: '**/test/test_results/*.xml', allowEmptyArchive: true
}
void runTests() {
sh "make test"
}
void runMinikubeSetup() {
sh """
make e2e-setup-minikube IMG=${operatorRepo}:${VERSION}
"""
}
void runE2eTests() {
sh """
make e2e-test IMG=${operatorRepo}:${VERSION}
"""
}
void runMinikubeCleanup() {
sh '''
make e2e-cleanup-minikube
'''
}
void runIstioMinikubeSetup() {
sh """
make e2e-setup-minikube-istio IMG=${operatorRepo}:${VERSION}
"""
}
void runIstioE2eTests() {
sh """
make e2e-test-istio IMG=${operatorRepo}:${VERSION} E2E_ISTIO_AMBIENT=true
"""
}
void runBlackDuckScan() {
// Trigger BlackDuck scan job with CONTAINER_IMAGES parameter when params.PUBLISH_IMAGE is true
if (params.PUBLISH_IMAGE) {
build job: 'securityscans/Blackduck/KubeNinjas/kubernetes-operator', wait: false, parameters: [ string(name: 'branch', value: "${env.BRANCH_NAME}"), string(name: 'CONTAINER_IMAGES', value: "${operatorRegistry}/${operatorRepo}:${VERSION}-${branchNameTag}-${timeStamp}") ]
} else {
build job: 'securityscans/Blackduck/KubeNinjas/kubernetes-operator', wait: false, parameters: [ string(name: 'branch', value: "${env.BRANCH_NAME}") ]
}
}
/**
* Publishes the built Docker image to the internal Artifactory registry.
* Tags the image with multiple tags (version-specific, branch-specific, latest).
* Requires Artifactory credentials.
*/
void publishToInternalRegistry() {
withCredentials([usernamePassword(credentialsId: 'builder-credentials-artifactory', passwordVariable: 'docker_password', usernameVariable: 'docker_user')]) {
sh """
# make sure to logout first to avoid issues with cached credentials
docker logout ${operatorRegistry}
echo "${docker_password}" | docker login --username ${docker_user} --password-stdin ${operatorRegistry}
# Create tags
docker tag ${operatorRepo}:${VERSION} ${operatorRegistry}/${operatorRepo}:${VERSION}
docker tag ${operatorRepo}:${VERSION} ${operatorRegistry}/${operatorRepo}:${VERSION}-${branchNameTag}
docker tag ${operatorRepo}:${VERSION} ${operatorRegistry}/${operatorRepo}:${VERSION}-${branchNameTag}-${timeStamp}
docker tag ${operatorRepo}:${VERSION} ${operatorRegistry}/${operatorRepo}:latest
# Push images to internal registry
docker push ${operatorRegistry}/${operatorRepo}:${VERSION}
docker push ${operatorRegistry}/${operatorRepo}:${VERSION}-${branchNameTag}
docker push ${operatorRegistry}/${operatorRepo}:${VERSION}-${branchNameTag}-${timeStamp}
docker push ${operatorRegistry}/${operatorRepo}:latest
"""
}
}
pipeline {
agent {
label {
label 'cld-kubernetes'
}
}
options {
checkoutToSubdirectory '.'
buildDiscarder logRotator(artifactDaysToKeepStr: '20', artifactNumToKeepStr: '', daysToKeepStr: '30', numToKeepStr: '')
skipStagesAfterUnstable()
}
triggers {
// Trigger nightly builds on the develop branch
parameterizedCron( env.BRANCH_NAME == 'develop' ? '''00 05 * * * % E2E_MARKLOGIC_IMAGE_VERSION=ml-docker-db-dev-tierpoint.bed-artifactory.bedford.progress.com/marklogic/marklogic-server-ubi-rootless:latest-12
00 05 * * * % E2E_MARKLOGIC_IMAGE_VERSION=ml-docker-db-dev-tierpoint.bed-artifactory.bedford.progress.com/marklogic/marklogic-server-ubi-rootless:latest-11; PUBLISH_IMAGE=false
00 07 * * * % E2E_MARKLOGIC_IMAGE_VERSION=ml-docker-db-dev-tierpoint.bed-artifactory.bedford.progress.com/marklogic/marklogic-server-ubi-rootless:latest-12; VERIFY_ISTIO_AMBIENT=true''' : '')
}
environment {
PATH = "/space/go/bin:${env.PATH}"
MINIKUBE_HOME = "/space/minikube/"
KUBECONFIG = "/space/.kube-config"
GOPATH = "/space/go"
}
parameters {
string(name: 'E2E_MARKLOGIC_IMAGE_VERSION', defaultValue: 'ml-docker-db-dev-tierpoint.bed-artifactory.bedford.progress.com/marklogic/marklogic-server-ubi-rootless:latest-12', description: 'Docker image to use for tests.', trim: true)
string(name: 'VERSION', defaultValue: '1.2.0', description: 'Version to tag the image with.', trim: true)
booleanParam(name: 'PUBLISH_IMAGE', defaultValue: false, description: 'Publish image to internal registry')
string(name: 'emailList', defaultValue: emailList, description: 'List of email for build notification', trim: true)
booleanParam(name: 'VERIFY_ISTIO_AMBIENT', defaultValue: true, description: 'Run Istio ambient mode e2e tests (requires fresh minikube cluster with Istio)')
}
stages {
stage('Pre-Build-Check') {
steps {
preBuildCheck()
}
}
stage('Run-tests') {
steps {
runTests()
}
}
stage('Run-Minikube-Setup') {
steps {
runMinikubeSetup()
}
}
stage('Run-e2e-Tests') {
steps {
runE2eTests()
}
}
stage('Cleanup Environment') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
runMinikubeCleanup()
}
}
}
stage('Istio-Minikube-Setup') {
when {
expression { return params.VERIFY_ISTIO_AMBIENT }
}
steps {
runIstioMinikubeSetup()
}
}
stage('Run-Istio-e2e-Tests') {
when {
expression { return params.VERIFY_ISTIO_AMBIENT }
}
steps {
runIstioE2eTests()
}
}
stage('Istio-Cleanup') {
when {
expression { return params.VERIFY_ISTIO_AMBIENT }
}
steps {
runMinikubeCleanup()
}
}
// Publish image to internal registries (conditional)
stage('Publish Image') {
when {
anyOf {
branch 'develop'
expression { return params.PUBLISH_IMAGE }
}
}
steps {
publishToInternalRegistry()
}
}
stage('Run-BlackDuck-Scan') {
steps {
runBlackDuckScan()
}
}
}
post {
always {
publishTestResults()
}
success {
resultNotification('✅ Success')
}
failure {
resultNotification('❌ Failure')
}
unstable {
resultNotification('⚠️ Unstable')
}
aborted {
resultNotification('🚫 Aborted')
}
}
}