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

Commit af09101

Browse files
committed
Update build action to use new temp folder for build report
1 parent 5fb8b97 commit af09101

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public String getUrlName() {
6060

6161
public List<BuildArtifactData> getBuildArtifact() throws ParseException, InterruptedException, IOException {
6262
List<BuildArtifactData> artifactData = new ArrayList<BuildArtifactData>();
63-
FilePath fl = new FilePath(new File(build.getRootDir().getAbsolutePath() + "/" + BUILD_ARTIFACT_FILE));
63+
FilePath fl = new FilePath(new File(build.getRootDir().getAbsolutePath(), BUILD_ARTIFACT_FILE));
6464
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(fl.toURI())), "UTF-8")) {
6565
Object obj = new JSONParser().parse(reader);
6666
JSONObject jo = (JSONObject) obj;

src/main/java/com/mathworks/ci/actions/RunMatlabBuildAction.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public void run() throws IOException, InterruptedException, MatlabExecutionExcep
5454
runner.addEnvironmentVariable(
5555
"MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE",
5656
"ciplugins.jenkins.getDefaultPlugins");
57+
runner.addEnvironmentVariable(
58+
"MW_MATLAB_TEMP_FOLDER",
59+
runner.getTempFolder().toString());
5760

5861
// Redirect output to the build annotator
5962
runner.redirectStdOut(annotator);
@@ -75,6 +78,20 @@ public void run() throws IOException, InterruptedException, MatlabExecutionExcep
7578

7679
try {
7780
runner.runMatlabCommand(command);
81+
82+
// Handle build result
83+
Run<?,?> build = this.params.getBuild();
84+
FilePath jsonFile = new FilePath(runner.getTempFolder(), "buildArtifact.json");
85+
if (jsonFile.exists()) {
86+
FilePath rootLocation = new FilePath(
87+
new File(
88+
build.getRootDir().getAbsolutePath(),
89+
"buildArtifact.json")
90+
);
91+
jsonFile.copyTo(rootLocation);
92+
jsonFile.delete();
93+
build.addAction(new BuildArtifactAction(build));
94+
}
7895
} catch (Exception e) {
7996
this.params.getTaskListener().getLogger()
8097
.println(e.getMessage());
@@ -90,20 +107,5 @@ public void run() throws IOException, InterruptedException, MatlabExecutionExcep
90107
System.err.println(e.toString());
91108
}
92109
}
93-
94-
// Handle build result
95-
Run<?,?> build = this.params.getBuild();
96-
FilePath jsonFile = new FilePath(params.getWorkspace(), ".matlab" + File.separator + "buildArtifact.json");
97-
if (jsonFile.exists()) {
98-
FilePath rootLocation = new FilePath(
99-
new File(
100-
build.getRootDir()
101-
.getAbsolutePath()
102-
+ File.separator
103-
+ "buildArtifact.json"));
104-
jsonFile.copyTo(rootLocation);
105-
jsonFile.delete();
106-
build.addAction(new BuildArtifactAction(build));
107-
}
108110
}
109111
}

src/main/resources/+ciplugins/+jenkins/BuildReportPlugin.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
function runTaskGraph(plugin, pluginData)
88
[email protected](plugin, pluginData);
9-
[fID, msg] = fopen(fullfile(getenv("WORKSPACE"),".matlab/buildArtifact.json"), "w");
10-
9+
[fID, msg] = fopen(fullfile(getenv("MW_MATLAB_TEMP_FOLDER"),"buildArtifact.json"), "w");
1110
if fID == -1
1211
warning("ciplugins:jenkins:BuildReportPlugin:UnableToOpenFile","Could not open a file for Jenkins build result table due to: %s", msg);
1312
else
@@ -26,4 +25,4 @@ function runTaskGraph(plugin, pluginData)
2625
end
2726
end
2827
end
29-
end
28+
end

src/test/java/unit/com/mathworks/ci/actions/RunMatlabBuildActionTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public void init() {
5454
when(runner.getTempFolder()).thenReturn(tempFolder);
5555
when(tempFolder.getRemote()).thenReturn("/path/less/traveled");
5656

57-
when(params.getWorkspace()).thenReturn(tempFolder);
58-
5957
when(params.getTaskListener()).thenReturn(listener);
6058
when(listener.getLogger()).thenReturn(out);
6159

@@ -137,22 +135,22 @@ public void shouldPrintAndRethrowMessage() throws IOException, InterruptedExcept
137135
public void shouldNotAddActionIfNoBuildResult() throws IOException, InterruptedException, MatlabExecutionException {
138136
action.run();
139137

140-
verify(build, never()).addAction(any(BuildArtifactAction.class));
138+
verify(build, never()).addAction(any(BuildArtifactAction.class));
141139
}
142140

143141
@Test
144-
public void shouldCopyBuildResultsToRootAndAddsAction() throws IOException, InterruptedException, MatlabExecutionException {
142+
public void shouldCopyBuildResultsToRootAndAddAction() throws IOException, InterruptedException, MatlabExecutionException {
145143
File tmp = Files.createTempDirectory("temp").toFile();
146144
tmp.deleteOnExit();
145+
146+
File dest = Files.createTempDirectory("dest").toFile();
147+
dest.deleteOnExit();
147148

148-
File matlab = new File(tmp, ".matlab");
149-
File json = new File(matlab, "buildArtifact.json");
150-
151-
matlab.mkdirs();
149+
File json = new File(tmp, "buildArtifact.json");
152150
json.createNewFile();
153-
154-
doReturn(new FilePath(tmp)).when(params).getWorkspace();
155-
doReturn(tmp).when(build).getRootDir();
151+
152+
doReturn(new FilePath(tmp)).when(runner).getTempFolder();
153+
doReturn(dest).when(build).getRootDir();
156154

157155
boolean runTimeException = false;
158156
try {
@@ -166,7 +164,7 @@ public void shouldCopyBuildResultsToRootAndAddsAction() throws IOException, Inte
166164
// Should have deleted original file
167165
assertFalse(json.exists());
168166
// Should have copied file to root dir
169-
assertTrue(new File(tmp, "buildArtifact.json").exists());
167+
assertTrue(new File(dest, "buildArtifact.json").exists());
170168
}
171169

172170
@Test

0 commit comments

Comments
 (0)