From c53f06c3e18bccf9f638871b2537f77886dd2f50 Mon Sep 17 00:00:00 2001 From: Sayali Gaikawad <61760125+gaiksaya@users.noreply.github.com> Date: Thu, 27 Oct 2022 16:52:47 -0700 Subject: [PATCH] Create publishToNpm library (#21) * Add npm publishing lib Signed-off-by: Sayali Gaikawad --- tests/jenkins/TestPublishToNpm.groovy | 53 +++++++++++++++++++ tests/jenkins/jobs/PublishToNpm_Jenkinsfile | 24 +++++++++ .../jenkins/jobs/PublishToNpm_Jenkinsfile.txt | 10 ++++ ...ardReleasePipelineWithArgs_JenkinsFile.txt | 1 - .../StandardReleasePipeline_JenkinsFile.txt | 1 - .../lib-testers/PublishToNpmLibTester.groovy | 42 +++++++++++++++ vars/publishToNpm.groovy | 22 ++++++++ vars/standardReleasePipeline.groovy | 1 - 8 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 tests/jenkins/TestPublishToNpm.groovy create mode 100644 tests/jenkins/jobs/PublishToNpm_Jenkinsfile create mode 100644 tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt create mode 100644 tests/jenkins/lib-testers/PublishToNpmLibTester.groovy create mode 100644 vars/publishToNpm.groovy diff --git a/tests/jenkins/TestPublishToNpm.groovy b/tests/jenkins/TestPublishToNpm.groovy new file mode 100644 index 000000000..8d90a7f6e --- /dev/null +++ b/tests/jenkins/TestPublishToNpm.groovy @@ -0,0 +1,53 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package jenkins.tests + +import jenkins.tests.BuildPipelineTest +import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString +import static org.hamcrest.CoreMatchers.hasItem +import static org.hamcrest.MatcherAssert.assertThat +import org.junit.Before +import org.junit.Test + +class TestPublishToNpm extends BuildPipelineTest { + @Override + @Before + void setUp() { + + this.registerLibTester(new PublishToNpmLibTester('https://github.com/opensearch-project/opensearch-ci', '1.0.0')) + super.setUp() + } + + @Test + public void test() { + super.testPipeline("tests/jenkins/jobs/PublishToNpm_Jenkinsfile") + } + + @Test + void 'verify shell commands'(){ + runScript('tests/jenkins/jobs/PublishToNpm_Jenkinsfile') + + def npmCommands = getShellCommands() + assertThat(npmCommands, hasItem( + 'npm set registry "https://registry.npmjs.org"; npm set //registry.npmjs.org/:_authToken NPM_TOKEN; npm publish --dry-run && npm publish --access public'.toString() + )) + + } + def getShellCommands() { + def shCommands = helper.callStack.findAll { call -> + call.methodName == 'sh' + }.collect { call -> + callArgsToString(call) + }.findAll { npmCommand -> + npmCommand.contains('npm') + } + return shCommands + } +} diff --git a/tests/jenkins/jobs/PublishToNpm_Jenkinsfile b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile new file mode 100644 index 000000000..e75fc8713 --- /dev/null +++ b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile @@ -0,0 +1,24 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +pipeline { + agent none + stages { + stage('publishToNpm') { + steps { + script { + publishToNpm( + repository: 'https://github.com/opensearch-project/opensearch-ci', + tag: '1.0.0' + ) + } + } + } + } +} diff --git a/tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt new file mode 100644 index 000000000..4b072a9cc --- /dev/null +++ b/tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt @@ -0,0 +1,10 @@ + PublishToNpm_Jenkinsfile.run() + PublishToNpm_Jenkinsfile.pipeline(groovy.lang.Closure) + PublishToNpm_Jenkinsfile.echo(Executing on agent [label:none]) + PublishToNpm_Jenkinsfile.stage(publishToNpm, groovy.lang.Closure) + PublishToNpm_Jenkinsfile.script(groovy.lang.Closure) + PublishToNpm_Jenkinsfile.publishToNpm({repository=https://github.com/opensearch-project/opensearch-ci, tag=1.0.0}) + publishToNpm.checkout({$class=GitSCM, branches=[{name=1.0.0}], userRemoteConfigs=[{url=https://github.com/opensearch-project/opensearch-ci}]}) + publishToNpm.string({credentialsId=publish-to-npm-token, variable=NPM_TOKEN}) + publishToNpm.withCredentials([NPM_TOKEN], groovy.lang.Closure) + 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) diff --git a/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt b/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt index b30fd1ff1..33d12b024 100644 --- a/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt +++ b/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt @@ -9,4 +9,3 @@ standardReleasePipeline.script(groovy.lang.Closure) standardReleasePipeline.postCleanup() postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true}) - standardReleasePipeline.sh(docker image prune -f --all) diff --git a/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt b/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt index a44747eed..9b150c67b 100644 --- a/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt +++ b/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt @@ -9,4 +9,3 @@ standardReleasePipeline.script(groovy.lang.Closure) standardReleasePipeline.postCleanup() postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true}) - standardReleasePipeline.sh(docker image prune -f --all) diff --git a/tests/jenkins/lib-testers/PublishToNpmLibTester.groovy b/tests/jenkins/lib-testers/PublishToNpmLibTester.groovy new file mode 100644 index 000000000..c4581f6f3 --- /dev/null +++ b/tests/jenkins/lib-testers/PublishToNpmLibTester.groovy @@ -0,0 +1,42 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ +import static org.hamcrest.CoreMatchers.notNullValue +import static org.hamcrest.MatcherAssert.assertThat + +class PublishToNpmLibTester extends LibFunctionTester { + + private String repository + private String tag + + public PublishToNpmLibTester(repository, tag){ + this.repository = repository + this.tag = tag + } + + void configure(helper, binding){ + helper.registerAllowedMethod("checkout", [Map], {}) + helper.registerAllowedMethod("withCredentials", [Map, Closure], { args, closure -> + closure.delegate = delegate + return helper.callClosure(closure) + }) + } + void parameterInvariantsAssertions(call){ + assertThat(call.args.repository.first(), notNullValue()) + assertThat(call.args.tag.first(), notNullValue()) + } + + boolean expectedParametersMatcher(call) { + return call.args.repository.first().toString().equals(this.repository) + && call.args.tag.first().toString().equals(this.tag) + } + + String libFunctionName(){ + return 'publishToNpm' + } +} \ No newline at end of file diff --git a/vars/publishToNpm.groovy b/vars/publishToNpm.groovy new file mode 100644 index 000000000..a7e0ead6d --- /dev/null +++ b/vars/publishToNpm.groovy @@ -0,0 +1,22 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/** Library to publish artifacts to NPM registry under @opensearch-project namespace +@param Map args = [:] args A map of the following parameters +@param args.repository - Repository to be used to publish the artifact to npm +@param args.tag - Tag reference to be used to publish the artifact +*/ +void call(Map args = [:]) { + + checkout([$class: 'GitSCM', branches: [[name: "${args.tag}" ]], userRemoteConfigs: [[url: "${args.repository}" ]]]) + + withCredentials([string(credentialsId: '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 --dry-run && npm publish --access public""" + } +} \ No newline at end of file diff --git a/vars/standardReleasePipeline.groovy b/vars/standardReleasePipeline.groovy index 5e8390dad..171351aa0 100644 --- a/vars/standardReleasePipeline.groovy +++ b/vars/standardReleasePipeline.groovy @@ -40,7 +40,6 @@ void call(Map args = [:], Closure body) { always { script { postCleanup() - sh 'docker image prune -f --all' } } }