55 *
66 */
77
8+ import hudson .model .Run ;
9+ import hudson .util .ArgumentListBuilder ;
10+ import java .io .ByteArrayOutputStream ;
11+ import java .io .File ;
812import java .io .IOException ;
913import org .jenkinsci .plugins .workflow .steps .StepContext ;
1014import 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}
0 commit comments