问题描述
我想用Groovy插件添加一个Build步骤来读取文件并根据文件内容触发构建失败。
I want to add a Build step with the Groovy plugin to read a file and trigger a build fail depending on the content of the file.
如何注入groovy插件中的工作区文件路径?
How can I inject the workspace file path in the groovy plugin ?
myFileDirectory = // Get workspace filepath here ???
myFileName = "output.log"
myFile = new File(myFileDirectory + myFileName)
lastLine = myFile.readLines().get(myFile.readLines().size().toInteger() - 1)
if (lastLine ==~ /.Fatal Error.*/ ){
println "Fatal error found"
System.exit(1)
} else{
println "nothing to see here"
}
推荐答案
根据您的意见,您最好使用 插件。
Based on your comments, you would be better off with Text-finder plugin.
它允许搜索文件以及控制台,正则表达式,然后设置构建 unstable
或失败
如果找到。
It allows to search file(s), as well as console, for a regular expression and then set the build either unstable
or failed
if found.
至于Groovy,您可以使用以下命令访问 $ {WORKSPACE}
环境变量:
def workspace = manager.build.getEnvVars()[WORKSPACE]
As for the Groovy, you can use the following to access ${WORKSPACE}
environment variable:def workspace = manager.build.getEnvVars()["WORKSPACE"]
这篇关于使用Groovy脚本从Jenkins的Workspace中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!