简体   繁体   English

使用Groovy读取文件并将文件内容与管道参数进行比较

[英]Read a file and compare the contents of the file to pipeline parameters using Groovy

I have a file file.txt that gets created with some content, sample as below 我有一个包含一些内容的文件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

I have set of parameters that i get as input from my pipeline 我有从管道中输入的参数集

  **Sample parameters**
  buildnum - 1235
  prod - crm
  trunk - 104 ... etc 

I am trying to read the file and compare its data to the parameters and Print "All the parameters match" if 我正在尝试读取文件并将其数据与参数进行比较,如果出现,则打印“所有参数匹配”

 [ buildnum parameter = id  value in file , prod parameter= area value in file , branch parameter = trunk value in file ] 

Can anyone help how to write it in groovy , below doesn't work 谁能帮忙用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++
//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'

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

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