简体   繁体   中英

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. 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. 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. 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.

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!

Do not use Groovy's I/O functions as they would be executed on the Jenkins Master. Always use the Pipeline DSL steps , in this case writeFile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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