Home > Mobile >  How to run a program on agent and capture the return code or string
How to run a program on agent and capture the return code or string

Time:08-26

I am setting up a simple pipeline in jenkins. At one stage I need to run a program that exists on the agent hard drive and capture the output. I have just a psudo code in mind, I have Something like this:

pipeline {
    agent {
        label 'MyAgent'
    }

    stages {
        stage('Hello') {
            steps {                
                echo 'Hello World'   p_TargetDevice
             // Run a program on agent
             // var result = execute c:\test\program.exe 
             // if result = 0 then continue to next stage
             // else print/log the result and stop the pipeline

            }
        }
        
        stage('Bye') {
            steps {
                echo 'Goodbye World'
            }
        }
    }
}

CodePudding user response:

If you are on windows you can use bat: Windows Batch Script Step to execute your command and then use the option returnStatus: true to return the execution status. Check the following sample.

pipeline {
    agent {
        label 'MyAgent'
    }

    stages {
        stage('Hello') {
            steps {                
             echo 'Hello World'   p_TargetDevice
          script {
                   command = 'execute c:\test\program.exe'
                   batExit = bat returnStatus: true,label: 'Test Code',
                         script: """
                              ${command}
                            """
                     echo "batExit = $batExit"
              }
            }
        }
        
        stage('Bye') {
            steps {
                echo 'Goodbye World'
            }
        }
    }
}

  • Related