From 9e868c4bfe936b3006beb484590b09b77d2e3147 Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 26 May 2024 16:41:49 +0300 Subject: [PATCH 1/2] more postbuildaction to function --- src/main/java/org/jfrog/bamboo/JfTask.java | 16 ++++++ .../org/jfrog/bamboo/PostBuildAction.java | 49 ------------------- 2 files changed, 16 insertions(+), 49 deletions(-) delete mode 100644 src/main/java/org/jfrog/bamboo/PostBuildAction.java diff --git a/src/main/java/org/jfrog/bamboo/JfTask.java b/src/main/java/org/jfrog/bamboo/JfTask.java index 85ebdf8..7583bc0 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,6 +103,7 @@ public class JfTask extends JfContext implements TaskType { buildLog.error(ExceptionUtils.getRootCauseMessage(e), e); return resultBuilder.failedWithError().build(); } + removeJfrogTempDir(); return resultBuilder.success().build(); } @@ -186,6 +188,20 @@ 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. + */ + private void removeJfrogTempDir() { + try { + FileUtils.deleteDirectory(BambooUtils.getJfrogTmpDir(customVariableContext)); + } catch (IOException e) { + buildLog.error("Failed to delete JFrog temporary directory: " + e.getMessage()); + } + } + /** * 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; - } -} From d820e3fa5ea7fa485ae7fb17db09ce2d0feca6ff Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 26 May 2024 17:13:07 +0300 Subject: [PATCH 2/2] Refactor function --- src/main/java/org/jfrog/bamboo/JfTask.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jfrog/bamboo/JfTask.java b/src/main/java/org/jfrog/bamboo/JfTask.java index 7583bc0..793566d 100644 --- a/src/main/java/org/jfrog/bamboo/JfTask.java +++ b/src/main/java/org/jfrog/bamboo/JfTask.java @@ -103,8 +103,7 @@ public class JfTask extends JfContext implements TaskType { buildLog.error(ExceptionUtils.getRootCauseMessage(e), e); return resultBuilder.failedWithError().build(); } - removeJfrogTempDir(); - return resultBuilder.success().build(); + return cleanUpTask(resultBuilder); } /** @@ -193,12 +192,16 @@ private int configAllJFrogServers() throws IOException, InterruptedException { * 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 void removeJfrogTempDir() { + 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(); } }