Skip to content

Instantly share code, notes, and snippets.

@RickeyWard
Created October 19, 2022 15:42
Show Gist options
  • Save RickeyWard/6d42c0bd8620456ae3fa94f106fb4e05 to your computer and use it in GitHub Desktop.
Save RickeyWard/6d42c0bd8620456ae3fa94f106fb4e05 to your computer and use it in GitHub Desktop.
Docker-Workflow-Plugin Skip Failjob on stop error patch
diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java
index ce27cea..05d5db7 100644
--- a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java
+++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java
@@ -83,6 +83,13 @@ public class DockerClient {
@Restricted(NoExternalUse.class)
public static boolean SKIP_RM_ON_STOP = Boolean.getBoolean(DockerClient.class.getName() + ".SKIP_RM_ON_STOP");
+ /**
+ * Skip failing a job if docker-stop / rm fails / times out.
+ */
+ @SuppressFBWarnings(value="MS_SHOULD_BE_FINAL", justification="mutable for scripts")
+ @Restricted(NoExternalUse.class)
+ public static boolean SKIP_FAILJOB_ON_STOP_ERROR = Boolean.getBoolean(DockerClient.class.getName() + ".SKIP_FAILJOB_ON_STOP_ERROR");
+
// e.g. 2015-04-09T13:40:21.981801679Z
public static final String DOCKER_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
@@ -181,7 +188,13 @@ public class DockerClient {
public void stop(@NonNull EnvVars launchEnv, @NonNull String containerId) throws IOException, InterruptedException {
LaunchResult result = launch(launchEnv, false, "stop", "--time=1", containerId);
if (result.getStatus() != 0) {
- throw new IOException(String.format("Failed to kill container '%s'.", containerId));
+ if(SKIP_FAILJOB_ON_STOP_ERROR) {
+ if (LOGGER.isLoggable(Level.WARNING)) {
+ LOGGER.log(Level.WARNING, "Failed to kill container {0}, SKIP_FAILJOB_ON_STOP_ERROR is enabled.", containerId);
+ }
+ } else {
+ throw new IOException(String.format("Failed to kill container '%s'.", containerId));
+ }
}
if (!SKIP_RM_ON_STOP) {
rm(launchEnv, containerId);
@@ -198,7 +211,13 @@ public class DockerClient {
LaunchResult result;
result = launch(launchEnv, false, "rm", "-f", containerId);
if (result.getStatus() != 0) {
- throw new IOException(String.format("Failed to rm container '%s'.", containerId));
+ if(SKIP_FAILJOB_ON_STOP_ERROR) {
+ if (LOGGER.isLoggable(Level.WARNING)) {
+ LOGGER.log(Level.WARNING, "Failed to rm container {0}, SKIP_FAILJOB_ON_STOP_ERROR is enabled.", containerId);
+ }
+ } else {
+ throw new IOException(String.format("Failed to rm container '%s'.", containerId));
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment