Matlab and TeamCity integration - build agent Matlab license, exit, and xlsread issues

Hi,

I recently managed to get Matlab integrated nicely(?) into TeamCity and I thought I would share my result since there isn't much on the web to help.

I am using:
Windows 7 64 bit
TeamCity Professional 9.0.1 (build 32116)
Matlab R2014b
svn, version 1.8.11 (r1643975)

First things first, the build agent. Because of Matlab licensing you need to ensure the build agent service runs as the same user that Matlab is licensed for, not the system. Do this by opening the Window Services app, going to the 'properties' section of the TeamCity Build Agent service, selecting the 'Log On' tab, choose the 'This Account' option and then entering the users details. If you don't, your build will fail quickly with a licensing issue (you actually get weird popups for 'interactive services detection').


For the build I am using a build step with the 'Runner Type' set to 'Command Line'. Using a 'custom script' I have this to run my tests:
set mllogfile="c:\temp\test_gmarOvernight.txt"
matlab -wait -r "test_gmarOvernight" -logfile %%mllogfile%%
set mlerror=%%errorlevel%%
type %%mllogfile%%
exit %%mlerror%%

The double % are required to escape the % for accessing dos variables.



In my test_gmarOvernight.m file I ensure that I exit the code with a 0 for a pass, and another number for a fail:

try
    %Tests go here


    %% Done
    if testsFailed
        % something went wrong in one or more of the tests
        disp('FAILED!');
        exit(1); %risk tests failed exit code
    else
        disp('Success!');
        exit(0);
    end




catch thisEx
    %something went wrong with this script
    displayLastError(thisEx);
    exit(2); %test script failed exit code
end


When I run Matlab from TeamCity I get nothing in the build log from the matlab prompt running the tests. Sending the results to a logfile, and then using 'type' in the script sends the prompt text to the build log so you can see what actually happened.

Because 'type' also sets the dos errorlevel I have to save it straight after the matlab call to another dos variable and then return it as the last step in the script.



Finally, one of my tests uses xlsread. If you get an exception like this in your Matlab log:

Microsoft Office Excel cannot access the file 'd:\testing\testing.xls'.
There are several possible reasons:
• The file name or path does not exist
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.

Then there is some weird Windows automation error. This took a while to find (thanks Google for find this guys solution Author : Debarchan(MSFT) SQL Developer Engineer, Microsoft) but it worked for me:

For Windows 2008 Server x64: Create the following directory:
C:\Windows\SysWOW64\config\systemprofile\Desktop

For Windows 2008 Server x86: Create the following directory:
C:\Windows\System32\config\systemprofile\Desktop



I hope this helps someone else get going as I've been through a bit of pain solving all this!

Enjoy!
Stewart

3 comments
Comment actions Permalink

Thanks a lot !!! Everything works fine with your good tips !! You saved me at least a few hours :-)

0
Comment actions Permalink

I just worked with MATLAB BUILD on a server, It took me 3 days to figure out correct settings and not its working, I am using SYSTEM account to activate MATLAB license, instead of my user id, I had to replace it with SYSTEM. Because this is used by teamcity to run windows service and I cant login to server soo much.

Thanks for very good article. I got ideas from it.

0
Comment actions Permalink

Hi,

I just wanted to add that the C:\Windows\SysWOW64\config\systemprofile\Desktop folder is still required in Server 2016.

I was setting  up a new build agent and was getting the following errors:

System.Runtime.InteropServices.COMException : Microsoft Excel cannot access the file 'D:\TC_work\Source\bin\Release\MessageRegister.xls'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook.

As soon as I created the Desktop systemprofile folder the tests all passed.

Tim

0

Please sign in to leave a comment.