Forum Discussion

tvklovesu's avatar
tvklovesu
Frequent Contributor
2 years ago

How to read the error code in console output from Jenkins pipeline script

Hello,

I have configured my jenkins pipeline with Declarative pipeline script as shown below. Currently I am able to send the emails when the build is unstable. But there are some other situations where the build fails due to testExecute launch error on the remote machine. And even for that failure it is sending emails to everyone in the recipient list. I want to check if the error code is -1 then send the email only to selected emails list else send to everyone.

 

pipeline{
agent {label "windows"}
stages {
stage('Test complete'){

steps {
   Step1

}
}
}

post{
unstable {
// echo "This will run when test unstable"
emailext (
subject: "EA smoke test results in Prod",
body: "Please click below to access the test results \n" + env.BUILD_URL+"TestComplete/",
to: "vinod@xxx.com",
from: "donotreply@cloudbees.com")
}
}

}

 

console output with the error exit code to read in the above pipeline script and if that's true then send the emails to only selected people.

[TestComplete] Test runner exit code: -1.
[TestComplete] [WARNING] Error: The user did not log in using the SmartBear account or did not pass the accesskey in the command-line arguments. TestComplete will be closed.The user did not log in using the SmartBear account or did not pass the accesskey in the command-line arguments. TestComplete will be closed..
[TestComplete] [WARNING] Unable to find the log file "1677263408853.tclogx".
[TestComplete] [WARNING] Unable to find the log file "1677263408853.htmlx".
[TestComplete] [WARNING] Unable to find the log file "1677263408853.mht".
[TestComplete] [WARNING] Errors occurred during the test execution.
[TestComplete] Marking the build as UNSTABLE.
[TestComplete] [WARNING] Unable to publish test results (xml data is empty).
[TestComplete] The test execution finished (Prod_SmokeTest/CORE_Prod_Smoke). 

 

  • Try global variable currentBuild, E.G. currentBuild.rawBuild.getLog(100) get last 100 line of the log

    • tvklovesu's avatar
      tvklovesu
      Frequent Contributor

      Bonibom Is that something I can enter in my pipeline script? but how to know if error code is -1.

      I want this log to verify the error code before sending the emails.

      • Bonibom's avatar
        Bonibom
        Contributor

        Yes, it is global variable and you can use that within the script. Another way you can get the log from a particular build that your pipeline starts

        bRun = build job: 'SomeJobStartingTestComplete', parameters: [ .....
        bRun.getRawBuild().getLog(100)

         

        Then you can read the log line by line, E.G.

        List<String> log = bRun.getRawBuild().getLog(100)
        for (String line : log) 
        {
            if (line.contains("Test runner exit code: -1") 
            {
                 // Any action if exit code is -1
                 ....
            }
        }