Skip to content

Commit

Permalink
Add tagging feature to npm publishing (#492)
Browse files Browse the repository at this point in the history
Signed-off-by: Sayali Gaikawad <[email protected]>
(cherry picked from commit d88c840)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Sep 10, 2024
1 parent 2405f11 commit 8408f13
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jacocoTestReport {
}
}

String version = '6.8.4'
String version = '6.9.0'

task updateVersion {
doLast {
Expand Down
4 changes: 2 additions & 2 deletions tests/jenkins/TestPublishToNpm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestPublishToNpm extends BuildPipelineTest {
this.registerLibTester(new PublishToNpmLibTester('artifact', '/tmp/workspace/example.tgz'))
super.testPipeline('tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile')
assertThat(getShellCommands('npm'), hasItem(
'\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 '
'\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 '
))
assertThat(getShellCommands('nvmrc'), hasItem('rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc'))
}
Expand All @@ -39,7 +39,7 @@ class TestPublishToNpm extends BuildPipelineTest {
this.registerLibTester(new PublishToNpmLibTester('github'))
super.testPipeline('tests/jenkins/jobs/PublishToNpm_Jenkinsfile')
assertThat(getShellCommands('npm'), hasItem(
'\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 '
'\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 '
))
assertThat(getShellCommands('nvmrc'), hasItem('rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc'))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

pipeline {
environment {
tag = '2.0.0'
tag = '2.0.0-beta.1'
repository = 'https://github.com/opensearch-project/opensearch-ci'
}
agent none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
publishToNpm.sh(
npm set registry "https://registry.npmjs.org"
npm set //registry.npmjs.org/:_authToken NPM_TOKEN
npm publish /tmp/workspace/example.tgz --dry-run && npm publish /tmp/workspace/example.tgz --access public
npm publish /tmp/workspace/example.tgz --dry-run && npm publish /tmp/workspace/example.tgz --access public --tag beta
)
publishToNpm.sh(rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc)
2 changes: 1 addition & 1 deletion tests/jenkins/jobs/PublishToNpm_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pipeline {
steps {
script {
publishToNpm(
publicationType: 'github'
publicationType: 'github',
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
publishToNpm.sh(
npm set registry "https://registry.npmjs.org"
npm set //registry.npmjs.org/:_authToken NPM_TOKEN
npm publish --dry-run && npm publish --access public
npm publish --dry-run && npm publish --access public --tag latest
)
publishToNpm.sh(rm -rf /tmp/workspace/.nvmrc && rm -rf ~/.nvmrc)
10 changes: 9 additions & 1 deletion vars/publishToNpm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ void call(Map args = [:]) {
if (args.publicationType == 'github') {
checkout([$class: 'GitSCM', branches: [[name: "${env.tag}" ]], userRemoteConfigs: [[url: "${env.repository}" ]]])
}
def npmTag = getNpmTag("${env.tag}")

withCredentials([string(credentialsId: 'jenkins-opensearch-publish-to-npm-token', variable: 'NPM_TOKEN')]) {
sh """
npm set registry "https://registry.npmjs.org"
npm set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
npm publish ${artifactPath} --dry-run && npm publish ${artifactPath} --access public
npm publish ${artifactPath} --dry-run && npm publish ${artifactPath} --access public --tag ${npmTag}
"""
}
println('Cleaning up')
Expand All @@ -45,3 +46,10 @@ void parameterCheck(String publicationType, String artifactPath) {
error('publicationType: github does take any argument with it.')
}
}

String getNpmTag(String githubTag) {
def matcher = githubTag =~ /-(\w+)\./
def npmTag = matcher ? matcher[0][1] : 'latest'
println("Tagging the release as '${npmTag}'")
return npmTag
}

0 comments on commit 8408f13

Please sign in to comment.