Skip to content

Commit db7691f

Browse files
authored
Merge pull request #277 from ChrisPark89/feat/npmpub
Changed Jenkinsfile to publish master branch to npmjs.org
2 parents aa043b4 + 8d38797 commit db7691f

File tree

1 file changed

+85
-113
lines changed

1 file changed

+85
-113
lines changed

Jenkinsfile

Lines changed: 85 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def PIPELINE_CONTROL = [
2121
build: true,
2222
unit_test: true,
2323
system_test: true,
24-
deploy: true,
25-
ci_skip: false ]
24+
cd_skip: false
25+
]
2626

2727
/**
2828
* The result strings for a build status
@@ -34,10 +34,16 @@ def BUILD_RESULT = [
3434
]
3535

3636
/**
37-
* Test npm registry using for smoke test
37+
* Test npm registry
3838
*/
3939
def TEST_NPM_REGISTRY = "https://eu.artifactory.swg-devops.com/artifactory/api/npm/cicsts-npm-virtual"
4040

41+
/**
42+
* npm registry
43+
*/
44+
def NPM_REGISTRY = "registry.npmjs.org"
45+
def NPM_FULL_REGISTRY = "https://registry.npmjs.org"
46+
4147
/**
4248
* The root results folder for items configurable by environmental variables
4349
*/
@@ -137,6 +143,11 @@ pipeline {
137143
// other Jenkins jobs or needing root access.
138144
NPM_CONFIG_PREFIX = "${WORKSPACE}/npm-global"
139145
PATH = "${NPM_CONFIG_PREFIX}/bin:${PATH}"
146+
147+
// Credential to publish to npmjs.org
148+
// NPM_CREDENTIALS_USR is email address
149+
// NPM_REDENTIALS_PSW is the login token
150+
NPM_CREDENTIALS = credentials('ibmcics.npmjs.auth.token')
140151
}
141152

142153
stages {
@@ -154,45 +165,6 @@ pipeline {
154165
}
155166
}
156167

