Search code examples
npmcontinuous-integrationartifactorynpm-package

How to publish/deploy a npm package to custom artifactory


I want to do something like this:

  1. Create an npm package. Basically, a common code which I want to use for all of my projects. Which I created.
  2. But now What I want is, Every time I commit something in git for this project, Jenkins should build it with updated alpha/beta version and should publish to my own artifactory.

Solution

  • Your Jenkins job can be configured to be triggered by a webhook, which would take care of the first part (every time I commit). Depending on which Git server you're using you can find a lot of tutorials how to do that:

    please note this is just a random selection of tutorials how to set up the webhook triggers to work with Git servers and by no means an exhaustive list

    To publish your package to JFrog Artifactory you can either use the Jenkins Artifactory Plugin, or use the NPM command line. If you want to use the npm command line, you'll need to authenticate first:

    # setting the default registry to Artifactory
    npm config set registry http://<ARTIFACTORY_SERVER_DOMAIN>:8081/artifactory/api/npm/npm-repo/
    # log in
    npm login
    

    alternatively you can get a .npmrc file directly from Artifactory using:

    curl -u admin:<CREDENTIAL> http://<ARTIFACTORY_SERVER_DOMAIN>:8081/artifactory/api/npm/auth
    

    After that, there are two ways you can push your package to Artifactory:

    • Edit your package.json file and add a publishConfig section to a local repository: "publishConfig":{"registry":"http://localhost:8081/artifactory/api/npm/npm-repo/"}
    • Provide a local repository to the npm publish command: npm publish --registry http://localhost:8081/artifactory/api/npm/npm-repo/