Jenkins 使用 Groovy 读取 Jenkinsfile 中文件的特定行 [英] Jenkins Read a Specific Line of a File in Jenkinsfile with Groovy
问题描述
我正在尝试使用 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.
我可以加载文件
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屋!