Skip to content

Commit a960f46

Browse files
committed
Changed Jenkinsfile to publish master branch to npmjs.org
Signed-off-by: Jeonghyuk Park <[email protected]>
1 parent cb9031e commit a960f46

File tree

2 files changed

+82
-47
lines changed

2 files changed

+82
-47
lines changed

Jenkinsfile

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

2727
/**
@@ -34,10 +34,15 @@ 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 = "https://registry.npmjs.org/"
45+
4146
/**
4247
* The root results folder for items configurable by environmental variables
4348
*/
@@ -137,6 +142,9 @@ pipeline {
137142
// other Jenkins jobs or needing root access.
138143
NPM_CONFIG_PREFIX = "${WORKSPACE}/npm-global"
139144
PATH = "${NPM_CONFIG_PREFIX}/bin:${PATH}"
145+
146+
// Credential to publish to npmjs.org
147+
NPM_CREDENTIALS = credentials('ibmcics.npmjs.auth.token')
140148
}
141149

142150
stages {
@@ -523,53 +531,74 @@ pipeline {
523531
/************************************************************************
524532
* STAGE
525533
* -----
526-
* Bump Version
534+
* Check for CD Skip
527535
*
528536
* TIMEOUT
529537
* -------
530-
* 5 Minutes
538+
* 2 Minutes
531539
*
532540
* EXECUTION CONDITIONS
533541
* --------------------
534-
* - PIPELINE_CONTROL.ci_skip is false
535-
* - PIPELINE_CONTROL.deploy is true
536-
* - The build is still successful and not unstable
542+
* - Always
537543
*
538-
* DESCRIPTION
539-
* -----------
540-
* Bumps the pre-release version in preparation for publishing to an npm
541-
* registry.
544+
* DECRIPTION
545+
* ----------
546+
* Compare the version in package.json with released version. If the same,
547+
* the build is stopped. Needed because the pipeline does do some simple
548+
* merge on the master branch for documentation updates
549+
*
550+
* OUTPUTS
551+
* -------
552+
* PIPELINE_CONTROL.cd_skip will be set to 'true',
553+
* - if master branch's release version and new version are the same
554+
* - if there isn't any new commit
542555
************************************************************************/
543-
stage('Bump Version') {
556+
stage('Check for CD Skip') {
544557
when {
545558
allOf {
546559
expression {
547560
return PIPELINE_CONTROL.ci_skip == false
548561
}
549562
expression {
550-
return PIPELINE_CONTROL.deploy
563+
return PIPELINE_CONTROL.cd_skip == false
551564
}
552565
expression {
553566
return RELEASE_BRANCHES.contains(BRANCH_NAME)
554567
}
555-
expression {
556-
return GIT_COMMIT != GIT_PREVIOUS_SUCCESSFUL_COMMIT
557-
}
558568
}
559569
}
560570
steps {
561-
timeout(time: 5, unit: 'MINUTES') {
562-
echo "Bumping Version"
563-
564-
// This npm command does the version bump
571+
timeout(time: 2, unit: 'MINUTES') {
565572
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"
573+
// Retrieve new version from package.json
574+
def NEW_VERSION = sh (
575+
script: 'echo "console.log(require(\'./package.json\').version);" | node',
576+
returnStdout: true
577+
).trim()
578+
echo "NEW_VERSION: ${NEW_VERSION}"
579+
// Retrieve released version from npmjs.org
580+
def RELEASED_VERSION = sh (
581+
script: 'npm view "$(echo "console.log(require(\'./package.json\').name);" | node)" version || true',
582+
returnStdout: true
583+
).trim()
584+
echo "RELEASED_VERSION: ${RELEASED_VERSION}"
585+
586+
// skip deploying, if version has not changed.
587+
// However, we deploy dev branch even if version has not changed
588+
// as long as there is any new commit
589+
if (NEW_VERSION == RELEASED_VERSION && BRANCH_NAME != DEV_BRANCH) {
590+
echo 'New version and released version are the same. Skip deploying.'
591+
PIPELINE_CONTROL.cd_skip = true
592+
}
593+
if (GIT_COMMIT == GIT_PREVIOUS_SUCCESSFUL_COMMIT) {
594+
echo 'There is no new commit. Skip deploying'
595+
PIPELINE_CONTROL.cd_skip = true
596+
}
569597
}
570598
}
571599
}
572600
}
601+
573602
/************************************************************************
574603
* STAGE
575604
* -----
@@ -582,7 +611,6 @@ pipeline {
582611
* EXECUTION CONDITIONS
583612
* --------------------
584613
* - PIPELINE_CONTROL.ci_skip is false
585-
* - PIPELINE_CONTROL.deploy is true
586614
* - The build is still successful and not unstable
587615
*
588616
* DESCRIPTION
@@ -601,47 +629,54 @@ pipeline {
601629
return PIPELINE_CONTROL.ci_skip == false
602630
}
603631
expression {
604-
return PIPELINE_CONTROL.deploy
632+
return PIPELINE_CONTROL.cd_skip == false
605633
}
606634
expression {
607635
return RELEASE_BRANCHES.contains(BRANCH_NAME)
608636
}
609-
expression {
610-
return GIT_COMMIT != GIT_PREVIOUS_SUCCESSFUL_COMMIT
611-
}
612637
}
613638
}
614639
steps {
615640
timeout(time: 5, unit: 'MINUTES') {
616641
echo 'Deploy Binary'
617-
withCredentials([usernamePassword(credentialsId: ARTIFACTORY_CREDENTIALS_ID, usernameVariable: 'USERNAME', passwordVariable: 'API_KEY')]) {
618-
619-
// Set up authentication to Artifactory
642+
script {
620643
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"
644+
// set @zowe registry
645+
sh "echo always-auth=true >> .npmrc"
623646
sh "echo @brightside:registry=https://api.bintray.com/npm/ca/brightside/ >> .npmrc"
624647
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"
648+
if (BRANCH_NAME == MASTER_BRANCH) {
649+
// Set credential
650+
sh "echo _auth=${NPM_CREDENTIALS} >> .npmrc"
651+
// Set registry
652+
sh "echo registry=$NPM_REGISTRY >> .npmrc"
653+
// deploy
654+
echo "publishing to $NPM_REGISTRY"
655+
sh "npm publish --tag latest"
656+
} else if (BRANCH_NAME == DEV_BRANCH) {
657+
// Set version
658+
def baseVersion = sh returnStdout: true, script: 'node -e "console.log(require(\'./package.json\').version.split(\'-\')[0])"'
659+
def preReleaseVersion = baseVersion.trim() + "-next." + new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("UTC"))
660+
sh "npm version ${preReleaseVersion} --no-git-tag-version"
661+
// Set credential
662+
withCredentials([usernamePassword(credentialsId: ARTIFACTORY_CREDENTIALS_ID, usernameVariable: 'USERNAME', passwordVariable: 'API_KEY')]) {
663+
sh 'curl -u $USERNAME:$API_KEY https://eu.artifactory.swg-devops.com/artifactory/api/npm/auth/ >> .npmrc'
636664
}
665+
// Set registry
666+
sh "echo registry=$TEST_NPM_REGISTRY >> .npmrc"
667+
// deploy
668+
echo "publishing to $TEST_NPM_REGISTRY"
669+
sh "npm publish --tag alpha"
670+
} else {
671+
echo "Only master and dev branch is allowed to deploy the app (current branch: $BRANCH_NAME). Skip deploying"
637672
}
638-
639-
sh "rm -f .npmrc"
640673
}
641674
}
642675
}
643676
}
644677
}
678+
679+
// Slack notification when fails or back to normal
645680
post {
646681
unsuccessful {
647682
script {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zowe-cli-cics-deploy-plugin",
3-
"version": "0.5.0",
3+
"version": "1.0.0",
44
"description": "IBM CICS Bundle generation and deployment for Zowe CLI",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)