3
votes

I want to include the value of the "version" parameter in package.json as part of the Jenkins build name.

I'm using the Jenkins Build Name Setter plugin - https://wiki.jenkins-ci.org/display/JENKINS/Build+Name+Setter+Plugin

So far I've tried to use PROPFILE syntax in the "Build name macro template" step:

${PROPFILE,file="./mainline/projectDirectory/package.json",property="\"version\""}

This successfully creates a build, but includes the quotes and comma surrounding the value of the version property in package.json, for example:

"0.0.1",

I want just the value inside returned, so it reads

0.0.1

How can I do this? Is there a different plugin that would work better for parsing package.json and getting it into the template, or should I resort to some sort of regex for removing the characters I don't want?

UPDATE:

I tried using token transforms based on reading the Token Macro Plugin documentation, but it's not working:

${PROPFILE%\"\,#\",file="./mainline/projectDirectory/package.json",property="\"version\""}

still just returns

However, using only one escaped character and only one of # or % works. No other combinations I tried work.

${PROPFILE%\,,file="./mainline/projectDirectory/package.json",property="\"version\""}

which returns "0.0.1" (comma removed)

${PROPFILE#\"%\"\,,file="./mainline/projectDirectory/package.json",property="\"version\""}

which returns "0.0.1", (no characters removed)


UPDATE: Tried to use the new Jenkins Token Macro plugin's JSON macro with no luck.

Jenkins Build Name Setter set to update the build name with Macro:

${JSON,file="./mainline/pathToFiles/package.json",path="version"}-${P4_CHANGELIST}

Jenkins build logs for this job show:

10:57:55 Evaluated macro: 'Error processing tokens: Error while parsing action 'Text/ZeroOrMore/FirstOf/Token/DelimitedToken/DelimitedToken_Action3' at input position (line 1, pos 74):
10:57:55 ${JSON,file="./mainline/pathToFiles/package.json",path="version"}-334319
10:57:55                                                                          ^
10:57:55 
10:57:55 java.io.IOException: Unable to serialize org.jenkinsci.plugins.tokenmacro.impl.JsonFileMacro$ReadJSON@2707de37'
3
Where is that PROPFILE-makro defined?AlexS
Yoiu probably have a copy-paste-error in your last code sample.AlexS
PROPFILE is not meant to be used with JSON, PROPFILE is for use with properties files (similar to ini files). It would probably be better to add support for JSON in a new macro. In addition the % and # are for removing things from the beginning and end of the string, not all occurrences in the string.slide
Ok, I could write a new macro, but I was hoping this one would work well enough for my purposes. I'm looking for removing instances from the beginning and end - specifically removing one " character at the beginning and two characters, one " and one , at the end of the string. That is not removing all occurrences in the string. When I try PROPFILE#\"%\"\, I would expect it to remove these three characters, but it does not.nyarasha
You can't mix transforms, only a single transform can be used. I implemented a JSON macro and released token-macro. It should be available if you install 2.1slide

3 Answers

3
votes

I implemented a new macro JSON, which takes a file and a path (which is the key hierarchy in the JSON for the value you want) in token-macro-2.1. You can only use a single transform per macro usage.

2
votes

Try the token transformations # and % (see Token-Makro-Plugin):

${PROPFILE#"%",file="./mainline/projectDirectory/package.json",property="\"version\""}

1
votes

(This will only help if you are using pipelines. But for what it's worth,..)

What works for me is a combination of readJSON from the Pipeline Utility Steps plugin and directly setting currentBuild.displayName, thusly:

script {
    // readJSON from "Pipeline Utility Steps"
    def packageJson = readJSON file: 'package.json'
    def version = packageJson.version

    echo "Setting build version: ${packageJson.version}"
    currentBuild.displayName = env.BUILD_NUMBER + " - " + packageJson.version
    // currentBuild.description = "other cool stuff"
}

Omitting error handling etc obvs.