How to Automate the Publishing Process of NPM Modules using Codefresh

How to Automate the Publishing Process of NPM Modules using Codefresh

3 min read

NPM is the default registry that comes with Node.js, making it currently the largest package registry in the world.

Despite its popularity, NPM still has some issues. Recently I published a new module to NPM and found that needed to manually publish to NPM every time I had an update. I used the most common use case:

Pushing to Github, merging into the master branch and publishing to the NPM registry.

git add .
git commit -m "Added new awosome feature"
git push origin master
npm publish .

As a developer, it’s painful to have to constantly publish your module over and over to two different sources. Naturally, I want to keep it simple and only push to Github. So I decided to find a way to automate all of the publishing and future steps, including running unit tests, integration tests, etc. In this post, I’ll explain how I used Codefresh to do that.

Simple Solution using Codefresh

Codefresh is a great platform to manage and automate the lifecycle of Docker-based applications. It allows you to easily configure pipelines, which are a series of steps that are triggered every time a commit is pushed. For example, I configured my pipeline to build a Docker image from my project (without using a Dockerfile in my repository), run unit tests, and when all tests pass successfully, deploy the image to my NPM registry. Codefresh provides the ability to add customize steps to my pipeline, so I can automate the publish process and solve this problem.

Let’s go over the steps I did to achieve this automation.

Preparing my project

  1. Installing ci-publish (NPM module that helped me to make it easier):
    npm install --save-dev ci-publish
  2. Adding new script to scripts in package.json. It should look like:
     "scripts": {
        "ci-publish": "ci-publish"
      }
  3. Increase the version in, package.json you may increase only the minor part to keep versioning best practices
  4. Push and merge into master branch
  5. Get NPM registry token: in order to publish to NPM registry, we should use NPM token. That token is generated after login to NPM using NPM CLI (note that the token is valid until you log out, changing the password, or invalidate it manually):

NPM login

  •  Find the token in ~/.npmrc file. Copy to the clipboard the token part, we will need it to the next step

Configure your automated pipeline in Codefresh

  • Login to Codefresh using your favorite source code manager
  • Add your repository:

    • Choose your project from the list
    • Pick the template option and select Node.js template:

    • Build your service
    • From the build view, go to the pipeline configuration
    • Add webhook
    • Change the build to match by regex only when the master branch is updated

/master/gi
    • In Deploy script section add npm run ci-publish
    • In section add Environment Variables NPM_TOKEN (you may encrypt it) and paste the token that you copied before.
    • Build it

From now on, all commits to the master branch will automatically be published to NPM (assuming the version has been increased, the build will fail otherwise since NPM will reject it).

Live example

Here you can find my repository promise-fcm that uses Codefresh to automatically publish to NPM. I use a codefresh.yml file to get more flexibility on the steps and flow within my pipeline. Check out the documentation to learn more about codefresh.yml.

Conclusion

Codefresh’s platform is not just for Docker-based applications. Users can leverage the platform and get all the benefits of CI/CD automation. We’d love to hear how you’re using Codefresh! Reply in the comment box below to let us know how you use the Codefresh platform and resources, or if you have any feedback/ questions.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Build your GitOps skills and credibility today with a GitOps Certification.

Get GitOps Certified

Ready to Get Started?
  • safer deployments
  • More frequent deployments
  • resilient deployments