Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI with MATLAB

I have gitlab CI running testing some scripts and I've used the following lines of .gitlab-ci.yml to show the output of MATLAB builds:

before_script:

test1:
 script:
   - matlab -nosplash -nodesktop -minimize -wait -logfile matlab-output.txt -r Model
   - type matlab-output.txt

This works perfectly when the build is sucessful however not when it fails because the second command doesn't run. I've check gitlab-ci-runner and it doesn't have an 'after_script' option. How'd you tackle this?

Note: this is Windows.

like image 690
Zaki Mohzani Avatar asked Jan 07 '16 04:01

Zaki Mohzani


1 Answers

I think that your issue is two-fold. It's partially because GITLAB doesn't call your type statement, but also that the MATLAB process never returns because the script never completes.

For example, type these at the command line:

# This one will fail and notice that it never ends
matlab -nodesktop -nosplash -minimize -wait -logfile log.txt -r 'disp(a); exit;'

This is because MATLAB is never able to execute the exit command.

On the other hand, in the case of a success, it is able to reach the exit and therefore returns.

# This one will pass
matlab -nodesktop -nosplash -minimize -wait -logfile log.txt -r 'disp(1); exit;'

The way that I actually solve this is two-fold. First, I wrap the command I'm trying to call in a try/catch statement and then convert any errors/exceptions to a string format and display those.

I have this sort of thing within a file called runtests.m

% runtests.m
exit_code = 0;

try
    Model
catch ME
    disp(getReport(ME))
    exit_code = 1;
end

% Ensure that we ALWAYS call exit
exit(exit_code);

Then I have a bash script which actually makes the call to MATLAB and prints the log output and returns the same error code that was returned from MATLAB

# runtests.sh
LOGFILE=log.txt

matlab -nodesktop -nosplash -minimize -wait -logfile "$LOGFILE" -r 'runtests';
CODE=$?

cat "$LOGFILE"

exit $CODE

The added benefit here is that my users can run the tests exactly as GITLAB CI runs them on their own machines.

And then my .gitlab-ci.yml file is quite simple

test1:
  script:
  - "runtests.sh"
like image 173
Suever Avatar answered Oct 17 '22 19:10

Suever