157-
/************************************************************************
158-
* STAGE
159-
* -----
160-
* Check for CI Skip
161-
*
162-
* TIMEOUT
163-
* -------
164-
* 2 Minutes
165-
*
166-
* EXECUTION CONDITIONS
167-
* --------------------
168-
* - Always
169-
*
170-
* DECRIPTION
171-
* ----------
172-
* Checks for the [ci skip] text in the last commit. If it is present,
173-
* the build is stopped. Needed because the pipeline does do some simple
174-
* git commits on the master branch for the purposes of version bumping.
175-
*
176-
* OUTPUTS
177-
* -------
178-
* PIPELINE_CONTROL.ci_skip will be set to 'true' if [ci skip] is found in the
179-
* commit text.
180-
************************************************************************/
181-
stage('Check for CI Skip') {
182-
steps {
183-
timeout(time: 2, unit: 'MINUTES') {
184-
script {
185-
// This checks for the [ci skip] text. If found, the status code is 0
186-
def result = sh returnStatus: true, script: 'git log -1 | grep \'.*\\[ci skip\\].*\''
187-
if (result == 0) {
188-
echo '"ci skip" spotted in the git commit. Aborting.'
189-
PIPELINE_CONTROL.ci_skip = true
190-
}
191-
}
192-
}
193-
}
194-
}
195-
196168
/************************************************************************
197169
* STAGE
198170
* -----
@@ -204,7 +176,6 @@ pipeline {
204176
*
205177
* EXECUTION CONDITIONS
206178
* --------------------
207-
* - PIPELINE_CONTROL.ci_skip is false
208179
* - PIPELINE_CONTROL.build is true or PIPELINE_CONTROL.smoke_test is true
209180
*
210181
* DESCRIPTION
@@ -219,9 +190,6 @@ pipeline {
219190
stage('Install Zowe CLI') {
220191
when {
221192
allOf {
222-
expression {
223-
return PIPELINE_CONTROL.ci_skip == false
224-
}
225193
expression {
226194
return PIPELINE_CONTROL.build || PIPELINE_CONTROL.smoke_test
227195
}
@@ -251,7 +219,6 @@ pipeline {
251219
*
252220
* EXECUTION CONDITIONS
253221
* --------------------
254-
* - PIPELINE_CONTROL.ci_skip is false
255222
* - PIPELINE_CONTROL.build is true
256223
*
257224
* DESCRIPTION
@@ -265,9 +232,6 @@ pipeline {
265232
stage('Install Dependencies') {
266233
when {
267234
allOf {
268-
expression {
269-
return PIPELINE_CONTROL.ci_skip == false
270-
}
271235
expression {
272236
return PIPELINE_CONTROL.build
273237
}
@@ -293,7 +257,6 @@ pipeline {
293257
*
294258
* EXECUTION CONDITIONS
295259
* --------------------
296-
* - PIPELINE_CONTROL.ci_skip is false
297260
* - PIPELINE_CONTROL.build is true
298261
*
299262
* DESCRIPTION
@@ -307,9 +270,6 @@ pipeline {
307270
stage('Build') {
308271
when {
309272
allOf {
310-
expression {
311-
return PIPELINE_CONTROL.ci_skip == false
312-
}
313273
expression {
314274
return PIPELINE_CONTROL.build
315275
}
@@ -339,7 +299,6 @@ pipeline {
339299
*
340300
* EXECUTION CONDITIONS
341301
* --------------------
342-
* - PIPELINE_CONTROL.ci_skip is false
343302
* - PIPELINE_CONTROL.unit_test is true
344303
*
345304
* ENVIRONMENT VARIABLES
@@ -379,9 +338,6 @@ pipeline {
379338
stage('Test: Unit') {
380339
when {
381340
allOf {
382-
expression {
383-
return PIPELINE_CONTROL.ci_skip == false
384-
}
385341
expression {
386342
return PIPELINE_CONTROL.unit_test
387343
}
@@ -443,7 +399,6 @@ pipeline {
443399
*
444400
* EXECUTION CONDITIONS
445401
* --------------------
446-
* - PIPELINE_CONTROL.ci_skip is false
447402
* - PIPELINE_CONTROL.system_test is true
448403
*
449404
* ENVIRONMENT VARIABLES
@@ -486,9 +441,6 @@ pipeline {
486441
stage('Test: System') {
487442
when {
488443
allOf {
489-
expression {
490-
return PIPELINE_CONTROL.ci_skip == false
491-
}
492444
expression {
493445
return PIPELINE_CONTROL.system_test
494446
}
@@ -523,53 +475,71 @@ pipeline {
523475
/************************************************************************
524476
* STAGE
525477
* -----
526-
* Bump Version
478+
* Check for CD Skip
527479
*
528480
* TIMEOUT
529481
* -------
530-
* 5 Minutes
482+
* 2 Minutes
531483
*
532484
* EXECUTION CONDITIONS
533485
* --------------------
534-
* - PIPELINE_CONTROL.ci_skip is false
535-
* - PIPELINE_CONTROL.deploy is true
536-
* - The build is still successful and not unstable
486+
* - Always
537487
*
538-
* DESCRIPTION
539-
* -----------
540-
* Bumps the pre-release version in preparation for publishing to an npm
541-
* registry.
488+
* DECRIPTION
489+
* ----------
490+
* Compare the version in package.json with released version. If the same,
491+
* the build is stopped. Needed because the pipeline does do some simple
492+
* merge on the master branch for documentation updates
493+
*
494+
* OUTPUTS
495+
* -------
496+
* PIPELINE_CONTROL.cd_skip will be set to 'true',
497+
* - if master branch's release version and new version are the same
498+
* - if there isn't any new commit
542499
************************************************************************/
543-
stage('Bump Version') {
500+
stage('Check for CD Skip') {
544501
when {
545502
allOf {
546503
expression {
547-
return PIPELINE_CONTROL.ci_skip == false
548-
}
549-
expression {
550-
return PIPELINE_CONTROL.deploy
504+
return PIPELINE_CONTROL.cd_skip == false
551505
}
552506
expression {
553507
return RELEASE_BRANCHES.contains(BRANCH_NAME)
554508
}
555-
expression {
556-
return GIT_COMMIT != GIT_PREVIOUS_SUCCESSFUL_COMMIT
557-
}
558509
}
559510
}
560511
steps {
561-
timeout(time: 5, unit: 'MINUTES') {
562-
echo "Bumping Version"
563-
564-
// This npm command does the version bump
512+
timeout(time: 2, unit: 'MINUTES') {
565513
script {
566-
def baseVersion = sh returnStdout: true, script: 'node -e "console.log(require(\'./package.json\').version.split(\'-\')[0])"'
567-
def preReleaseVersion = baseVersion.trim() + "-next." + new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("UTC"))
568-
sh "npm version ${preReleaseVersion} --no-git-tag-version"
514+
// Retrieve new version from package.json
515+
def NEW_VERSION = sh (
516+
script: 'echo "console.log(require(\'./package.json\').version);" | node',
517+
returnStdout: true
518+
).trim()
519+
echo "NEW_VERSION: ${NEW_VERSION}"
520+
// Retrieve released version from npmjs.org
521+
def RELEASED_VERSION = sh (
522+
script: 'npm view "$(echo "console.log(require(\'./package.json\').name);" | node)" version --registry '+NPM_FULL_REGISTRY+' || true',
523+
returnStdout: true
524+
).trim()
525+
echo "RELEASED_VERSION: ${RELEASED_VERSION}"
526+
527+
// skip deploying, if version has not changed.
528+
// However, we deploy dev branch even if version has not changed
529+
// as long as there is any new commit
530+
if (NEW_VERSION == RELEASED_VERSION && BRANCH_NAME != DEV_BRANCH) {
531+
echo 'New version and released version are the same. Skip deploying.'
532+
PIPELINE_CONTROL.cd_skip = true
533+
}
534+
if (GIT_COMMIT == GIT_PREVIOUS_SUCCESSFUL_COMMIT) {
535+
echo 'There is no new commit. Skip deploying'
536+
PIPELINE_CONTROL.cd_skip = true
537+
}
569538
}
570539
}
571540
}
572541
}
542+
573543
/************************************************************************
574544
* STAGE
575545
* -----
@@ -581,8 +551,6 @@ pipeline {
581551
*
582552
* EXECUTION CONDITIONS
583553
* --------------------
584-
* - PIPELINE_CONTROL.ci_skip is false
585-
* - PIPELINE_CONTROL.deploy is true
586554
* - The build is still successful and not unstable
587555
*
588556
* DESCRIPTION
@@ -598,50 +566,54 @@ pipeline {
598566
when {
599567
allOf {
600568
expression {
601-
return PIPELINE_CONTROL.ci_skip == false
602-
}
603-
expression {
604-
return PIPELINE_CONTROL.deploy
569+
return PIPELINE_CONTROL.cd_skip == false
605570
}
606571
expression {
607572
return RELEASE_BRANCHES.contains(BRANCH_NAME)
608573
}
609-
expression {
610-
return GIT_COMMIT != GIT_PREVIOUS_SUCCESSFUL_COMMIT
611-
}
612574
}
613575
}
614576
steps {
615577
timeout(time: 5, unit: 'MINUTES') {
616578
echo 'Deploy Binary'
617-
withCredentials([usernamePassword(credentialsId: ARTIFACTORY_CREDENTIALS_ID, usernameVariable: 'USERNAME', passwordVariable: 'API_KEY')]) {
618-
619-
// Set up authentication to Artifactory
579+
script {
620580
sh "rm -f .npmrc"
621-
sh 'curl -u $USERNAME:$API_KEY https://eu.artifactory.swg-devops.com/artifactory/api/npm/auth/ >> .npmrc'
622-
sh "echo registry=$TEST_NPM_REGISTRY >> .npmrc"
581+
// set @zowe registry
582+
sh "echo always-auth=true >> .npmrc"
623583
sh "echo @brightside:registry=https://api.bintray.com/npm/ca/brightside/ >> .npmrc"
624584
sh "echo @brightside:always-auth=false >> .npmrc"
625-
626-
script {
627-
if (BRANCH_NAME == MASTER_BRANCH) {
628-
echo "publishing next to $TEST_NPM_REGISTRY"
629-
sh "npm publish --tag alpha"
630-
} else if (BRANCH_NAME == DEV_BRANCH) {
631-
echo "publishing next to $TEST_NPM_REGISTRY"
632-
sh "npm publish --tag next"
633-
} else {
634-
echo "publishing latest to $TEST_NPM_REGISTRY"
635-
sh "npm publish --tag latest"
585+
if (BRANCH_NAME == MASTER_BRANCH) {
586+
// Set registry
587+
sh "echo registry=$NPM_FULL_REGISTRY >> .npmrc"
588+
// Set credential
589+
sh "echo //$NPM_REGISTRY/:_authToken=${NPM_CREDENTIALS_PSW} >> .npmrc"
590+
// deploy
591+
echo "publishing to $NPM_FULL_REGISTRY"
592+
sh "npm publish --tag latest"
593+
} else if (BRANCH_NAME == DEV_BRANCH) {
594+
// Set version
595+
def baseVersion = sh returnStdout: true, script: 'node -e "console.log(require(\'./package.json\').version.split(\'-\')[0])"'
596+
def preReleaseVersion = baseVersion.trim() + "-next." + new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("UTC"))
597+
sh "npm version ${preReleaseVersion} --no-git-tag-version"
598+
// Set credential
599+
withCredentials([usernamePassword(credentialsId: ARTIFACTORY_CREDENTIALS_ID, usernameVariable: 'USERNAME', passwordVariable: 'API_KEY')]) {
600+
sh 'curl -u $USERNAME:$API_KEY https://eu.artifactory.swg-devops.com/artifactory/api/npm/auth/ >> .npmrc'
636601
}
602+
// Set registry
603+
sh "echo registry=$TEST_NPM_REGISTRY >> .npmrc"
604+
// deploy
605+
echo "publishing to $TEST_NPM_REGISTRY"
606+
sh "npm publish --tag alpha"
607+
} else {
608+
echo "Only master and dev branch is allowed to deploy the app (current branch: $BRANCH_NAME). Skip deploying"
637609
}
638-
639-
sh "rm -f .npmrc"
640610
}
641611
}
642612
}
643613
}
644614
}
615+
616+
// Slack notification when fails or back to normal
645617
post {
646618
unsuccessful {
647619
script {

0 commit comments

Comments
 (0)