Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 9162a21

Browse files
authored
Merge pull request #188 from mathworks/pipeline_stage_failure
Add support for stage failures in pipelines
2 parents e99162e + d35f81f commit 9162a21

File tree

8 files changed

+87
-14
lines changed

8 files changed

+87
-14
lines changed

src/main/java/com/mathworks/ci/MatlabCommandStepExecution.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ public Void run() throws Exception {
4343

4444
workspace.mkdirs();
4545

46-
int res = execMatlabCommand(workspace, launcher, listener, env);
46+
int exitCode = execMatlabCommand(workspace, launcher, listener, env);
4747

48-
getContext().setResult((res == 0) ? Result.SUCCESS : Result.FAILURE);
49-
48+
if(exitCode == 0){
49+
getContext().setResult(Result.SUCCESS);
50+
return null;
51+
}
52+
53+
// throw an exception if return code is non-zero
54+
stop(new MatlabExecutionException(exitCode));
5055
return null;
5156
}
5257

@@ -74,9 +79,6 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
7479
.println("#################### Starting command output ####################");
7580
return matlabLauncher.pwd(workspace).join();
7681

77-
} catch (Exception e) {
78-
listener.getLogger().println(e.getMessage());
79-
return 1;
8082
} finally {
8183
// Cleanup the tmp directory
8284
if (uniqueTmpFolderPath.exists()) {
@@ -99,4 +101,4 @@ private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String unique
99101

100102
matlabCommandFile.write(matlabCommandFileContent, "UTF-8");
101103
}
102-
}
104+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mathworks.ci;
2+
3+
import java.lang.Exception;
4+
5+
public class MatlabExecutionException extends Exception {
6+
7+
private final int exitCode;
8+
9+
MatlabExecutionException(int exitCode) {
10+
super(String.format(Message.getValue("matlab.execution.exception.prefix"), exitCode));
11+
this.exitCode = exitCode;
12+
}
13+
14+
/*
15+
* Function to retrieve MATLAB process's exit code.
16+
* This may require Jenkins In-process script approval.
17+
*/
18+
public int getExitCode() {
19+
return exitCode;
20+
}
21+
}

src/main/java/com/mathworks/ci/MatlabRunTestsStepExecution.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ public Void run() throws Exception {
4242

4343
workspace.mkdirs();
4444

45-
int res = execMatlabCommand(workspace, launcher, listener, env);
45+
int exitCode = execMatlabCommand(workspace, launcher, listener, env);
4646

47-
getContext().setResult((res == 0) ? Result.SUCCESS : Result.FAILURE);
48-
47+
if(exitCode == 0){
48+
getContext().setResult(Result.SUCCESS);
49+
return null;
50+
}
51+
52+
// throw an exception if returned exit code is non-zero
53+
stop(new MatlabExecutionException(exitCode));
4954
return null;
5055
}
5156

@@ -72,9 +77,6 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
7277
getRunnerScript(MatlabBuilderConstants.TEST_RUNNER_SCRIPT, envVars.expand(getCommandArgs())));
7378

7479
return matlabLauncher.pwd(workspace).join();
75-
} catch (Exception e) {
76-
listener.getLogger().println(e.getMessage());
77-
return 1;
7880
} finally {
7981
// Cleanup the runner File from tmp directory
8082
final FilePath matlabRunnerScript =

src/main/resources/config.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ builder.matlab.customcommandoption.display.name = Custom
1414
Releaseinfo.matlab.version.not.found.error = Error finding MATLAB release for given MATLAB root. Verify MATLAB root path.
1515
matlab.not.found.error = Unable to launch MATLAB from the specified location. Verify the path to MATLAB root folder.
1616
matlab.not.found.error.for.node = Unable to launch MATLAB '%s' on the node '%s'. Verify global tool configuration for the specified node.
17+
matlab.execution.exception.prefix = Received a nonzero exit code %d while trying to run MATLAB.
1718
Builder.matlab.modelcoverage.support.warning = To generate a Cobertura model coverage report, use MATLAB R2018b or a newer release.
1819
Builder.matlab.exportstmresults.support.warning = To export Simulink Test Manager results, use MATLAB R2019a or a newer release.
1920
Builder.matlab.runner.script.target.file.linux.name = run_matlab_command.sh

src/test/java/com/mathworks/ci/RunMatlabCommandStepTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
import java.io.IOException;
8+
9+
import hudson.model.Result;
810
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
911
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
1012
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -120,4 +122,33 @@ public void verifyMatrixBuild() throws Exception {
120122
j.assertLogContains("pwd", build);
121123
j.assertLogContains("ver", build);
122124
}
125+
126+
/*
127+
* Test for verifying Run Matlab Command raises exception for non-zero exit code.
128+
* */
129+
@Test
130+
public void verifyExceptionForNonZeroExitCode() throws Exception {
131+
// exitMatlab is a mock command for run_matlab_command script to exit with 1.
132+
project.setDefinition(
133+
new CpsFlowDefinition("node { try {runMATLABCommand(command: 'exitMatlab')}catch(exc){echo exc.getMessage()}}", true));
134+
135+
WorkflowRun build = project.scheduleBuild2(0).get();
136+
j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build);
137+
j.assertBuildStatusSuccess(build);
138+
}
139+
140+
/*
141+
* Test for verifying Run Matlab Command raises exception for non-zero exit code.
142+
* */
143+
@Test
144+
public void verifyExceptionStackTraceForNonZeroExitCode() throws Exception {
145+
// exitMatlab is a mock command for run_matlab_command script to exit with 1.
146+
project.setDefinition(
147+
new CpsFlowDefinition("node { runMATLABCommand(command: 'exitMatlab')}", true));
148+
149+
WorkflowRun build = project.scheduleBuild2(0).get();
150+
j.assertBuildStatus(Result.FAILURE, build);
151+
j.assertLogContains("com.mathworks.ci.MatlabExecutionException", build);
152+
j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build);
153+
}
123154
}

src/test/java/com/mathworks/ci/RunMatlabTestsStepTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77

88
import java.io.IOException;
9+
10+
import hudson.model.Result;
911
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
1012
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
1113
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -125,6 +127,15 @@ public void verifyEmptyParameter() throws Exception {
125127
j.assertLogNotContains("SimulinkTestResultsPath", build);
126128
j.assertLogNotContains("CoberturaModelCoveragePath", build);
127129
}
130+
131+
@Test
132+
public void verifyExceptionForNonZeroExitCode() throws Exception {
133+
project.setDefinition(new CpsFlowDefinition(
134+
"node {runMATLABTests()}", true));
135+
WorkflowRun build = project.scheduleBuild2(0).get();
136+
j.assertBuildStatus(Result.FAILURE, build);
137+
j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build);
138+
}
128139

129140
/*@Integ Test
130141
* Verify default command options for test Filter using selectByFolder option

src/test/resources/run_matlab_command_test.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ echo "tester_started"
55
set "arg1=%~1"
66

77
echo "%arg1%"
8+
9+
if "%arg1%" == "exitMatlab" (exit /b 1)

src/test/resources/run_matlab_command_test.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
#Copyright 2020 The MathWorks, Inc.
44

55
echo "tester_started"
6-
echo $1
6+
echo $1
7+
if [[ $1 == "exitMatlab" ]]; then
8+
exit 1
9+
fi

0 commit comments

Comments
 (0)