Skip to content

Commit

Permalink
Create publishToNpm library (#21)
Browse files Browse the repository at this point in the history
* Add npm publishing lib

Signed-off-by: Sayali Gaikawad <[email protected]>
  • Loading branch information
gaiksaya committed Nov 4, 2022
1 parent adc2338 commit c53f06c
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 3 deletions.
53 changes: 53 additions & 0 deletions tests/jenkins/TestPublishToNpm.groovy
Original file line number Diff line number Diff line change
@@ -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
}
}
24 changes: 24 additions & 0 deletions tests/jenkins/jobs/PublishToNpm_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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'
)
}
}
}
}
}
10 changes: 10 additions & 0 deletions tests/jenkins/jobs/PublishToNpm_Jenkinsfile.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
standardReleasePipeline.script(groovy.lang.Closure)
standardReleasePipeline.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
standardReleasePipeline.sh(docker image prune -f --all)
1 change: 0 additions & 1 deletion tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
standardReleasePipeline.script(groovy.lang.Closure)
standardReleasePipeline.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
standardReleasePipeline.sh(docker image prune -f --all)
42 changes: 42 additions & 0 deletions tests/jenkins/lib-testers/PublishToNpmLibTester.groovy
Original file line number Diff line number Diff line change
@@ -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'
}
}
22 changes: 22 additions & 0 deletions vars/publishToNpm.groovy
Original file line number Diff line number Diff line change
@@ -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 <required> - Repository to be used to publish the artifact to npm
@param args.tag <required> - 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"""
}
}
1 change: 0 additions & 1 deletion vars/standardReleasePipeline.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void call(Map args = [:], Closure body) {
always {
script {
postCleanup()
sh 'docker image prune -f --all'
}
}
}
Expand Down

0 comments on commit c53f06c

Please sign in to comment.