groovyjenkins-groovy

Capture and convert the output of command into csv file in groovy


I have below code in groovy which is providing 2 different output. I want to take the first row values one by one from command1 output and substitute in command2 as variable in loop.

import java.text.SimpleDateFormat
import java.util.*
import java.text.DateFormat

ANALYSIS_HOME="/jenkins/analysis"
pipeline {
    agent { label 'analysis && linux' }

    stages {
        stage('First step') {
            steps {
                script {
                    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'ANLYSIS', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {

                        def projectsList = sh(script: "${ANALYSIS_HOME}/bin/cov-manage-im --url https://example.com/ --on-new-cert trust --user ${USERNAME} --password ${PASSWORD} --mode projects --show --fields project,creation-date,last-modified-date", returnStdout: true)
                        def streamsList = sh(script: "${ANALYSIS_HOME}/bin/cov-manage-im --url https://example.com/ --on-new-cert trust --user ${USERNAME} --password ${PASSWORD} --mode streams --show --fields stream,expiration,primary-project", returnStdout: true)

                        def projectsCSV = "${ANALYSIS_HOME}/projects_output.csv"
                        def streamsCSV = "${ANALYSIS_HOME}/streams_output.csv"

                        writeFile(file: projectsCSV, text: projectsList)
                        writeFile(file: streamsCSV, text: streamsList)
                        
                        println "CSV files have been created: ${projectsCSV} and ${streamsCSV}"
                    }
                }
            }
        }
    }
}

Below is the command output:-

Project,Creation Date,Last Modified Date
A,2022-10-26T12:22:50.191+08:00,2022-10-26T12:22:50.191+08:00
B,2024-05-14T18:06:29.835+08:00,2024-05-14T18:06:29.835+08:00
C,2024-05-20T16:47:27.400+08:00,2024-05-20T16:47:27.400+08:00
D,2024-05-23T16:55:18.828+08:00,2024-05-23T16:55:18.828+08:00
E,2022-08-30T15:11:38.953+08:00,2022-08-30T15:11:38.953+08:00
F,2024-08-13T18:38:59.696+08:00,2024-08-13T18:38:59.696+08:00


Stream,Expiration,Primary Project
A,disabled,A
G,disabled,A
B,disabled,A
C,disabled,D
E,disabled,D
F,disabled,D
E,disabled,D

How can I send the row1 output from command1 one by one in loop to second command and save second command loop output in one single file by concatinating all loop output.


Solution

  • Modify your script to write the projectsList and streamsList outputs into a file

    import java.text.SimpleDateFormat
    import java.util.*
    import java.text.DateFormat
    
    ANALYSIS_HOME="/jenkins/analysis"
    pipeline {
        agent { label 'analysis && linux' }
    
        stages {
            stage('First step') {
                steps {
                    script {
                        withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'ANLYSIS', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
    
                            def projectsList = sh(script: "${ANALYSIS_HOME}/bin/cov-manage-im --url https://example.com/ --on-new-cert trust --user ${USERNAME} --password ${PASSWORD} --mode projects --show --fields project,creation-date,last-modified-date", returnStdout: true)
                            def streamsList = sh(script: "${ANALYSIS_HOME}/bin/cov-manage-im --url https://example.com/ --on-new-cert trust --user ${USERNAME} --password ${PASSWORD} --mode streams --show --fields stream,expiration,primary-project", returnStdout: true)
    
                            def projectsCSV = "${ANALYSIS_HOME}/projects_output.csv"
                            def streamsCSV = "${ANALYSIS_HOME}/streams_output.csv"
    
                            writeFile(file: projectsCSV, text: projectsList)
                            writeFile(file: streamsCSV, text: streamsList)
                            
                            println "CSV files have been created: ${projectsCSV} and ${streamsCSV}"
                        }
                    }
                }
            }
        }
    }
    

    you will have two CSV files (projects_output.csv and streams_output.csv) saved in the specified directory (ANALYSIS_HOME).