[英]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.我想在 package.json 中包含“version”参数的值作为 Jenkins 构建名称的一部分。
I'm using the Jenkins Build Name Setter plugin - https://wiki.jenkins-ci.org/display/JENKINS/Build+Name+Setter+Plugin我正在使用 Jenkins Build Name Setter 插件 - 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 语法:
${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:这成功地创建了一个构建,但包含了 package.json 中 version 属性值周围的引号和逗号,例如:
"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?是否有不同的插件可以更好地解析 package.json 并将其放入模板,或者我应该诉诸某种正则表达式来删除我不想要的字符?
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)返回"0.0.1"
(删除逗号)
${PROPFILE#\"%\"\,,file="./mainline/projectDirectory/package.json",property="\"version\""}
which returns "0.0.1",
(no characters removed)返回"0.0.1",
(没有删除字符)
UPDATE: Tried to use the new Jenkins Token Macro plugin's JSON macro with no luck.更新:尝试使用新的 Jenkins Token Macro 插件的 JSON 宏,但没有成功。
Jenkins Build Name Setter set to update the build name with Macro: Jenkins 构建名称设置器设置为使用宏更新构建名称:
${JSON,file="./mainline/pathToFiles/package.json",path="version"}-${P4_CHANGELIST}
Jenkins build logs for this job show: Jenkins 构建此作业的日志显示:
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.我在 token-macro-2.1 中实现了一个新的宏 JSON,它接受一个文件和一个路径(这是 JSON 中你想要的值的关键层次结构)。 You can only use a single transform per macro usage.每个宏用法只能使用一个转换。
Try the token transformations #
and %
(see Token-Makro-Plugin ):尝试标记转换#
和%
(参见Token-Makro-Plugin ):
${PROPFILE#"%",file="./mainline/projectDirectory/package.json",property="\\"version\\""} ${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:对我有用的是来自 Pipeline Utility Steps 插件的 readJSON 和直接设置 currentBuild.displayName 的组合,因此:
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.省略错误处理等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.