Skip to content

Commit d88c840

Browse files
authored
Add tagging feature to npm publishing (#492)
Signed-off-by: Sayali Gaikawad <[email protected]>
1 parent f0454a4 commit d88c840

7 files changed

+16
-8
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ jacocoTestReport {
127127
}
128128
}
129129

130-
String version = '6.8.4'
130+
String version = '6.9.0'
131131

132132
task updateVersion {
133133
doLast {

tests/jenkins/TestPublishToNpm.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TestPublishToNpm extends BuildPipelineTest {
2929
this.registerLibTester(new PublishToNpmLibTester('artifact', '/tmp/workspace/example.tgz'))
3030
super.testPipeline('tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile')
3131
assertThat(getShellCommands('npm'), hasItem(
32-
'\n npm set registry \"https://registry.npmjs.org\"\n npm set //registry.npmjs.org/:_authToken NPM_TOKEN\n npm publish /tmp/workspace/example.tgz --dry-run && npm publish /tmp/workspace/example.tgz --access public\n '
32+
'\n npm set registry \"https://registry.npmjs.org\"\n npm set //registry.npmjs.org/:_authToken NPM_TOKEN\n npm publish /tmp/workspace/example.tgz --dry-run && npm publish /tmp/workspace/example.tgz --access public --tag beta\n '
3333
))
3434
assertThat(getShellCommands('nvmrc'), hasItem('rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc'))
3535
}
@@ -39,7 +39,7 @@ class TestPublishToNpm extends BuildPipelineTest {
3939
this.registerLibTester(new PublishToNpmLibTester('github'))
4040
super.testPipeline('tests/jenkins/jobs/PublishToNpm_Jenkinsfile')
4141
assertThat(getShellCommands('npm'), hasItem(
42-
'\n npm set registry \"https://registry.npmjs.org\"\n npm set //registry.npmjs.org/:_authToken NPM_TOKEN\n npm publish --dry-run && npm publish --access public\n '
42+
'\n npm set registry \"https://registry.npmjs.org\"\n npm set //registry.npmjs.org/:_authToken NPM_TOKEN\n npm publish --dry-run && npm publish --access public --tag latest\n '
4343
))
4444
assertThat(getShellCommands('nvmrc'), hasItem('rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc'))
4545
}

tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
pipeline {
1111
environment {
12-
tag = '2.0.0'
12+
tag = '2.0.0-beta.1'
1313
repository = 'https://github.com/opensearch-project/opensearch-ci'
1414
}
1515
agent none

tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
publishToNpm.sh(
1010
npm set registry "https://registry.npmjs.org"
1111
npm set //registry.npmjs.org/:_authToken NPM_TOKEN
12-
npm publish /tmp/workspace/example.tgz --dry-run && npm publish /tmp/workspace/example.tgz --access public
12+
npm publish /tmp/workspace/example.tgz --dry-run && npm publish /tmp/workspace/example.tgz --access public --tag beta
1313
)
1414
publishToNpm.sh(rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc)

tests/jenkins/jobs/PublishToNpm_Jenkinsfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pipeline {
1818
steps {
1919
script {
2020
publishToNpm(
21-
publicationType: 'github'
21+
publicationType: 'github',
2222
)
2323
}
2424
}

tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
publishToNpm.sh(
1111
npm set registry "https://registry.npmjs.org"
1212
npm set //registry.npmjs.org/:_authToken NPM_TOKEN
13-
npm publish --dry-run && npm publish --access public
13+
npm publish --dry-run && npm publish --access public --tag latest
1414
)
1515
publishToNpm.sh(rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc)

vars/publishToNpm.groovy

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ void call(Map args = [:]) {
1818
if (args.publicationType == 'github') {
1919
checkout([$class: 'GitSCM', branches: [[name: "${env.tag}" ]], userRemoteConfigs: [[url: "${env.repository}" ]]])
2020
}
21+
def npmTag = getNpmTag("${env.tag}")
2122

2223
withCredentials([string(credentialsId: 'jenkins-opensearch-publish-to-npm-token', variable: 'NPM_TOKEN')]) {
2324
sh """
2425
npm set registry "https://registry.npmjs.org"
2526
npm set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
26-
npm publish ${artifactPath} --dry-run && npm publish ${artifactPath} --access public
27+
npm publish ${artifactPath} --dry-run && npm publish ${artifactPath} --access public --tag ${npmTag}
2728
"""
2829
}
2930
println('Cleaning up')
@@ -45,3 +46,10 @@ void parameterCheck(String publicationType, String artifactPath) {
4546
error('publicationType: github does take any argument with it.')
4647
}
4748
}
49+
50+
String getNpmTag(String githubTag) {
51+
def matcher = githubTag =~ /-(\w+)\./
52+
def npmTag = matcher ? matcher[0][1] : 'latest'
53+
println("Tagging the release as '${npmTag}'")
54+
return npmTag
55+
}

0 commit comments

Comments
 (0)