-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add npm publishing lib Signed-off-by: Sayali Gaikawad <[email protected]>
- Loading branch information
Showing
8 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters