extract version from package.json with bash inside jenkins pipeline
For my it this:
sh(script: "grep \"version\" package.json | cut -d '\"' -f4 | tr -d '[[:space:]]'", returnStdout: true)
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"
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')
Actually, I am wondering, why echo $packageVersion
doesn't fail with an error, as this variable is not defined, but should be echo packageVersion
.