Reputation: 509
Script in package.json:
"scripts": {
"version": "echo $npm_package_version"
},
One of the stage in Jenkins pipeline:
stage('Build'){
sh 'npm install'
def packageVersion = sh 'npm run version'
echo $packageVersion
sh 'VERSION=${packageVersion} npm run build'
}
I got version from npm script output, but following line
echo $packageVersion
return null
Is packageVersion value not correctly assigned?
Edited: when using Pipeline Utility Steps with following code
stage('Build'){
def packageJSON = readJSON file: 'package.json'
def packageJSONVersion = packageJSON.version
echo packageJSONVersion
sh 'VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build'
}
I get
[Pipeline] echo
1.1.0
[Pipeline] sh
[...] Running shell script + VERSION=_16_SOME_BRANCH npm run build
So I am able to extract version, but still cannot pass it when running script
Upvotes: 17
Views: 23456
Reputation: 310
stage('Read JSON') {
steps {
script {
def packageJson = readJSON file: 'package.json'
def packageJsonVersion = packageJson.version
echo "${packageJsonVersion}"
}
}
}
Upvotes: 2
Reputation: 2618
If you have Node available:
stage {
steps {
script {
def version = sh(script: "node -p \"require('./package.json').version\"", returnStdout: true).trim()
}
}
}
Or if you're using Docker:
def version = sh(script: "docker run --rm node:slim node -p \"require('./package.json').version\"", returnStdout: true).trim()
Then you can:
sh "VERSION=${version}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build"
Upvotes: 3
Reputation: 37620
The sh
step by default returns nothing, so packageVersion
should be null
.
To return the output of the executed command, use it like this:
sh(script: 'npm run version', returnStdout: true)
This variant of sh returns the output instead of printing it.
Actually, I am wondering, why echo $packageVersion
doesn't fail with an error, as this variable is not defined, but should be echo packageVersion
.
Upvotes: 4
Reputation: 13
To access an npm environment variable outside the scope of a run-script, parse the variable with bash:
$ npm run env | grep npm_package_version | cut -d '=' -f 2
Author: https://remarkablemark.org/blog/2018/08/14/package-json-version/
Upvotes: 0
Reputation: 37
For my it this:
sh(script: "grep \"version\" package.json | cut -d '\"' -f4 | tr -d '[[:space:]]'", returnStdout: true)
Upvotes: 2
Reputation: 2598
This command will take exact property version: ...
in package.json
and can work on both Mac and Linux. The other solutions using grep
will not give you correct answer in case of you have > 1 version
keyword in your package.json
(it'll return all of them instead of just the one you want)
awk -F'"' '/"version": ".+"/{ print $4; exit; }' package.json
Upvotes: 0
Reputation: 433
Below snippet worked for me: Credits to @Ferenc Takacs
version = sh(returnStdout: true, script: "grep 'version' package.json | cut -d '"' -f4 | tr '\n' '\0'")
Upvotes: 0
Reputation: 597
This worked for me:
Full version number:
PACKAGE_VERSION = sh returnStdout: true, script: '''grep 'version' package.json | cut -d '"' -f4 | tr '\n' '\0''''
echo "Current package version: $PACKAGE_VERSION"
$ > 1.2.3
Major version only:
PACKAGE_VERSION = sh returnStdout: true, script: '''grep 'version' package.json | cut -d '"' -f4 | cut -d '.' -f1 | tr '\n' '\0''''
echo "Current package Major version: $PACKAGE_VERSION"
$ > 1
Upvotes: 1
Reputation: 37620
After your edit using readJSON
, you now get string interpolation wrong. Variables within single quotes are not replaced in Groovy, only within double quotes.
sh 'VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build'
must be
sh "VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build"
Upvotes: 8