4

I'm trying to integrate my MATLAB unit tests into Jenkins in a Windows environment. My problem is that I am not able to get the MATLAB output in my Jenkins console, even for a simple disp('Hello World!').

I create a Jenkins free job to execute the following batch command: matlab -nodisplay -r "disp('Hello World!');exit".

Here is my result:

C:\Jenkins\jobs\runAllTests\workspace>matlab -nodisplay -r "disp('Hello World!');exit" 
C:\Jenkins\jobs\runAllTests\workspace>exit 0 
Finished: SUCCESS

Of course, I want to use Jenkins to execute a script to run several unit tests. But the problem is the same, I am not able to catch the MATLAB outputs.

Thanks in advance for any help.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
toto
  • 81
  • 6

2 Answers2

4

I finally manage to get the matlab output in the console. I follow a tip given in @AndyCampbell blog by @Guy Starbuck:

start /wait matlab -nodesktop -nosplash -minimize -wait -r "disp('Hello World!');exit" -logfile unittestlog.txt
set output=%errorlevel%
MORE unittestlog.txt
EXIT %output%

thanks all for your help.

Amro
  • 123,847
  • 25
  • 243
  • 454
toto
  • 81
  • 6
  • Sounds great I am glad you are up and running. The curvy quotes issue is definitely pernicious. Also, you beat me to it but I was going to suggest adding a "type logfile" to your batch script but you are getting the same with your call to MORE unittestlog.txt. Another interesting approach is to actually point the -logfile option directly to the jenkins log. something like -logfile "%JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\log". I tried this quickly and on the plus side it actually prints the output while the job is running. However, it might conflict with other Jenkins job output. – Andy Campbell Jun 16 '16 at 13:12
  • Thanks Andy, I will try this approach. – toto Jun 17 '16 at 09:28
  • Works fine :) thanks @AndyCampbell – toto Jun 20 '16 at 08:26
2

You need to add -wait to the MATLAB command.

On Windows, Jenkins wraps the command in a batch file that returns immediately, and therefore doesn't capture the output (and by the way, it will also always exit with a success status even if MATLAB itself didn't).

By adding -wait, it will delay the exit until MATLAB has finished, and it will also return with the appropriate exit status.

PS Also see this excellent series of posts by @AndyCampbell on integrating MATLAB with Jenkins.

Edit:

The above works for me. But here's a couple of other things I would check, as they've been gotchas for me when I was getting it set up - perhaps they will help you too:

  1. Make sure the build step is an "Execute Windows batch command" step rather than an "Execute shell" step, as it's a pain to get the unix utilities installed and running on Windows
  2. Make sure that you have the right type of quote marks in the MATLAB build command. They need to be straight quotes, not curly quotes - both the single and double ones
  3. If you copied and pasted the MATLAB build command into Jenkins, make sure you didn't accidentally paste in any extra invisible characters - try typing the command directly into Jenkins
  4. Make sure there are no licensing issues - for example, Jenkins may be running as user1 and will call MATLAB as user1, but MATLAB is licensed to user2. If you call MATLAB with -nodesktop in this case it will just silently fail (and may even leave a zombie MATLAB process hanging around, with an invisible license error dialog, that you can only quit from with Task Manager)
  5. To assist in troubleshooting, you can add -logfile \path\to\logfile.txt to your MATLAB command, which can diagnose some issues. You can also use a startup.m file and/or a finish.m file - these should run at startup (before your build command) and just before exit (after your build command). Finally you could try using a build command that does something simple to the filesystem, rather than a disp (this would diagnose whether it's an issue with MATLAB running at all, or an issue with Jenkins collecting its output).
Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • Hi Sam, Thanks for your answer. I got the same result with the -wait option. Actually, I'm already trying to follow the post of AndyCampbell (which i agree is excellent). – toto Jun 15 '16 at 13:00
  • @toto Thank you both for the kudos. toto, it sounds like Sam's answer gets you going is that right? Note you may also be interested in the -logfile option which redirects the log file generated by MATLAB to a file which can be saved as a Jenkins artifact and/or printed to standard out to be included directly in the Jenkins log. – Andy Campbell Jun 15 '16 at 13:24
  • @Andy No, the -wait option does not solve my issue, I am still not able to see the matlab output (here the 'hello world') in the corresponding jenkins job console. I'm aware of the -logfile option. With this option, the 'hello world' is written in the file, but still not in the console :( – toto Jun 15 '16 at 13:37
  • @toto it works for me - sorry. But see my edit for some extra possible tips. – Sam Roberts Jun 15 '16 at 13:41
  • @Sam: thanks again for your support. Unfortunately, all these points are OK (except maybe the 4th one, but I don't know how to check the user linked to Jenkins and to the MATLAB licence). To precise my issue, I try to run some unit tests by using a script (like described in Andy blog post). But no MATLAB output are seen in Jenkins and the Jenkins job is ok, whatever my tests results. Furthermore, I use the -logfile option, but nothing is logged whereas it works fine if I execute my script in command line. – toto Jun 15 '16 at 14:45
  • @toto This may not be the issue, but to check the user Jenkins is running as, you can use Start->Administrative Tools->Services, scroll down to Jenkins, then look in the "Log On As" column. If it's not the same as the user to which MATLAB is licensed, you can right click on it, select Properties and then switch users. You may need to start and stop Jenkins after that. I'm afraid I don't think I have any other suggestions - you may need help from a Jenkins guy, and I'm a MATLAB guy really :) – Sam Roberts Jun 15 '16 at 15:47