Setting a unique build number in Jenkins using either yarn or npm from the command line - here's how!

I'm fairly new to the world of nodejs and I am currently working on automating the regular build of a UI application using Jenkins. I have encountered a challenge with setting the 'build number' part of the version from the command line before the actual build process.

I intend to adhere to the guidelines provided at https://semver.org/ in order to achieve a resulting version number format like so:

1.2.0-dev.235

Where

  • 1.2.0 - represent the versions defined by developers and committed into the Version Control System (VCS).
  • dev.235 - is the tag that is automatically generated during the build process, indicating that 'dev' is the branch name and '235' is the build number assigned by Jenkins.

Up to this point, I have only been able to find a standard method of explicitly setting the 'build number' without affecting the 'major.minor.patch' components of the version number.

It would be ideal to have a single command line option for accomplishing this, similar to the following example:

yarn build --build-number dev.235

Is there a way for me to achieve this task?

Answer №1

Since I couldn't find an 'officially good' answer, I decided to create a workaround that works for me.

Here is how I tackled the issue...

1) Create a script in Jenkins (using a "Execute shell" step):

yarn install
node prebuild.js $MYPRJ_BUILD_NUMBER
yarn build

2) The prebuild.js script reads values from package.json and build environment variables (like MYPRD_BUILD_NUMBER) to generate a file named src/buildNumber.ts with initial content:

// Caution: This file will be re-generated on the build server
export const APP_VERSION: string = "0.0.0";
export const APP_BUILD_NUMBER: string = "localDev";

Contents of prebuild.js:

var fs = require('fs');
var package = JSON.parse(fs.readFileSync('package.json', 'utf8'));
var version = package.version
var args = process.argv.slice(2);
var buildNumber = args[0] || process.env.MYPRJ_BUILD_NUMBER || 'localDev'
var buildNumberFn = "./src/buildNumber.ts";
var buildNumberTs = 
    "// Warning: this will be re-generated on build server\n" + 
    "export const APP_VERSION: string = \"" + version + "\";\n" + 
    "export const APP_BUILD_NUMBER: string = \"" + buildNumber + "\";\n"

fs.writeFileSync(buildNumberFn, buildNumberTs);
console.info("Generated into " + buildNumberFn + "\n: " + buildNumberTs);

3) The generated build number can now be imported and used in the application just like any other JavaScript code:

import * as appVersion from './buildNumber';

