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

Commit 6265ee0

Browse files
authored
Build visulization support for pipeline (#278)
1 parent 2f875aa commit 6265ee0

File tree

3 files changed

+94
-16
lines changed

3 files changed

+94
-16
lines changed

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

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

8+
import hudson.model.Run;
9+
import hudson.util.ArgumentListBuilder;
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.File;
812
import java.io.IOException;
913
import org.jenkinsci.plugins.workflow.steps.StepContext;
1014
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
@@ -21,6 +25,9 @@ public class MatlabBuildStepExecution extends SynchronousNonBlockingStepExecutio
2125

2226
private String tasks;
2327
private String startupOptions;
28+
private static String DEFAULT_PLUGIN = "+ciplugins/+jenkins/getDefaultPlugins.m";
29+
private static String BUILD_REPORT_PLUGIN = "+ciplugins/+jenkins/BuildReportPlugin.m";
30+
private static String TASK_RUN_PROGRESS_PLUGIN = "+ciplugins/+jenkins/TaskRunProgressPlugin.m";
2431

2532
public MatlabBuildStepExecution(StepContext context, String tasks, String startupOptions) {
2633
super(context);
@@ -38,12 +45,21 @@ public Void run() throws Exception {
3845
final FilePath workspace = getContext().get(FilePath.class);
3946
final TaskListener listener = getContext().get(TaskListener.class);
4047
final EnvVars env = getContext().get(EnvVars.class);
48+
final Run<?,?> build = getContext().get(Run.class);
4149

4250
//Make sure the Workspace exists before run
4351

4452
workspace.mkdirs();
53+
System.out.println("THE ROOT DIR IS"+ build.getRootDir().toString());
4554

46-
int exitCode = execMatlabCommand(workspace, launcher, listener, env);
55+
int exitCode = execMatlabCommand(workspace, launcher, listener, env, build);
56+
//Add build result action
57+
FilePath jsonFile = new FilePath(workspace, ".matlab/buildArtifact.json");
58+
if(jsonFile.exists()){
59+
jsonFile.copyTo(new FilePath(new File(build.getRootDir().getAbsolutePath()+"/buildArtifact.json")));
60+
jsonFile.delete();
61+
}
62+
build.addAction(new BuildArtifactAction(build, workspace));
4763

4864
if(exitCode != 0){
4965
// throw an exception if return code is non-zero
@@ -60,20 +76,26 @@ public void stop(Throwable cause) throws Exception {
6076
}
6177

6278
private int execMatlabCommand(FilePath workspace, Launcher launcher,
63-
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
79+
TaskListener listener, EnvVars envVars, Run<?,?> build) throws IOException, InterruptedException {
6480
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
6581
final String uniqueBuildFile =
6682
"build_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
6783
final FilePath uniqueTmpFolderPath =
6884
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
6985

7086
// Create MATLAB script
71-
createMatlabScriptByName(uniqueTmpFolderPath, uniqueBuildFile, listener);
87+
createMatlabScriptByName(uniqueTmpFolderPath, uniqueBuildFile, listener, envVars);
88+
89+
// Copy JenkinsLogging plugin in temp folder
90+
copyFileInWorkspace(DEFAULT_PLUGIN,DEFAULT_PLUGIN,uniqueTmpFolderPath);
91+
copyFileInWorkspace(BUILD_REPORT_PLUGIN,BUILD_REPORT_PLUGIN,uniqueTmpFolderPath);
92+
copyFileInWorkspace(TASK_RUN_PROGRESS_PLUGIN,TASK_RUN_PROGRESS_PLUGIN,uniqueTmpFolderPath);
93+
7294
ProcStarter matlabLauncher;
7395

74-
try {
75-
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
76-
"setenv('MW_ORIG_WORKING_FOLDER', cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"')); "+ uniqueBuildFile, startupOptions, uniqueTmpFldrName);
96+
try (BuildConsoleAnnotator bca = new BuildConsoleAnnotator(listener.getLogger(), build.getCharset())) {
97+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, bca, envVars,
98+
"setenv('MW_ORIG_WORKING_FOLDER', cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"'));"+ uniqueBuildFile, startupOptions, uniqueTmpFldrName);
7799
listener.getLogger()
78100
.println("#################### Starting command output ####################");
79101
return matlabLauncher.pwd(workspace).join();
@@ -86,24 +108,80 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
86108
}
87109
}
88110

89-
private void createMatlabScriptByName(FilePath uniqueTmpFolderPath, String uniqueScriptName, TaskListener listener) throws IOException, InterruptedException {
111+
private void createMatlabScriptByName(FilePath uniqueTmpFolderPath, String uniqueScriptName, TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
90112

91113
// Create a new command runner script in the temp folder.
92114
final FilePath matlabBuildFile =
93115
new FilePath(uniqueTmpFolderPath, uniqueScriptName + ".m");
94116
final String tasks = getContext().get(EnvVars.class).expand(getTasks());
117+
118+
// Set ENV variable to override the default plugin list
119+
envVars.put("MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE", "ciplugins.jenkins.getDefaultPlugins");
95120
String cmd = "buildtool";
96121

97122
if (!tasks.trim().isEmpty()) {
98123
cmd += " " + tasks;
99124
}
100125
final String matlabBuildFileContent =
101-
"cd(getenv('MW_ORIG_WORKING_FOLDER'));\n" + cmd;
126+
"addpath(pwd);cd(getenv('MW_ORIG_WORKING_FOLDER'));\n" + cmd;
102127

103128
// Display the commands on console output for users reference
104129
listener.getLogger()
105130
.println("Generating MATLAB script with content:\n" + cmd + "\n");
106131

107132
matlabBuildFile.write(matlabBuildFileContent, "UTF-8");
108133
}
134+
135+
public ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
136+
Launcher launcher, BuildConsoleAnnotator bca, EnvVars envVars, String matlabCommand, String startupOpts, String uniqueName)
137+
throws IOException, InterruptedException {
138+
// Get node specific temp .matlab directory to copy matlab runner script
139+
FilePath targetWorkspace;
140+
ProcStarter matlabLauncher;
141+
ArgumentListBuilder args = new ArgumentListBuilder();
142+
if (launcher.isUnix()) {
143+
targetWorkspace = new FilePath(launcher.getChannel(),
144+
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);
145+
146+
// Determine whether we're on Mac on Linux
147+
ByteArrayOutputStream kernelStream = new ByteArrayOutputStream();
148+
launcher.launch()
149+
.cmds("uname")
150+
.masks(true)
151+
.stdout(kernelStream)
152+
.join();
153+
154+
String binaryName;
155+
String runnerName = uniqueName + "/run-matlab-command";
156+
if (kernelStream.toString("UTF-8").contains("Linux")) {
157+
binaryName = "glnxa64/run-matlab-command";
158+
} else {
159+
binaryName = "maci64/run-matlab-command";
160+
}
161+
162+
args.add(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "/" + runnerName);
163+
args.add(matlabCommand);
164+
args.add(startupOpts.split(" "));
165+
166+
matlabLauncher = launcher.launch().envs(envVars).cmds(args).stdout(bca);
167+
168+
// Copy runner for linux platform in workspace.
169+
copyFileInWorkspace(binaryName, runnerName, targetWorkspace);
170+
} else {
171+
targetWorkspace = new FilePath(launcher.getChannel(),
172+
workspace.getRemote() + "\\" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);
173+
174+
final String runnerName = uniqueName + "\\run-matlab-command.exe";
175+
176+
args.add(targetWorkspace.toString() + "\\" + runnerName, "\"" + matlabCommand + "\"");
177+
args.add(startupOpts.split(" "));
178+
179+
matlabLauncher = launcher.launch().envs(envVars).cmds(args).stdout(bca);
180+
181+
// Copy runner for Windows platform in workspace.
182+
copyFileInWorkspace("win64/run-matlab-command.exe", runnerName,
183+
targetWorkspace);
184+
}
185+
return matlabLauncher;
186+
}
109187
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
139139
BuildConsoleAnnotator bca = new BuildConsoleAnnotator(listener.getLogger(), build.getCharset());
140140
String options = getStartupOptions() == null ? "" : getStartupOptions().getOptions();
141141
try {
142-
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
142+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, bca, envVars,
143143
"setenv('MW_ORIG_WORKING_FOLDER', cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"'));"+ uniqueBuildFile, options, uniqueTmpFldrName);
144144

145145

@@ -152,7 +152,6 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
152152
return 1;
153153
} finally {
154154
bca.forceEol();
155-
bca.close();
156155
// Cleanup the tmp directory
157156
if (uniqueTmpFolderPath.exists()) {
158157
uniqueTmpFolderPath.deleteRecursive();
@@ -186,7 +185,8 @@ private void createMatlabScriptByName(FilePath uniqueTmpFolderPath, String uniqu
186185
matlabCommandFile.write(matlabCommandFileContent, "UTF-8");
187186
}
188187

189-
public ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
188+
189+
private ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
190190
Launcher launcher, BuildConsoleAnnotator bca, EnvVars envVars, String matlabCommand, String startupOpts, String uniqueName)
191191
throws IOException, InterruptedException {
192192
// Get node specific temp .matlab directory to copy matlab runner script

src/main/resources/com/mathworks/ci/BuildArtifactAction/index.jelly

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@
5959
<j:forEach var="p" items="${it.buildArtifact}" varStatus="status">
6060
<tr>
6161
<td class="pane" align="left">
62-
<a href="../console#matlab-${p.taskName}">${p.taskName}</a>
62+
<a href="../console#matlab-${p.taskName}]">${p.taskName}</a>
6363
</td>
6464
<td class="pane" style="width:9em">
6565
<span class="${pst.cssClass}">
66-
<j:if test="${p.taskStatus != false}">
67-
<a href="../console#matlab-${p.taskName}"><font color="#EF2929"> FAILED </font> </a>
66+
<j:if test="${p.taskFailed != false}">
67+
<a href="../console#matlab-${p.taskName}]"><font color="#EF2929"> FAILED </font> </a>
6868
</j:if>
69-
<j:if test="${p.taskStatus == false}">
69+
<j:if test="${p.taskFailed == false}">
7070
<j:if test="${p.taskSkipped == false}">
7171
<font color="#729FCF"> PASSED </font>
7272
</j:if>
@@ -78,7 +78,7 @@
7878
</td>
7979
<td class="pane" style="width:10em">
8080
<span class="${pst.cssClass}">
81-
${p.description}
81+
${p.taskDescription}
8282
</span>
8383
</td>
8484
<td class="pane no-wrap" style="width:6em" data="${p.duration}"> ${p.taskDuration}</td>

0 commit comments

Comments
 (0)