简体   繁体   English

Jenkins使用Groovy读取Jenkinsfile中文件的特定行

[英]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. 问题是,不允许FilereadLines()

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

From this other question 从另一个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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