diff --git a/src/main/java/org/jfrog/bamboo/JfTask.java b/src/main/java/org/jfrog/bamboo/JfTask.java index 85ebdf8..793566d 100644 --- a/src/main/java/org/jfrog/bamboo/JfTask.java +++ b/src/main/java/org/jfrog/bamboo/JfTask.java @@ -12,6 +12,7 @@ import com.atlassian.plugin.PluginAccessor; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.exception.ExceptionUtils; +import org.codehaus.plexus.util.FileUtils; import org.jetbrains.annotations.NotNull; import org.jfrog.bamboo.config.ServerConfig; import org.jfrog.bamboo.config.ServerConfigManager; @@ -102,7 +103,7 @@ public class JfTask extends JfContext implements TaskType { buildLog.error(ExceptionUtils.getRootCauseMessage(e), e); return resultBuilder.failedWithError().build(); } - return resultBuilder.success().build(); + return cleanUpTask(resultBuilder); } /** @@ -186,6 +187,24 @@ private int configAllJFrogServers() throws IOException, InterruptedException { return exitCode; } + /** + * Removes the JFrog temporary directory that was previously created. + * This function is intentionally not part of the PostBuildAction + * because it lacks the necessary permissions to delete the directory + * when the build is executed on a remote agent. + * + * @return TaskResult status indicating the success of the cleanup. + */ + private @NotNull TaskResult cleanUpTask(TaskResultBuilder resultBuilder) { + try { + FileUtils.deleteDirectory(BambooUtils.getJfrogTmpDir(customVariableContext)); + return resultBuilder.success().build(); + } catch (IOException e) { + buildLog.error("Failed to delete JFrog temporary directory: " + e.getMessage()); + return resultBuilder.failedWithError().build(); + } + } + /** * Runs the 'jf config add' command to configure the server. * diff --git a/src/main/java/org/jfrog/bamboo/PostBuildAction.java b/src/main/java/org/jfrog/bamboo/PostBuildAction.java deleted file mode 100644 index 2444ed0..0000000 --- a/src/main/java/org/jfrog/bamboo/PostBuildAction.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.jfrog.bamboo; - -import com.atlassian.bamboo.build.CustomPostBuildCompletedAction; -import com.atlassian.bamboo.v2.build.BuildContext; -import com.atlassian.bamboo.variable.CustomVariableContext; -import org.codehaus.plexus.util.FileUtils; -import org.jetbrains.annotations.NotNull; -import org.jfrog.bamboo.utils.BambooUtils; - -import java.io.IOException; - -public class PostBuildAction implements CustomPostBuildCompletedAction { - private BuildContext buildContext; - private CustomVariableContext customVariableContext; - - /** - * Initializes the post-build action with the build context. - * - * @param buildContext The build context. - */ - @Override - public void init(final @NotNull BuildContext buildContext) { - this.buildContext = buildContext; - } - - /** - * Performs the post-build action. - * - * @return The modified build context. - * @throws IOException If an I/O error occurs. - */ - @Override - public @NotNull BuildContext call() throws IOException { - // Delete Bamboo temp directory - String fullBuildKey = buildContext.getResultKey().getKey(); - FileUtils.deleteDirectory(BambooUtils.getJfrogTmpSubdir(customVariableContext, fullBuildKey)); - return buildContext; - } - - /** - * Sets the custom variable context. - * - * @param customVariableContext The custom variable context. - */ - @SuppressWarnings("unused") - public void setCustomVariableContext(CustomVariableContext customVariableContext) { - this.customVariableContext = customVariableContext; - } -}