Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jenkins

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. 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

like image 875
vkopio Avatar asked Dec 07 '22 18:12

vkopio


2 Answers

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

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

From this other question

like image 158
Thiago Avatar answered Dec 11 '22 10:12

Thiago


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]
like image 26
vkopio Avatar answered Dec 11 '22 09:12

vkopio