[inQuiry] NPM package release automation

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

node { // Get the branch name from the Jenkins job parameters def branchName = params.branchName // Get the NPM package version from the Jenkins job parameters def version = params.version // Change directory to the package's directory sh 'cd projects/questionset-editor-library' // Validate the package version def validateVersion = sh(script: "npm version ${npmPackageVersion} --verify", returnStdout: true).trim() // If the version is not valid, exit the script if (!validateVersion) { echo "The NPM package version is not valid." exit 1 } // Check if the current version is already published def publishedVersions = sh(returnStdout: true, script: 'npm view <package-name> versions --json').trim() def publishedVersion = publishedVersions.split(',')[0] // Check if the package is already published // def isPublished = sh(returnStdout: true, script: 'npm list --depth=0 | grep ${npmPackageVersion}') // If the current version is not published, bump the version and commit it to GitHub if (version != publishedVersion) { // Bump package version sh 'npm version ${version}' // Build the application sh 'npm run build' // Go back to the root directory sh 'cd ../..' // Commit the changes to the source code sh 'git add package.json' sh 'git commit -m "Bump version to ${version}"' // Push the changes to the repository sh 'git push origin ${branchName}' // Change directory to the package's dist directory sh 'cd dist/questionset-editor-library' // Publish the package to the NPM registry sh 'npm publish --access public' } else { echo "The NPM package is already published." exit 1 } }
  • 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:

#!/bin/bash # Get the branch name and NPM package version from Jenkins parameters branch_name=$1 npm_package_version=$2 # Change directory to the project root cd /path/to/project # Check if the NPM package is already published npm_package_url="https://registry.npmjs.org/$npm_package_name@$npm_package_version" if curl -s $npm_package_url > /dev/null; then echo "The NPM package is already published." else # Commit the NPM package version to GitHub git add package.json git commit -m "Bumping NPM package version to $npm_package_version" git push origin $branch_name # Publish the NPM package to the repository npm publish fi

 

./publish_npm_package.sh master 1.0.0