function showVersion(backendVersion) {
    const version = document.createElement('div');
    version.style.cssText = "position: fixed; top: 0; right: 0; font: 9px Arial; color: gray; color: lightgrey; z-index: 1000; padding: 0 7px;";
    version.textContent = "UI: " + appVersion.APP_VERSION + "-" + appVersion.APP_BUILD_NUMBER + ", Server: " + backendVersion;
    document.body.appendChild(version);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with the npm installation of Ionic

Just starting out with the Ionic framework, I decided to install Cordova using npm with the command npm install -g cordova, which successfully installed version 6.2.0. However, when I tried to install Ionic with npm install -g ionic, I encountered an error ...

Razzle fails to initiate the server

After creating my React project with Razzle.js and downloading it from a repository, I encountered an issue when trying to run it. Upon running yarn start, the screen gets stuck at a message saying the source is compiled and server is running on localhost: ...

Install only software packages that have been released a minimum of X weeks ago

In an effort to enhance security within certain projects at my company, I am looking to limit the installation of npm packages to versions that are at least X weeks old. This precaution is intended to mitigate the risk of potential compromise by malicious ...

The presence of an imported node module that exports firebaseui results in jest tests failing due to the error message: "ReferenceError: window is not defined"

I created my own NPM package that includes various React components which utilize the firebaseui library. It's important to note that firebaseui requires the DOM to be present in order to function properly. In my index.ts, this is how I'm exporti ...

Experiencing difficulties during the installation of a React Project on a Windows operating system

I recently encountered an issue while trying to install a React Web Application on my Windows system. Everything was working perfectly until I re-installed my Windows operating system. Despite having Node and Npm installed, the application stopped function ...

npm is a tool that allows you to easily incorporate pre-existing JavaScript

Currently, I am in the process of learning about npm modules and my goal is to package this module as an npm package. I have gone through the documentation on how to create an npm module. https://docs.npmjs.com/getting-started/creating-node-modules Base ...

Automate your transformations with a Gruntfile that includes grunt-contrib-watch, browserify, and hbsfy (handlebars) plugins

I am fairly new to the world of tools like grunt, browserify, and handlebars. I have set up my gruntfile.js to monitor changes in certain .js files and then automatically run the default browserify bundle command on them. Below is a snippet from my current ...

Encountering problems with starting up Reactjs and receiving an ENOENT error. Seeking guidance on troubleshooting this issue

view image hereWhen I try to initialize react using the command prompt with npx create-react-app my-app I am getting an ENOENT error. I suspect that I may have not installed Node.js properly, as I did check Chocolatey during installation. Should I uninst ...

What is the process for installing Ionic with composer?

I attempted to set up the npm bridge by executing the following: $ composer require eloquent/composer-npm-bridge:^3 In the hopes of being able to install packages for Ionic using npm. Unfortunately, my plan seems to be failing as I keep encountering thi ...

Error encountered while trying to install npm for a web project

While attempting to install npm modules, I am encountering the following issue: PS C:\Users\maxzag\Desktop\svoi> npm install npm ERR! Cannot read property '0' of undefined npm ERR! A complete log of this run can be found ...

What steps can I take to resolve a peer dependency conflict?

After delving into some articles regarding peer dependencies, I've learned that it's commonly utilized in a plugin's package.json file. Let's say I have a project where I install package A (npm<3 || >7) which has a peer dependenc ...

The installation failed with an error message stating that it could not install from the "android" directory because there is no package.json file present

I encountered an issue while trying to run the command mentioned in the subject title. Can someone please assist me? The commands I used are: ionic cordova platform add android ionic cordova add android ionic cordova prepare android S C:\Users\j ...

What is the process for installing npm version 7.24.2 or earlier on Debian Buster?

I am currently working on a Raspberry Pi 3, running Raspberry Pi OS Lite Legacy (Debian Buster) with armv7l architecture. The node version I have installed is v10.24.0, as the application I need to run requires node version 10 or lower. To try and meet th ...

The Heroku deployment encountered an error and ended with a message stating "npm ERR! 404 Not Found: [email protected]"

While attempting to deploy nodejs to Heroku (or Firebase), I am encountering a persistent error related to [email protected]. Despite updating to version 4.0.1, the issue persists. I have tried uninstalling and reinstalling, as well as deleting the no ...

Guide on automatically updating the version number of package.json in a Jenkinsfile and renaming the 'dist' folder to match

Here is the content of my package.json file: "name": "@company/sdk", "version": "1.0.15", "description": "Created using create-react-library", "author": "me", "l ...

How to access files within a Docker container using a node package that has been installed

I am in the process of developing an npm package that needs to be installed within another node system running inside a docker container. One of the requirements for my package is to access files located at /etc/files/, however, I have been encountering d ...

Encountered an issue while running "npm run" command on a new Laravel project - Module npm-cli.js could not be located

Can someone please assist me with this issue? I encountered an error while trying to run npm run on my fresh laravel project. I started by installing a new laravel file using larave new vue-lar-proj then I installed the necessary modules using npm instal ...

I'm unable to address NPM vulnerabilities

My knowledge about npm is limited, and I'm facing an issue that needs resolution: # npm audit report semver <7.5.2 Severity: moderate semver vulnerable to Regular Expression Denial of Service - https://github.com/advisories/GHSA-c2qf-rxjj-qqgw No ...

Issue encountered during the installation of Cordova

When trying to install Cordova, I encountered an error. I am not connected through a proxy; instead, I am using a dongle for internet access. ...

I keep encountering an unusual error when I use the UUID npm module. Can someone please help me figure out

Encountering the recurring issue of receiving the error message: "Error: Package exports for 'D:\test\node_modules\uuid' do not define a '.' subpath" every time I attempt to require it. Operating System - Windows 10 Pro ...