简体   繁体   English

使用groovy在詹金斯奴隶上写一个CSV文件

[英]write a csv file on a jenkins slave using groovy

I need to write a csv file from a large json file on a jenkins slave using groovy. 我需要使用groovy从jenkins从属服务器上的大型json文件写入一个csv文件。 Previously the method I used first was only ran on the 'Master' see below: 以前,我首先使用的方法仅在“主”上运行,请参见下文:

def file = new FilePath(channel, envVars['WORKSPACE'] + separator + 'FDCUtilities' + separator + 'GroovyTest' + separator + 'json.json')
def outPutCSV = envVars['WORKSPACE'] + separator + 'FDCUtilities' + separator + 'GroovyTest' + separator + 'test.csv'

def results = jsonSlurper.parseText(file.readToString())
def FILE_HEADER = ['ID','TEST NAME','TOTALLINES', 'TOTAL COVERED', 'COVERED %']

new File(outPutCSV).withWriter { fileWriter ->
    csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT)
    csvFilePrinter.printRecord(FILE_HEADER)
    results.each{
        csvFilePrinter.printRecord([it.id, it.name, it.totalLines, it.totalCovered, it.coveredPercent])
    }
}

Seeing as we can no longer use file and must use 'FilePath' I cannot figure out for the life of me how to the previous csv writer with the new FilePath. 看来我们不能再使用文件,而必须使用“ FilePath”,所以我无法终生想出如何使用新FilePath来访问以前的csv编写器。 My thought is you just can't as I am having trouble finding documentation where you can either append a file with filepath or write csv's with it. 我的想法是您做不到,因为我在查找文档时遇到麻烦,您可以在其中添加带有文件路径的文件或使用它编写csv文件。 My thought was just to make a string value and assigning csv to the write file, however, I cannot get it to seem to look right or work properly. 我的想法只是创建一个字符串值并将csv分配给写入文件,但是,我无法使它看起来正确或正常工作。

My current code: 我当前的代码:

def jsonSlurper = new JsonSlurper()

// access the files on the current workspace regardless slave or master
def file = new FilePath(channel, envVars['WORKSPACE'] + separator + 'FDCUtilities' + separator + 'GroovyTest' + separator + 'json.json')
def outPutCSV = new FilePath(channel, envVars['WORKSPACE'] + separator + 'FDCUtilities' + separator + 'GroovyTest' + separator + 'test.csv')

def results = jsonSlurper.parseText(file.readToString())

    test = "ID,TEST NAME,TOTAL LINES,TOTAL COVERED,COVERED %"
    results.each {
        test = test.concat(it.id, it.name, it.totalLines, it.totalCovered, it.coveredPercent, "\n")
    }

    outPutCSV.write(test ,null)

I am still learning groovy and jenkins working together so any help would be greatly appreciated! 我仍在学习groovy和jenkins的合作,因此将不胜感激!

Do not use Groovy's I/O functions as they would be executed on the Jenkins Master. 不要使用Groovy的I / O函数,因为它们会在Jenkins Master上执行。 Always use the Pipeline DSL steps , in this case writeFile . 始终使用Pipeline DSL步骤 ,在这种情况下为writeFile

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

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