[英]Jenkins Read a Specific Line of a File in Jenkinsfile with Groovy
I am trying to read a specific line of an html
file in a Jenkins stage
with Groovy
and save its contents to an environment variable. 我试图在Groovy
的Jenkins stage
读取html
文件的特定行,并将其内容保存到环境变量中。 The problem is, File
and readLines()
are not allowed. 问题是,不允许File
和readLines()
。
I am able to load a file with 我可以加载文件
env.WORKSPACE = pwd()
def file = readFile "${env.WORKSPACE}/file.html"
Provided in this answer 在此答案中提供
But how can I access instantly to the contents of line n
? 但是,如何立即访问第n
行的内容? I am using Jenkins 2.32
我正在使用Jenkins 2.32
I Tried the suggestion of tim_yates from the comments but System
was also forbidden. 我从评论中尝试了tim_yates的建议,但System
也是被禁止的。 What ultimately worked for me was just changing System.getProperty("line.separator")
to new line character "\\n"
. 最终对我System.getProperty("line.separator")
只是将System.getProperty("line.separator")
更改为新的行字符"\\n"
。
So the full answer was in its simplicity: 因此,完整的答案很简单:
file.split("\n")[n]
Just going to leave documented here, but you can also use readLines(). 仅在此处保留文档,但您也可以使用readLines()。
def file = readFile location
def lines = file.readLines()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.