-
Notifications
You must be signed in to change notification settings - Fork 12
/
Jenkinsfile
379 lines (349 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!groovy
pipeline {
agent {
kubernetes {
yaml kubePodTemplate(name: 'gauge.yaml')
}
}
parameters {
validatingString( name: 'TARGET_VERSION',
defaultValue: 'NONE',
description: 'Tag to create after successful QA',
failedValidationMessage: 'Tag name must be NONE or a semantic version release or pre-release (i.e. no build metadata)',
regex: /NONE|${globals.SVRE_PRE_RELEASE}/)
}
environment {
ARTIFACTORY_CREDS = credentials('artifactory')
ARTIFACTORY_URL_UP = "${Artifactory.server('taas-artifactory-upload').getUrl()}"
ARTIFACTORY_URL_DOWN = "${Artifactory.server('taas-artifactory').getUrl()}"
}
stages {
stage('Init') {
steps {
script {
defaultInit()
applyCustomizations()
commitHash = "${env.GIT_COMMIT.take(7)}"
}
}
}
stage('QA') {
steps {
withEnv(['DOCKER_HOST=',
'SERVER_AUTH_TYPE=basic',
'SERVER_URL=http://127.0.0.1:5984',
'WIREMOCK_URL=http://127.0.0.1:8080',
'WIREMOCK_PORT=8080'
]) {
withCredentials([
usernamePassword(credentialsId: 'container-test-server',
usernameVariable: 'SERVER_USERNAME',
passwordVariable: 'SERVER_PASSWORD')
]) {
script {
sh './scripts/setup_couch.sh'
sh './scripts/setup_wiremock.sh'
}
runTests()
}
}
}
post {
always {
junit (
testResults: '**/junitreports/*.xml'
)
}
}
}
stage('SonarQube analysis') {
environment {
scannerHome = tool 'SonarQubeScanner'
}
// Scanning runs on dependabot core update and non-dependabot branches
when {
anyOf {
branch pattern: /^dependabot.*(?i)(ibm)[\.-].+sdk-core.*$/, comparator: 'REGEXP'
not {
branch 'dependabot*'
}
}
}
steps {
scanCode()
}
}
stage('Publish[staging]') {
environment {
STAGE_ROOT = "${ARTIFACTORY_URL_UP}/api/"
}
steps {
bumpVersion(true)
customizePublishingInfo()
withEnv(["LIB_NAME=${libName}",
"TYPE=${buildType}",
"ARTIFACT_URL=${artifactUrl}",
"MODULE_ID=${moduleId}",
"BUILD_NAME=${env.JOB_NAME}"]) {
publishStaging()
publishArtifactoryBuildInfo()
}
}
// This post stage resets the temporary version bump used to publish to staging
post {
always {
sh 'git reset --hard'
}
}
}
stage('Run Gauge tests') {
steps {
script {
buildResults = null
// For standard builds attempt to run on a matching env.BRANCH_NAME branch first and if it doesn't exist
// then fallback to TARGET_GAUGE_RELEASE_BRANCH_NAME if set or env.TARGET_GAUGE_DEFAULT_BRANCH_NAME.
gaugeBranchName = env.BRANCH_NAME
fallbackBranchName = env.TARGET_GAUGE_RELEASE_BRANCH_NAME ?: env.TARGET_GAUGE_DEFAULT_BRANCH_NAME
// For release builds (tag builds or the primary branch) do the reverse and attempt to run on the
// TARGET_GAUGE_RELEASE_BRANCH_NAME falling back to env.BRANCH_NAME or env.TAG_NAME if there is no match.
if (env.TAG_NAME || env.BRANCH_IS_PRIMARY){
gaugeBranchName = env.TARGET_GAUGE_RELEASE_BRANCH_NAME
fallbackBranchName = env.TAG_NAME ?: env.BRANCH_NAME
}
try {
buildResults = build job: "/${env.SDKS_GAUGE_PIPELINE_PROJECT}/${gaugeBranchName}", parameters: [
string(name: 'SDK_RUN_LANG', value: "$libName"),
string(name: "SDK_VERSION_${libName.toUpperCase()}", value: "${env.NEW_SDK_VERSION}")]
} catch (hudson.AbortException ae) {
// only run build in sdks-gauge default branch if BRANCH_NAME doesn't exist
if (ae.getMessage().contains("No item named /${env.SDKS_GAUGE_PIPELINE_PROJECT}/${gaugeBranchName} found")) {
echo "No matching branch named '${gaugeBranchName}' in sdks-gauge, building ${fallbackBranchName} branch"
build job: "/${env.SDKS_GAUGE_PIPELINE_PROJECT}/${fallbackBranchName}", parameters: [
string(name: 'SDK_RUN_LANG', value: "$libName"),
string(name: "SDK_VERSION_${libName.toUpperCase()}", value: "${env.NEW_SDK_VERSION}")]
} else {
throw ae
}
}
}
}
}
stage('Update version and tag') {
when {
beforeAgent true
allOf {
// We only bump the version and create a tag when building the default primary branch with a TARGET_VERSION
branch 'main'
not {
equals expected: 'NONE', actual: "${params.TARGET_VERSION}"
}
}
}
steps {
gitsh('github.com') {
// bump the version
bumpVersion(false)
// Push the version bump and release tag
sh 'git push --tags origin HEAD:main'
}
}
}
stage('Publish[repository]') {
// We publish only when building a tag that meets our semantic version release or pre-release tag format
when {
beforeAgent true
allOf {
buildingTag()
anyOf {
tag pattern: /${env.SVRE_PRE_RELEASE_TAG}/, comparator: "REGEXP"
}
}
}
steps {
publishPublic()
gitsh('github.com') {
publishDocs()
}
}
}
}
}
// Note default values cannot be assigned here.
def libName
def commitHash
def bumpVersion
def customizeVersion
def getNewVersion
def testVersionPrefix
def customizePublishingInfo
def publishArtifactoryBuildInfo
def publishArtifactoryBuildInfoScript
def artifactUrl
def moduleId
def buildType
def scanCode
void defaultInit() {
// Default to using bump-my-version
bumpVersion = { isDevRelease ->
newVersion = getNewVersion(isDevRelease)
// Set an env var with the new version
env.NEW_SDK_VERSION = newVersion
doVersionBump(isDevRelease, newVersion)
}
doVersionBump = { isDevRelease, newVersion, allowDirty ->
sh "bump-my-version bump patch --new-version ${newVersion} ${allowDirty ? '--allow-dirty': ''} ${isDevRelease ? '--no-commit' : '--tag --tag-message "Release {new_version}"'}"
}
getNewVersion = { isDevRelease ->
// Get a staging or target version and customize with lang specific requirements
return customizeVersion(isDevRelease ? getDevVersion() : getTargetVersion())
}
getTargetVersion = {
version = ''
if ('NONE' != params.TARGET_VERSION) {
version = params.TARGET_VERSION
} else {
// If a target version is not provided default to a patch bump
version = sh returnStdout: true, script: 'bump-my-version show-bump --ascii | grep patch | rev | cut -f1 -d " " | rev'
}
return version.trim()
}
getDevVersion = {
devVersion = getTargetVersion()
if (devVersion ==~ /${env.SVRE_RELEASE}/) {
// For a release (e.g. 1.0.0) use a hyphen separator (e.g. 1.0.0-dev)
devVersion += "-"
} else if (devVersion ==~ /${env.SVRE_PRE_RELEASE}/) {
// For a pre-release (e.g. 1.0.0-b7), add dot separator (e.g. 1.0.0-b7.dev)
devVersion += "."
}
// Now add dev identifier (a number is required by some package managers)
devVersion += "dev0"
// Add uniqueness with build metadata to dev build versions
devVersion += "+git${commitHash}.${currentBuild.startTimeInMillis}.${currentBuild.number}"
return devVersion
}
// Default no-op implementation to use semverFormatVersion
customizeVersion = { semverFormatVersion ->
semverFormatVersion
}
publishArtifactoryBuildInfoScript = {
// put build info on module/artifacts then overwrite and publish artifactory build
sh './scripts/publish_buildinfo.sh'
}
publishArtifactoryBuildInfo = {
// create base build info
rtBuildInfo (
buildName: "${env.BUILD_NAME}",
buildNumber: "${env.BUILD_NUMBER}",
includeEnvPatterns: ['BRANCH_NAME'],
maxDays: 90,
deleteBuildArtifacts: true,
asyncBuildRetention: true
)
rtPublishBuildInfo (
buildName: "${env.BUILD_NAME}",
buildNumber: "${env.BUILD_NUMBER}",
serverId: 'taas-artifactory-upload'
)
publishArtifactoryBuildInfoScript()
}
scanCode = {
withSonarQubeEnv(installationName: 'SonarQubeServer') {
sh "${scannerHome}/bin/sonar-scanner -Dsonar.qualitygate.wait=true -Dsonar.projectKey=cloudant-${libName}-sdk -Dsonar.branch.name=${env.BRANCH_NAME} -Dsonar.exclusions=examples/**"
}
}
}
// Language specific implementations of the methods:
// applyCustomizations()
// runTests()
// publishStaging()
// publishPublic()
// publishDocs()
// + other customizations
void applyCustomizations() {
libName = 'node'
bumpVersion = { isDevRelease ->
withCredentials([usernamePassword(usernameVariable: 'NPMRC_USER', passwordVariable: 'NPMRC_TOKEN', credentialsId: 'artifactory')]) {
withEnv(['NPMRC_EMAIL=' + env.NPMRC_USER]) {
withNpmEnv(registryArtifactoryDown) {
// Get the dependencies
sh "npm ci --no-audit"
// Get the target version with build meta if needed
newVersion = getNewVersion(isDevRelease)
// Update to the new version, not tagging for dev releases
sh "npm version ${isDevRelease ? '--no-git-tag-version' : '-m "Update version -> %s"'} ${newVersion}"
// Set env variable version from updated package.json
env.NEW_SDK_VERSION = sh returnStdout: true, script: 'jq -j .version package.json'
}
}
}
}
customizeVersion = { semverFormatVersion ->
// Semver build meta is unacceptable to npm
// Make it another pre-release identifier instead
semverFormatVersion.replace('+','.')
}
customizePublishingInfo = {
// Set the publishing names and types
buildType = 'GENERIC'
artifactUrl = "${STAGE_ROOT}storage/cloudant-sdks-npm-local/@ibm-cloud/cloudant/-/@ibm-cloud/cloudant-${env.NEW_SDK_VERSION}.tgz"
moduleId = "ibm-cloud:cloudant:${env.NEW_SDK_VERSION}"
}
}
// 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 up
def getRegistryArtifactoryUp() {
return "${env.ARTIFACTORY_URL_UP}/api/npm/cloudant-sdks-npm-virtual/"
}
// url of registry for artifactory down
def getRegistryArtifactoryDown() {
return "${env.ARTIFACTORY_URL_DOWN}/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()
}
}
void runTests() {
withCredentials([usernamePassword(usernameVariable: 'NPMRC_USER', passwordVariable: 'NPMRC_TOKEN', credentialsId: 'artifactory')]) {
withEnv(['NPMRC_EMAIL=' + env.NPMRC_USER]) {
withNpmEnv(registryArtifactoryDown) {
sh "npm ci --no-audit"
sh 'npm test'
}
}
}
}
void publishStaging() {
withCredentials([usernamePassword(usernameVariable: 'NPMRC_USER', passwordVariable: 'NPMRC_TOKEN', credentialsId: 'artifactory')]) {
withEnv(['NPMRC_EMAIL=' + env.NPMRC_USER]) {
withNpmEnv(registryArtifactoryUp) {
publishNpm(registryArtifactoryUp)
}
}
}
}
void publishPublic() {
withCredentials([string(credentialsId: 'npm-mail', variable: 'NPMRC_EMAIL'),
usernamePassword(credentialsId: 'npm-creds', passwordVariable: 'NPMRC_TOKEN', usernameVariable: 'NPMRC_USER')]) {
withNpmEnv(registryPublic) {
publishNpm(registryPublic)
}
}
}
void publishNpm(registry) {
sh 'npm run build'
sh "npm publish ./dist"
}
void publishDocs() {
sh './scripts/typedoc/publish-doc.sh'
}