How to Deploy an NPM Library to Nexus Repo Using Jenkins Pipeline?
Asked by
+keep the receipt
Hey everyone! I'm looking for some guidance on deploying my custom NPM library to a repository using a Jenkins pipeline. I've successfully set this up for Maven artifacts in the past, but I'm not sure how to adapt my pipeline to publish an NPM library. Currently, my stage looks like this:
```groovy
stage('push artifact to nexus') {
steps {
nexusArtifactUploader artifacts: [[
artifactId: 'custom-npm-lib',
classifier: '',
file: '???',
type: 'tar???']],
credentialsId: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
groupId: '????',
nexusUrl: 'my-nexus-hostname:8584',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'my-npm-repo',
version: '0.0.1'
}
}
```
My questions are: Should I use 'npm publish' or 'npm deploy'? What's the equivalent of 'mvn package' for NPM? And can someone provide an example of how to use nexusArtifactUploader to successfully push the library to the repo? Thank you in advance!
Answers
Glad you managed to work through it! Just a heads up, your final stage looks solid. Authenticating properly and ensuring you're publishing from the right directory is key. If you're ever in doubt, check the configurations on your local machine that worked and replicate them exactly in your Jenkins environment.
Reply to this answer
If you've tried the above and are getting authentication errors (`ENEEDAUTH`), ensure that your environment is set up correctly. It sounds like you might need to authenticate your machine. Run `npm adduser` or check your `.npmrc` for correct tokens. Also, make sure that your Jenkins is using the correct credentials stored under `npm_token`.
Reply to this answer
You'll want to use `npm publish` in your Jenkins pipeline instead of `nexusArtifactUploader` for NPM packages. Start by setting up a `.npmrc` like this:
```
//my-nexus-hostname:8081/repository/my-npm-repo/:_authToken=${NPM_TOKEN}
```
Then, in your Jenkins stage, you can use:
```groovy
sh '''
echo "//my-nexus-hostname:8081/repository/my-npm-repo/:_authToken=${NPM_TOKEN}" > .npmrc
npm publish --registry=http://my-nexus-hostname:8081/repository/my-npm-repo/
''''
Be sure you have a hosted NPM repository set up on Nexus, and store the NPM_TOKEN securely in your Jenkins credentials.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions