This document describes the problem statement and how to automate the process of releasing an NPM package.

Background & Problem Statement

Currently, to release an NPM package, a developer must update the package version in the source code, create a pull request (PR), and wait for the PR to be merged. Once the PR is merged, CircleCI will automatically publish the new version to the NPM repository. However, the developer cannot be sure that the package has been published until they manually check the NPM repository. This release process is also not transparent and it can be difficult to track if the package has failed.

Also manually releasing NPM packages can be time-consuming and error-prone, leading to problems such as releasing the wrong version of the package or releasing the package to the wrong repository. so to solve this problem we are proposing the below solution.

Solution

A simple solution is to automate the release process using Jenkins. This will ensure that releases are always done in a consistent and reliable manner.

Design

The design of the NPM package release automation through the Jenkins process is as follows:

  • Bumps the version number of the package.

  • Commits the changes to the package's source code.

  • Pushes the changes to the repository.

  • Publishes the package to the NPM registry.

This Jenkins job can be configured to run on a schedule or it can be triggered by manually or a changes to the package's source code.

Implementation

The NPM package release automation through the Jenkins process can be implemented by following these steps:

  1. Create a new Jenkins job.

  2. In the "Script" field, add the below code snippet.

  3. In the "Branches to build" section, select the branch that we want to release the package from.

  4. In the "Parameters" section, add a parameter called "version". The value of the parameter should be the version number of the package that we want to release.

  5. Once the Jenkins job is saved, we can trigger it to run. If the job succeeds, the NPM package will be released to the NPM registry.

(Note: In the "Parameters" section of the Jenkins job, we need to add two parameters named branchName and version with types of String and String.)

Here is an example of a script that can be used to automate the release of an NPM package:

Code snippet

1node { 2 // Get the branch name from the Jenkins job parameters 3 def branchName = params.branchName 4 5 // Get the NPM package version from the Jenkins job parameters 6 def version = params.version 7 8 // Change directory to the package's directory 9 sh 'cd projects/questionset-editor-library' 10 11 // Validate the package version 12 def validateVersion = sh(script: "npm version ${npmPackageVersion} --verify", returnStdout: true).trim() 13 14 // If the version is not valid, exit the script 15 if (!validateVersion) { 16 echo "The NPM package version is not valid." 17 exit 1 18 } 19 20 // Check if the current version is already published 21 def publishedVersions = sh(returnStdout: true, script: 'npm view <package-name> versions --json').trim() 22 def publishedVersion = publishedVersions.split(',')[0] 23 24 // Check if the package is already published 25 // def isPublished = sh(returnStdout: true, script: 'npm list --depth=0 | grep ${npmPackageVersion}') 26 27 // If the current version is not published, bump the version and commit it to GitHub 28 if (version != publishedVersion) { 29 // Bump package version 30 sh 'npm version ${version}' 31 32 // Build the application 33 sh 'npm run build' 34 35 // Go back to the root directory 36 sh 'cd ../..' 37 38 // Commit the changes to the source code 39 sh 'git add package.json' 40 sh 'git commit -m "Bump version to ${version}"' 41 42 // Push the changes to the repository 43 sh 'git push origin ${branchName}' 44 45 // Change directory to the package's dist directory 46 sh 'cd dist/questionset-editor-library' 47 48 // Publish the package to the NPM registry 49 sh 'npm publish --access public' 50 } else { 51 echo "The NPM package is already published." 52 exit 1 53 } 54}
  • This script will first get the branchName and version parameters from the Jenkins job parameters.

  • If the version is valid, bump the version.

  • Then, it will check if the current version of the NPM package is already published. If it is not, the script will bump the version and commit it to GitHub.

  • Finally, the script will publish the package to the NPM repository.

  • Show a status to the user about the status of the release.


Benefits:

The benefits of NPM package release automation through Jenkins include:

  • Increased consistency: Releases will always be done in a consistent and reliable manner.

  • Reduced errors: The risk of human error is minimized.

  • Improved efficiency: The time and effort required to release packages is reduced.

  • Improved visibility: The release process is more transparent and easy to track.


Shell script:

1#!/bin/bash 2 3# Get the branch name and NPM package version from Jenkins parameters 4branch_name=$1 5npm_package_version=$2 6 7# Change directory to the project root 8cd /path/to/project 9 10# Check if the NPM package is already published 11npm_package_url="https://registry.npmjs.org/$npm_package_name@$npm_package_version" 12if curl -s $npm_package_url > /dev/null; then 13 echo "The NPM package is already published." 14else 15 # Commit the NPM package version to GitHub 16 git add package.json 17 git commit -m "Bumping NPM package version to $npm_package_version" 18 git push origin $branch_name 19 20 # Publish the NPM package to the repository 21 npm publish 22fi


1./publish_npm_package.sh master 1.0.0