Logo

dev-resources.site

for different kinds of informations.

How to Add a Build Number to the Web Build in Angular

Published at
8/15/2023
Categories
angular
build
version
deployment
Author
sumitsamanta
Categories
4 categories in total
angular
open
build
open
version
open
deployment
open
Author
12 person written this
sumitsamanta
open
How to Add a Build Number to the Web Build in Angular

Have you ever found yourself wondering which version of a web build is running when you deploy your angular code? By adding some scripts to the angular build command I was able to solve this problem.

Instructions

Step 1 - Adding version in your environment file

In your environment.ts file, add a version property from the package.json



export const environment = {
  production: true,
  version: require('../../package.json').version,
};


Enter fullscreen mode Exit fullscreen mode

Step 2 - Create a build number by using the current time and previous build number

Create a build-version.js file on the root folder and add the following:



const { writeFileSync } = require('fs');
const { join } = require('path');

const BUILD_VERSION_PATH = join(__dirname, 'src/build-version.json');

function getTimestampBasedVersion() {
  return parseInt(new Date().getTime() / 1000);
}

let currentIteration = getTimestampBasedVersion();

try {
  currentIteration =
    require('./build-version.json')?.currentIteration ||
    getTimestampBasedVersion();
} catch (e) {
  currentIteration = getTimestampBasedVersion();
}

const buildVersion = {
  currentIteration: currentIteration + 1,
  buildVersion: `${
    require('./package.json')?.version
  }-build.${currentIteration}`,
};

writeFileSync(BUILD_VERSION_PATH, JSON.stringify(buildVersion, null, 2));


Enter fullscreen mode Exit fullscreen mode

This will try to fetch the previous build number from the build-version.json file and increment it by 1, if this file is not found then it will use the current timestamp as the build number.

The fs.writeFileSync() creates a new file if the specified file does not exist.

The path.join() method is used to join a number of path-segments using the platform-specific delimiter to form a single path.

Add build-version.json to the .gitignore file so that it does not create a commit on the build generation.

If you don’t want the current timestamp as the build number then you can start with 1 as the initial build number.



const { writeFileSync } = require('fs');
const { join } = require('path');

const BUILD_VERSION_PATH = join(__dirname, 'src/build-version.json');

let currentIteration = 1;

try {
  currentIteration =
    require('./build-version.json')?.currentIteration || currentIteration;
} catch (e) {
  currentIteration = 1;
}

const buildVersion = {
  currentIteration: currentIteration + 1,
  buildVersion: `${
    require('./package.json')?.version
  }-build.${currentIteration}`,
};

writeFileSync(BUILD_VERSION_PATH, JSON.stringify(buildVersion, null, 2));


Enter fullscreen mode Exit fullscreen mode

This approach could fail if the deployment creates a new docker image every time, then the build number will always be 1. So in those cases, we can use the current timestamp as the build number.

Step 3 - Adding bash script to generate the build number

Create a build-version.sh file and add the following:



node build-version.js
printf "\n\n"
cat src/build-version.json
printf "\n\n"


Enter fullscreen mode Exit fullscreen mode

This will generate the build version and also print the build number on the console of the docker’s or Jenkin’s console.

Step 4 - Showing the build number on the web browser

Add the following code snippet to the app.component.ts to show the build number on the console of the web browser



import { Component } from '@angular/core';
import { environment } from 'src/environments/environment';
import * as build from '../build-version.json';

@Component({
selector: 'ra-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {

constructor() {
this.displayBuildVersion();
}

/**

  • To display the current build version on the console
    */
    displayBuildVersion(): void {
    console.log(
    %cCurrent version: </span><span class="p">${</span><span class="nx">environment</span><span class="p">.</span><span class="nx">version</span><span class="p">}</span><span class="s2">,
    background: #0082af; padding: 8px 12px; border-radius: 4px; color: #fafafa; font-size: x-large
    );

    try {
    console.log(
    %cCurrent build number: </span><span class="p">${</span><span class="nx">build</span><span class="p">?.</span><span class="nx">buildVersion</span><span class="p">}</span><span class="s2">,
    background: #0082af; padding: 8px 12px; border-radius: 4px; color: #fafafa; font-size: large
    );
    } catch (e) {
    console.warn(e);
    }
    }
    }

Enter fullscreen mode Exit fullscreen mode




Step 5 - Running the build version script along with the angular build commands

Add the build version script build-version.sh to the build commands in the package.json



{
...
"version": "1.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build:prod": "bash build-version.sh && ng build --configuration production --aot",
"watch": "ng build --watch --configuration development",
"test": "ng test --code-coverage",
...
},
...
}

Enter fullscreen mode Exit fullscreen mode




Step 6 - Running your angular build and generating the build number

Once you run the build and deploy the code on the server you will be able to see the build numbers during the build in docker or Jenkins and also in the deployed website.

Jenkin’s console output showing the current build number

Web browser’s console log showing the current version and current build number

Last thoughts

Now you’ve added the build numbers, build again and again to see the build version being incremented on each build.

Happy coding!

version Article's
30 articles in total
Favicon
What is Actually GitLab?
Favicon
The Ultimate Guide: How to Check Laravel Version
Favicon
Bitbucket vs GitHub: What’s the Difference Between Both?
Favicon
Letz Understand NPM Versioning: A Beginner's Guide
Favicon
Crossing the Bridge: Migrating and Working Across Version Control Systems
Favicon
Puro — Uma forma eficiente de gerenciar as versões flutter
Favicon
Version Control Systems and Their Importance
Favicon
Check PyTorch version, CPU and GPU(CUDA) in PyTorch
Favicon
How To Use Versioning On AWS S3 Buckets To Enable Overwrite Protection Of Your Data
Favicon
What is semantic version and why knowing semantic versioning matters!
Favicon
GreptimeDB v0.6 Released - Support Migrating Table's Regions between Datanodes
Favicon
What is Semantic Versioning and why you should use it for your software ?
Favicon
Contaktlab Full Version VST Plug-ins – Elevate Your Music Production Experience Download.
Favicon
Alpha 1.0.3 Upcoming Changes (wip)
Favicon
Alpha v1.0.2 - Upcoming Changes
Favicon
Difference between tilde(~) and caret(^) in package.json?
Favicon
How to Add a Build Number to the Web Build in Angular
Favicon
Versions Are For Humans
Favicon
How to Install Any Laravel Version
Favicon
Why Your Cloud Assets Need A Time Machine
Favicon
Angular version upgrade v4 to v12 - part1
Favicon
Rust Revolution - `r` the rust version manager
Favicon
VS Code Extension - Git Graph
Favicon
React Versiyon Düşürme
Favicon
Flutter Versiyon Düşürme
Favicon
What is the differences between GIT and SVN
Favicon
Notify users when a new version of your site is available and prompt them to refresh the page.
Favicon
🔔 version-rocket 🚀
Favicon
Understanding version control and mastering git - Branches and more...!!
Favicon
Choose your preferred major version on all open-source managed software!

Featured ones: