Jenkins 读取一个文件,并使用Groovy将该文件的内容与管道参数进行比较

Jenkins 读取一个文件,并使用Groovy将该文件的内容与管道参数进行比较,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我有一个文件file.txt,它是用一些内容创建的,示例如下 /abc/jenkins/data/jobs/random/workspace/file.txt - With below content ------------------------- [abc, hde, jef ,dgd , 1243, 324# ,23534 ....] ==> First step id - 1235 branch - 104 path - dthdp:/opt/usr/

我有一个文件file.txt,它是用一些内容创建的,示例如下

 /abc/jenkins/data/jobs/random/workspace/file.txt - With below content 
  ------------------------- 
 [abc, hde, jef ,dgd , 1243, 324# ,23534 ....]
  ==> First step
  id - 1235
  branch - 104
  path - dthdp:/opt/usr/nimb/src/emc/crm/104/mat/1235
  area - crm
  rev - 10
  status - Running
我有一组参数,作为管道的输入

  **Sample parameters**
  buildnum - 1235
  prod - crm
  trunk - 104 ... etc 
我正在尝试读取文件,并将其数据与参数进行比较,如果需要,则打印“所有参数匹配”

有人能帮我用groovy写吗,下面的代码不起作用

    def file = new File(env.WORKSPACE+"/file.txt")
            def lineCount = 0
          file.eachLine { line ->
    def parts = line.split '\n'
              println "parts"
    if ( parts != 'params.buildnum' )
       System.err.println "Failure! Doesnt match with buildnm..."
    if ( parts !=  'params.trunk' )
      System.err.println "Failure! Doesnt match with trunk..."
     lineCount++

groovy.lang.GroovyRuntimeException:在groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1732)的groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1532)的groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1532)@DevOops中找不到与之匹配的构造函数,请使用:
新文件(文件路径).newReader()(“/ads/jenkins/data/jobs/form/workspace/filelog.txt”).newReader()我仍然会收到相同的错误检查修改后的答案。对于您,只需使用
def reader=…
注释一行,然后取消注释另一行即可。
    def file = new File(env.WORKSPACE+"/file.txt")
            def lineCount = 0
          file.eachLine { line ->
    def parts = line.split '\n'
              println "parts"
    if ( parts != 'params.buildnum' )
       System.err.println "Failure! Doesnt match with buildnm..."
    if ( parts !=  'params.trunk' )
      System.err.println "Failure! Doesnt match with trunk..."
     lineCount++
//just test data from file
def data = ''' 
[abc, hde, jef ,dgd , 1243, 324# ,23534 ....]
  ==> First step
  id - 1235
  branch - 104
  path - dthdp:/opt/usr/nimb/src/emc/crm/104/mat/1235
  area - crm
  rev - 10
  status - Running
  '''
def m = /\s*(\w+)\s*-\s*(.*)/

def reader = new StringReader(data)
//to get reader from file use the following code:
//def reader = new File(FILE_PATH).newReader()

//read lines and keep only valid according to regexp
def lines = reader.readLines().findAll{it=~m}
//convert valid lines into a map
def fmap = lines.collectEntries{ (it=~m).find{true}.with{[it[1],it[2]]} }
//validate value in map against parameter
assert fmap.id == '1235'