简体   繁体   中英

Setting Jenkins build name from package.json version value

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'

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.

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

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

(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM