From d88c840ed78f2f190315c32aab5df96b79cfa529 Mon Sep 17 00:00:00 2001 From: Sayali Gaikawad <61760125+gaiksaya@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:58:28 -0700 Subject: [PATCH] Add tagging feature to npm publishing (#492) Signed-off-by: Sayali Gaikawad --- build.gradle | 2 +- tests/jenkins/TestPublishToNpm.groovy | 4 ++-- .../jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile | 2 +- .../jobs/PublishToNpmUsingTarball_JenkinsFile.txt | 2 +- tests/jenkins/jobs/PublishToNpm_Jenkinsfile | 2 +- tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt | 2 +- vars/publishToNpm.groovy | 10 +++++++++- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 4fcdc4131..dca8e5be4 100644 --- a/build.gradle +++ b/build.gradle @@ -127,7 +127,7 @@ jacocoTestReport { } } -String version = '6.8.4' +String version = '6.9.0' task updateVersion { doLast { diff --git a/tests/jenkins/TestPublishToNpm.groovy b/tests/jenkins/TestPublishToNpm.groovy index e7d1c1b2b..8a9e35aad 100644 --- a/tests/jenkins/TestPublishToNpm.groovy +++ b/tests/jenkins/TestPublishToNpm.groovy @@ -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')) } @@ -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')) } diff --git a/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile b/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile index 90ff2ccf3..f79e20173 100644 --- a/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile +++ b/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile @@ -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 diff --git a/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile.txt b/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile.txt index f9d5ffdaf..910efbb92 100644 --- a/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile.txt +++ b/tests/jenkins/jobs/PublishToNpmUsingTarball_JenkinsFile.txt @@ -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) diff --git a/tests/jenkins/jobs/PublishToNpm_Jenkinsfile b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile index 86942e1fc..18e4e0556 100644 --- a/tests/jenkins/jobs/PublishToNpm_Jenkinsfile +++ b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile @@ -18,7 +18,7 @@ pipeline { steps { script { publishToNpm( - publicationType: 'github' + publicationType: 'github', ) } } diff --git a/tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt index b06d475f7..0bd58fe58 100644 --- a/tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt +++ b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt @@ -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) diff --git a/vars/publishToNpm.groovy b/vars/publishToNpm.groovy index 0bad12cd4..5f54697de 100644 --- a/vars/publishToNpm.groovy +++ b/vars/publishToNpm.groovy @@ -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') @@ -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 +} \ No newline at end of file