When executing commands from a shared library, stdout is always null. How 
do I get an handle on it ? This only happens when calling the shared 
library from a pipeline. If I run it in the 'Script Console' (with 
modification to make it execute), I get content from stdout. 

Example:

*Shared library:*

// CodeDeploy.groovy
class CodeDeploy {

    Script script

    def executeOnShell(String... command) {
        script.println "About to execute: " + command
        def process = new ProcessBuilder(command)
        process.redirectOutput()
        process.redirectErrorStream(true)
        def shell = process.start()
        InputStream shellIn = shell.getInputStream()
        BufferedReader reader = new BufferedReader(new 
InputStreamReader(shellIn))
        StringBuilder builder = new StringBuilder()
        String line = null;
        shell.waitFor()
        while ((line = reader.readLine()) != null) {
            builder.append(line);
            builder.append("\n");
        }


        script.println('Output from command: ' + builder.toString())

        int shellExitStatus = shell.exitValue();
        script.println('Exit value: ' + shellExitStatus)

        return builder.toString();
    }
}

*Pipeline Usage:*
//Jenkinsfile

@Library('jenkins-pipeline-libraries')
import com.wizrocket.jenkins.CodeDeploy

pipeline {

    agent any

    options {
        timestamps()
        buildDiscarder(logRotator(numToKeepStr: '9999'))
        disableConcurrentBuilds()
        timeout(time: 1, unit: 'HOURS')
    }

    stages {
        stage('lost output') {
            steps {
                script {
                    CodeDeploy cd = new CodeDeploy(script: this)
                    cd.executeOnShell('time', 'sleep', '10')
                }
            }
        }
    }

    post {
        always {
            deleteDir()
            echo 'I will always say Hello again!'
        }
    }
}

*Output:*

[Pipeline] }[Pipeline] // stage[Pipeline] timestamps[Pipeline] {[Pipeline] 
timeout*14:00:49* Timeout set to expire in 1 hr 0 min[Pipeline] {[Pipeline] 
stage[Pipeline] { (lost output)[Pipeline] script[Pipeline] {

[Pipeline] echo*14:00:49* About to execute: [time, sleep, 10][Pipeline] 
echo*14:00:59* Output from command: [Pipeline] echo*14:00:59* Exit value: 
0[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] 
stage[Pipeline] { (Declarative: Post Actions)[Pipeline] deleteDir[Pipeline] 
echo*14:00:59* I will always say Hello again![Pipeline] }[Pipeline] // 
stage[Pipeline] }[Pipeline] // timeout[Pipeline] }[Pipeline] // 
timestamps[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: 
SUCCESS



-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/df48a7c7-0e52-4206-9352-92d2fd238b18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to