-
Notifications
You must be signed in to change notification settings - Fork 25
/
publishToNpm.groovy
55 lines (51 loc) · 2.61 KB
/
publishToNpm.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* 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.publicationType <required> - github (will clone the repository at triggered tag and url and publish), artifact (Needs artifactPath arg to the URL or local artifact that needs to be published)
@param args.artifactPath <required with publicationType: artifact > - URL or local Path to the artifact that needs to be publish to NPM.See supported artifacts https://docs.npmjs.com/cli/v9/commands/npm-publish?v=true#description for more details.
*/
void call(Map args = [:]) {
parameterCheck(args.publicationType, args.artifactPath)
artifactPath = args.artifactPath ?: ''
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 --tag ${npmTag}
"""
}
println('Cleaning up')
sh """rm -rf ${WORKSPACE}/.nvmrc && rm -rf ~/.nvmrc"""
}
void parameterCheck(String publicationType, String artifactPath) {
allowedPublicationType = ['github', 'artifact']
if (!allowedPublicationType.contains(publicationType)) {
currentBuild.result = 'ABORTED'
error('Invalid publicationType. publicationType can either be github or artifact')
}
if (publicationType == 'artifact' && !artifactPath) {
currentBuild.result = 'ABORTED'
error('publicationType: artifact needs an artifactPath. Please provide artifactPath argument. See supported artifacts https://docs.npmjs.com/cli/v9/commands/npm-publish?v=true#description for more details')
}
if (publicationType == 'github' && artifactPath) {
currentBuild.result = 'ABORTED'
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
}