13

Here is my package.json looks like:

{ "name": "Myproject",
"version": "0.4.13",

Note:Here 4 is not the minor version.0013 is minor

"dependencies": {
"lodash": "^4.0.0",
"vinyl-fs": "2.2.1"
},
"repository": {},
"devDependencies": {
.........
......

How can I automate versioning of package.json using Jenkins build.

Required format should be: 0.4.13-$BUILD_NUMBER So far I try to do it using sed command:

sed -i "s/version: .*/version: 0.4.13-$BUILD_NUMBER/" package.json

But it's not updating version number in package.json file. Also used

npm version 0.4.13-$BUILD_NUMBER

FYI:The generated build artifact should look like 0.0013-1.war

Happy
  • 427
  • 2
  • 4
  • 17

1 Answers1

7

If you're using grunt, you could use the recommendation here.

Alternatively, there's a built in function in npm that does this for you. Run npm version, docs here.

Community
  • 1
  • 1
Dandy
  • 1,466
  • 16
  • 31
  • 1
    Thank you @Dandy.I already gone through those.But the problem with `npm version` is it's not generating the version number as I expected.The format I'm looking for is `0.0013-1.war` – Happy Oct 13 '16 at 16:23
  • There's also [`yarn version`](https://classic.yarnpkg.com/en/docs/cli/version/) for anyone using yarn – icc97 Sep 22 '20 at 10:37
  • Please check my [NodeAutoVersionPush][1] macro. It's a script using Visual Studio Code API to auto set the new version then do commit and push with a keyboard shortcut. The new version is based on current date and total commits. But you can easily tweak the code to your preference or for any other language. [1]: https://github.com/Sherbeeny/NodeAutoVersionPush – M. Sherbeeny Mar 10 '22 at 22:15