-
Notifications
You must be signed in to change notification settings - Fork 21
/
Jenkinsfile
299 lines (285 loc) · 9.93 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
#!groovy
// Copyright © 2017, 2023 IBM Corp. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
def getEnvForSuite(suiteName, version) {
def envVars = []
// Add test suite specific environment variables
switch(suiteName) {
case 'test':
envVars.add("COUCHBACKUP_MOCK_SERVER_PORT=${7700 + version.toInteger()}")
break
case 'test-network/conditions':
envVars.add("CLOUDANT_IAM_TOKEN_URL=${SDKS_TEST_IAM_URL}")
envVars.add("TEST_TIMEOUT_MULTIPLIER=50")
envVars.add("COUCHBACKUP_MOCK_SERVER_PORT=${7800 + version.toInteger()}")
break
case 'test-iam':
envVars.add("CLOUDANT_IAM_TOKEN_URL=${SDKS_TEST_IAM_URL}")
envVars.add("COUCHBACKUP_MOCK_SERVER_PORT=${7900 + version.toInteger()}")
break
default:
error("Unknown test suite environment ${suiteName}")
}
return envVars
}
// NB these registry URLs must have trailing slashes
// url of registry for public uploads
def getRegistryPublic() {
return "https://registry.npmjs.org/"
}
// url of registry for artifactory down
def getRegistryArtifactoryDown() {
return "${Artifactory.server('taas-artifactory').getUrl()}/api/npm/cloudant-sdks-npm-virtual/"
}
def noScheme(str) {
return str.substring(str.indexOf(':') + 1)
}
def withNpmEnv(registry, closure) {
withEnv(['NPMRC_REGISTRY=' + noScheme(registry),
'NPM_CONFIG_REGISTRY=' + registry,
'NPM_CONFIG_USERCONFIG=.npmrc-jenkins']) {
closure()
}
}
def runTest(version, filter=null, testSuite='test') {
if (filter == null) {
if (env.TEST_FILTER == null) {
// The set of default tests includes unit and integration tests, but
// not ones tagged #slower, #slowest.
filter = '-i -g \'#slowe\''
} else {
filter = env.TEST_FILTER
}
}
def testReportPath = "${testSuite}-${version}-results.xml"
// Run tests using creds
withEnv(getEnvForSuite("${testSuite}", version)) {
withCredentials([usernamePassword(credentialsId: 'testServerLegacy', usernameVariable: 'DB_USER', passwordVariable: 'DB_PASSWORD'),
usernamePassword(credentialsId: 'artifactory', usernameVariable: 'ARTIFACTORY_USER', passwordVariable: 'ARTIFACTORY_PW'),
string(credentialsId: 'testServerIamApiKey', variable: "${(testSuite == 'test-iam' || testSuite == 'test-network/conditions') ? 'COUCHBACKUP_TEST_IAM_API_KEY' : 'IAM_API_KEY'}")]) {
try {
// For the IAM tests we want to run the normal 'test' suite, but we
// want to keep the report named 'test-iam'
def testRun = (testSuite != 'test-iam') ? testSuite : 'test'
// Actions:
// Run tests using filter
withCredentials([usernamePassword(usernameVariable: 'NPMRC_USER', passwordVariable: 'NPMRC_TOKEN', credentialsId: 'artifactory')]) {
withEnv(['NPMRC_EMAIL=' + env.NPMRC_USER]) {
withNpmEnv(registryArtifactoryDown) {
// A note on credential encoding
// The couchbackup tool requires legacy credentials in the user-info portion of a URL, so creds in the URL must be encoded.
// Encoding in the Jenkins credentials store is not an option because the creds are also used [unencoded] in other places.
// Encoding in the couchbackup test code is not an option because the environment variable is used directly by couchbackup during tests.
// Encoding in this file is not an option because the credential must be interpolated by groovy which is a Jenkins "no no".
// Ergo we need to encode when we expand the env var in the shell. We do this by running $(node -e ...) as node is always available
// when we are running couchbackup tests, other utilities that could encode like jq may not always be available.
sh """
set +x
export COUCH_LEGACY_URL="https://\${DB_USER}:\$(node -e "console.log(encodeURIComponent(process.env.DB_PASSWORD));")@\${SDKS_TEST_SERVER_HOST}"
export COUCH_BACKEND_URL="${(testSuite == 'test-iam' || testSuite == 'test-network/conditions') ? '${SDKS_TEST_SERVER_URL}' : '${COUCH_LEGACY_URL}'}"
export COUCH_URL="${(testSuite == 'test-network/conditions') ? 'http://127.0.0.1:8888' : '${COUCH_BACKEND_URL}'}"
export PROXY_URL='http://127.0.0.1:8474'
set -x
./node_modules/mocha/bin/mocha.js --reporter xunit --reporter-options output=${testReportPath},suiteName=${testSuite} ${filter} ${testRun}
"""
}
}
}
} finally {
junit "**/${testReportPath}"
}
}
}
}
pipeline {
agent {
kubernetes {
yaml kubePodTemplate(name: 'couchbackup.yaml')
}
}
options {
disableConcurrentBuilds()
}
stages {
stage('Build') {
when {
beforeAgent true
// Skip when building tags as we just want to publish then
not {
buildingTag()
}
}
steps {
withCredentials([usernamePassword(usernameVariable: 'NPMRC_USER', passwordVariable: 'NPMRC_TOKEN', credentialsId: 'artifactory')]) {
withEnv(['NPMRC_EMAIL=' + env.NPMRC_USER]) {
withNpmEnv(registryArtifactoryDown) {
sh 'npm ci'
}
}
}
}
}
stage('QA') {
when {
beforeAgent true
// Skip when building tags as we just want to publish then
not {
buildingTag()
}
}
parallel {
// Stages that run on LTS version from full agent default container
stage('Lint') {
steps {
script{
sh 'npm run lint'
}
}
}
stage('Node LTS') {
steps {
script{
runTest('22')
}
}
}
stage('IAM Node LTS') {
steps {
script{
runTest('22', '-i -g \'#unit|#slowe\'', 'test-iam')
}
}
}
stage('Network Node LTS') {
when {
beforeAgent true
environment name: 'RUN_TOXY_TESTS', value: 'true'
}
steps {
script{
runTest('22', '', 'test-network/conditions')
}
}
}
stage('Node 18x') {
steps {
container('node18') {
script{
runTest('18')
}
}
}
}
stage('Node 20x') {
steps {
container('node20') {
script{
runTest('20')
}
}
}
}
}
}
stage('Log check') {
steps {
findText regexp: '.*EPIPE|DEP0137.*', alsoCheckConsoleOutput: true, unstableIfFound: true
}
}
stage('SonarQube analysis') {
when {
beforeAgent true
allOf {
expression { env.BRANCH_NAME }
not {
expression { env.BRANCH_NAME.startsWith('dependabot/') }
}
not {
buildingTag()
}
}
}
steps {
script {
def scannerHome = tool 'SonarQubeScanner';
withSonarQubeEnv(installationName: 'SonarQubeServer') {
sh "${scannerHome}/bin/sonar-scanner -Dsonar.qualitygate.wait=true -Dsonar.projectKey=couchbackup -Dsonar.branch.name=${env.BRANCH_NAME}"
}
}
}
}
// Publish the primary branch
stage('Publish') {
when {
beforeAgent true
anyOf {
buildingTag()
branch 'main'
}
}
stages {
stage('Tag and GH release') {
when {
beforeAgent true
branch 'main'
}
steps {
// Run the gitTagAndPublish which tags/publishes to github for release builds
// and does a dry run for snapshot builds
script {
gitTagAndPublish {
versionFile='package.json'
releaseApiUrl='https://api.github.com/repos/IBM/couchbackup/releases'
}
}
}
}
stage('NPM snapshot publish') {
when {
beforeAgent true
branch 'main'
}
steps {
// Make a snapshot version with the build ID added
sh "npm version --no-git-tag-version \$(npm version --json | jq -r '.[\"@cloudant/couchbackup\"]')-${env.BUILD_ID}"
// Upload using the NPM creds
withCredentials([string(credentialsId: 'npm-mail', variable: 'NPMRC_EMAIL'),
usernamePassword(credentialsId: 'npm-creds', passwordVariable: 'NPMRC_TOKEN', usernameVariable: 'NPMRC_USER')]) {
// publish the snapshot build to NPM
withNpmEnv(registryPublic) {
sh 'npm publish --tag snapshot'
}
}
}
}
stage('NPM publish') {
when {
beforeAgent true
buildingTag()
}
steps {
// Upload using the NPM creds
withCredentials([string(credentialsId: 'npm-mail', variable: 'NPMRC_EMAIL'),
usernamePassword(credentialsId: 'npm-creds', passwordVariable: 'NPMRC_TOKEN', usernameVariable: 'NPMRC_USER')]) {
// publish the tag build to NPM
withNpmEnv(registryPublic) {
sh 'npm publish'
}
}
}
}
}
}
}
}