Home > database >  How to update version number of package.json file automatically
How to update version number of package.json file automatically

Time:11-06

I have a requirement for upgrading the package.json version automatically through jenkins. We have the node project which will build through Jenkins, everything is fine but when we want to upgrade version in package.json everytime we need to manually make changes in package.json and then push to Jenkins through GitLab.

Is there any way to automate this step??

Here is Jenkinsfile

  pipeline {
  agent any
 stages {
    stage('Build') {
      steps {
         checkout([$class: 'GitSCM', branches: [[name: 'master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ID', url: 'https://gitlab.com/company/website.git']]])
             sh "pwd"
             sh "npm install"
             sh "npm run build"  
          }
       }
        stage('deploy') {
          steps {
                 sh "scp -v -o StrictHostKeyChecking=no -r /var/lib/jenkins/workspace/project/build/* ubuntu@prod:/var/www/project/"
         }
       }       
     }      
   }

Here is package.json file

  "name": "my-project",
  "version": "1.1.24",
  "description": "web application",
  "main": "index.js",
  "repository": "https://gitlab.com/",
  "private": true,

CodePudding user response:

Issue is resolved with awk command line tool in linux

#!/usr/bin/awk

awk -F'["]' -v OFS='"'  '/"version":/{split($4,a,".");$4=a[1] 1"."a[2]"."a[3] 1};1' ./package.json > ./package2.json && mv ./package2.json ./package.json
  • Related