Jenkins使用Groovy读取Jenkinsfile中文件的特定行 [英] Jenkins Read a Specific Line of a File in Jenkinsfile with Groovy

查看:308
本文介绍了Jenkins使用Groovy读取Jenkinsfile中文件的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Groovy 在Jenkins stage 中读取 html 文件的特定行,并将其内容保存到环境变量中.问题是不允许使用 File readLines().

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"

在此答案中提供

但是如何立即访问 n 行的内容?我正在使用 Jenkins 2.32

But how can I access instantly to the contents of line n? I am using Jenkins 2.32

推荐答案

我从注释中尝试了有关tim_yates的建议,但也禁止了 System .最终对我有用的只是将 System.getProperty("line.separator")更改为新的行字符"\ n" .

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]

这篇关于Jenkins使用Groovy读取Jenkinsfile中文件的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