Gitlab CI 与 MATLAB

Gitlab CI with MATLAB

我有 gitlab CI 运行正在测试一些脚本,我使用了以下 .gitlab-ci.yml 行来显示 MATLAB 构建的输出:

before_script:

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

这在构建成功时非常有效,但在构建失败时效果不佳,因为第二个命令没有 运行。我检查了 gitlab-ci-运行ner 并且它没有 'after_script' 选项。你是如何解决这个问题的?

注意:这是 Windows。

不能用CON作为输出文件吗?即

matlab -nosplash -nodesktop -minimize -wait -logfile CON -r Model

对于这个答案,我假设您的意思是 matlab ... 行失败,而 type matlab-output.txt 行没有 运行.

由于我们不知道您的 Model script/function 中包含什么,但您可以尝试将其包装在 try/catch.

例如

matlab -nosplash -nodesktop -minimize -wait -logfile matlab-output.txt -r "try; Model; end;"

如果您的模型文件中有 exit 命令,您可能需要在出现错误时强制 Matlab 退出:

matlab -nosplash -nodesktop -minimize -wait -logfile matlab-output.txt -r "try; Model; catch; quit force; end;"

您也可以将 try/catch 放入 Model.m 文件

据我了解您的问题,您希望 "type matlab-output.txt" 到 运行 这行无论是否发生故障。考虑到您有一个包含多行的作业,并且每行都可能失败,您所能做的就是创建第二个作业,或者创建一个 post-job 显然您想要 运行 即使发生故障。

例如,根据 documentation,你可以有另一份你可以 运行 总是的工作:

stages:
 - build
 - postbuild

before_script:

test1:
 stage: build
 script:
   - matlab -nosplash -nodesktop -minimize -wait -logfile matlab-output.txt -r Model
typematlab:
  stage: postbuild
  script:
   - type matlab-output.txt
  when: always

如果你有多个工作,我建议你把工作分成更多的阶段。

我认为您的问题是 two-fold。部分原因是 GITLAB 没有调用您的 type 语句,而且 MATLAB 进程永远不会 returns 因为脚本永远不会完成。

例如,在命令行输入这些:

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

这是因为 MATLAB 永远无法执行 exit 命令。

另一方面,在成功的情况下,它能够达到 exit,因此 returns。

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

我实际解决这个问题的方法是two-fold。首先,我将要调用的命令包装在 try/catch 语句中,然后将任何 errors/exceptions 转换为字符串格式并显示它们。

我在一个名为 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);

然后我有一个 bash 脚本,它实际上调用 MATLAB 并打印日志输出和 returns 从 MATLAB

返回的相同错误代码
# runtests.sh
LOGFILE=log.txt

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

cat "$LOGFILE"

exit $CODE

这里的额外好处是我的用户可以 运行 完全按照 GITLAB CI 运行 在他们自己的机器上进行测试。

然后我的 .gitlab-ci.yml 文件很简单

test1:
  script:
  - "runtests.sh"

抱歉耽搁了这么久,感谢大家的帮助。我现在有一个使用 Suever 代码的 运行 系统。我修改了它以适应 MATLAB 的单元框架。

所以,最后我的 .gitlab-ci.yml 是:

before_script:

Model-01:
  script:
    - cd developers\testModel
    - Gitlab_CI_Hook.cmd

Model-02:
  script:
    - cd developers\testModel
    - Gitlab_CI_Hook.cmd

其中 Gitlab_CI_Hook.cmd 是:

matlab -nosplash -nodesktop -minimize -wait -logfile matlab-output.txt -r Git_MATLAB_interface
type matlab-output.txt

set content=
for /f "delims=" %%i in (ExitCode.txt) do set content=%content% %%i

exit %content%

和Git_MATLAB_interface.m

% assume it always fails
exit_code = 1;

% MATLAB runs the test
result = runtests(pwd)

% check the result
if result.Passed ~= 0 && result.Failed == 0 && result.Incomplete == 0
    exit_code = 0;
end

% write the ExitCode
fid = fopen('ExitCode.txt','w');
fprintf(fid,'%d',exit_code);
fclose(fid);

% Ensure that we ALWAYS call exit that is always a success so that CI doesn't stop
exit