Jenkins Read a Specific Line of a File in Jenkinsfile with Groovy

我的梦境 提交于 2021-02-18 18:04:28

问题


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. The problem is, File and readLines() are not allowed.

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? I am using Jenkins 2.32


回答1:


Just going to leave documented here, but you can also use readLines().

def file = readFile location
def lines = file.readLines()

From this other question




回答2:


I Tried the suggestion of tim_yates from the comments but System was also forbidden. What ultimately worked for me was just changing System.getProperty("line.separator") to new line character "\n".

So the full answer was in its simplicity:

file.split("\n")[n]


来源:https://stackoverflow.com/questions/43740474/jenkins-read-a-specific-line-of-a-file-in-jenkinsfile-with-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!