Search code examples
windowsmatlabjenkinscommand-lineexit-code

How to return exit status of Matlab to Jenkins console output?


So far,I have tried using Jenkins to trigger matlab and do some test.But I want to let Jenkins know that if we have some error in matlab test,and communicate with Jenkins.So the console output will be failure.

Matlab version:2016b

So I put exit(1) in the last line of matlab script.But Jenkins console output still reveal SUCCESS.And I use %errorlevel% in batch command line,the result was 0 not what I expect.

I have tried use Start /wait in the beginning.Because I use Windows 10.But isn't help.

Below is my Jenkins batch command:

start /wait matlab.exe -wait -r -sd "D:\matlab" MyScript;exit -logfile OutputPrint.txt echo ExitCode is %errorlevel%

I expect that if some test cases in test manager didn't pass.It could inform Jenkins and the console output will be failure.

Thanks in advance for any help!


Solution

  • It seems that official support for providing an exit or error status from matlab was introduced in R2019a. I say this, because I see it in the online reference in R2019a, but it is missing in R2018b:

    https://www.mathworks.com/help/releases/R2019a/matlab/ref/quit.html
    vs
    https://www.mathworks.com/help/releases/R2018b/matlab/ref/quit.html

    However when I try it and do not give the 'force' input, it does seem to work for older versions of matlab, e.g. for R2016b:

    "C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
    echo %ERRORLEVEL%
    

    This gives me 10, which is what I expected. In your code, I see a couple of things that may give issues. I recommend to start with something simple as in my code and then add additional components if necessary and verify the errorlevel is still ok.

    1. I see you're using start, this may block the exit code from reaching Jenkins
    2. I see
        -r -sd "D:\matlab" MyScript;exit
    

    since the command to execute should come directly after -r, I think you meant:

        -sd "D:\matlab" -r MyScript;exit
    
    1. Finally because of the command "echo" after the matlab command, the errorlevel will be from the echo command. So if you want Jenkins to get the errorlevel from the MATLAB command, but you do want to run something after matlab, you should probably capture it in a separate variable and "reset" the errorlevel just before exiting the script, e.g.:
    "C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
    SET MATLABERROR=%ERRORLEVEL%
    echo ExitCode is %ERRORLEVEL%
    SET ERRORLEVEL=%MATLABERROR%