简体   繁体   English

在 Jenkinsfile 中逐行读取文件 - groovy

[英]Read file line by line in Jenkinsfile - groovy

I have a simple input file with two words in each line and parsing in Jenkinsfile (groovy), but seeing a issue,我有一个简单的输入文件,每行有两个单词,并在 Jenkinsfile (groovy) 中解析,但看到一个问题,

pipeline {
  agent any
  stages {
    stage('Test-1') {
      steps {
        echo 'Building..'
        script {
          for (x in 1..5) {
            println x //0,1,2,3,4,5
          }
        }
      }
    }
    stage('Test-2 ') {
      steps {
      script {
        File file = new File("my_file.txt")
        def line, noOfLines = 0;
        file.withReader { reader ->
          while ((line = reader.readLine()) != null) {
            println "${line}"
            noOfLines++
          }
        }
      }
      }
    }
    stage('Test-3') {
      steps {
          script {
          def whole_file_data = readFile(file: 'my_file.txt')
            whole_file_data.eachLine { String line ->
              def (p1, p2) = line.tokenize()
              println("line:${line}")
              println("p1:${p1} & p2:${p2}")
            }
        }
      }
    }
  }
}

The Test-1 works fine for testing, The Test-2 gives me error as Test-1 可以很好地进行测试,而 Test-2 给我的错误是

Scripts not permitted to use new java.io.File java.lang.String. Administrators can decide whether to approve or reject this signature.

The Test-3 stage prints ONLY FIRST Line and gives Warning as, Test-3 阶段仅打印第一行并给出警告,

[Pipeline] readFile
expected to call java.lang.String.eachLine but wound up catching org.jenkinsci.plugins.workflow.cps.CpsClosure2.call; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/
line:1111 one
[Pipeline] echo

My Jenkins version is Jenkins 2.249.1 What is causing issue?我的 Jenkins 版本是 Jenkins 2.249.1 导致问题的原因是什么? p1:1111 & p2:One p1:1111 & p2:一个

Test 1:测试1:

This is a shell script iterator.这是一个 shell 脚本迭代器。 I am unsure how it relates to the other two stages, but it is fine as is.我不确定它与其他两个阶段的关系如何,但它很好。

Test 2:测试 2:

This uses the File class in a Pipeline to read a file on the filesystem.这使用管道中的File class 来读取文件系统上的文件。 This has both security issues requiring admin approval pending sandbox mode enablement, and also only works on the Jenkins master given the usage in the question.这有两个安全问题需要管理员批准挂起沙盒模式启用,并且也仅适用于 Jenkins 主机给定问题中的用法。 Indeed, you encounter the security issue, and this usage should be avoided for all of these reasons.实际上,您会遇到安全问题,出于所有这些原因,应该避免这种用法。

Test 3:测试 3:

This uses the readFile pipeline step method in the Pipeline.这使用了 Pipeline 中的readFile管道步骤方法。 This is best practices, and should be greatly preferred over the usage in Test 2. The String content in the file is iterated, and the line is printed with correct usage.这是最佳实践,应该比测试 2 中的用法更可取。文件中的字符串内容被迭代,并以正确的用法打印该行。 The problem is with the line:问题出在这条线上:

def (p1, p2) = line.tokenize()

where the Tuple (p1, p2) is assigned to the Array return from tokenize (and implicitly recast).其中 Tuple (p1, p2)被分配给从tokenize返回的 Array(并隐式重铸)。 This lambda iterator scope tuple is causing a Cps issue with your pipeline as you can see in the error.如您在错误中看到的,此 lambda 迭代器 scope 元组导致您的管道出现Cps问题。 You can fix this either by re-assigning to null at the end of your script block:您可以通过在script块末尾重新分配给null来解决此问题:

p1 = null
p2 = null

or by not even using the temporary variables in the first place:或者甚至不使用临时变量:

println("p1:${line.tokenize()[0]} & p2:${line.tokenize()[1]}")

However, a safer alternative to both of these would probably be to iterate over the tokenized line instead, and print it without newline characters to ensure expected formatting.但是,对这两种方法更安全的替代方法可能是迭代标记化的行,并在没有换行符的情况下打印它以确保预期的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM