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

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

问题描述

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

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.

我可以加载文件

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") 更改为换行符 " ".

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

所以完整的答案在于它的简单性:

So the full answer was in its simplicity:

file.split("
")[n]

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

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