Skip to content

Commit

Permalink
[JENKINS-28697] Aborted build is marked as FAILED
Browse files Browse the repository at this point in the history
Fixed treating aborted build as SUCCESS. In addition on
a build abort there is a try to abort also Rundeck job.
  • Loading branch information
Marcin Zajaczkowski committed Jun 5, 2015
1 parent 4911cde commit 8d618fa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,6 @@
.settings
target
work

*.iml
.idea/
76 changes: 48 additions & 28 deletions src/main/java/org/jenkinsci/plugins/rundeck/RundeckNotifier.java
@@ -1,5 +1,7 @@
package org.jenkinsci.plugins.rundeck;

import static java.lang.String.format;

import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
Expand All @@ -25,6 +27,7 @@
import org.kohsuke.stapler.StaplerRequest;
import org.rundeck.api.*;
import org.rundeck.api.RundeckApiException.RundeckApiLoginException;
import org.rundeck.api.domain.RundeckAbort;
import org.rundeck.api.domain.RundeckExecution;
import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
import org.rundeck.api.domain.RundeckJob;
Expand All @@ -44,6 +47,8 @@ public class RundeckNotifier extends Notifier {
/** Pattern used for extracting the job reference (project:group/name) */
private static final transient Pattern JOB_REFERENCE_PATTERN = Pattern.compile("^([^:]+?):(.*?)\\/?([^/]+)$");

private static final int DELAY_BETWEEN_POLLS_IN_MILLIS = 5000;

private final String jobId;

private final String options;
Expand Down Expand Up @@ -182,47 +187,26 @@ private boolean notifyRundeck(RundeckClient rundeck, AbstractBuild<?, ?> build,
.setNodeFilters(parseProperties(nodeFilters, build, listener))
.build());

listener.getLogger().println("Notification succeeded ! Execution #" + execution.getId() + ", at "
+ execution.getUrl() + " (status : " + execution.getStatus() + ")");
listener.getLogger().printf("Notification succeeded ! Execution #%d, at %s (status : %s)%n",
execution.getId(), execution.getUrl(), execution.getStatus());
build.addAction(new RundeckExecutionBuildBadgeAction(execution.getUrl()));

if (Boolean.TRUE.equals(shouldWaitForRundeckJob)) {
listener.getLogger().println("Waiting for Rundeck execution to finish...");
while (ExecutionStatus.RUNNING.equals(execution.getStatus())) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
listener.getLogger().println("Oops, interrupted ! " + e.getMessage());
break;
}
execution = rundeck.getExecution(execution.getId());
}
listener.getLogger().println("Rundeck execution #" + execution.getId() + " finished in "
+ execution.getDuration() + ", with status : " + execution.getStatus());
execution = waitForRundeckExecutionToFinishAndReturnIt(rundeck, listener, execution);

if (Boolean.TRUE.equals(includeRundeckLogs)) {
listener.getLogger().println("BEGIN RUNDECK LOG OUTPUT");
RundeckOutput rundeckOutput = rundeck.getJobExecutionOutput(execution.getId(), 0, 0, 0);
if (null != rundeckOutput) {
List<RundeckOutputEntry> logEntries = rundeckOutput.getLogEntries();
if (null != logEntries) {
for (int i=0; i<logEntries.size(); i++) {
RundeckOutputEntry rundeckOutputEntry = (RundeckOutputEntry)logEntries.get(i);
listener.getLogger().println(rundeckOutputEntry.getMessage());
}
}
}
listener.getLogger().println("END RUNDECK LOG OUTPUT");
getAndPrintRundeckLogsForExecution(rundeck, listener, execution.getId());
}

switch (execution.getStatus()) {
case SUCCEEDED:
return true;
case ABORTED:
case FAILED:
case RUNNING: //possible if it was unable to abort execution after an interruption
return false;
default:
return true;
throw new IllegalStateException(format("Unexpected executions status: %s", execution.getStatus()));
}
} else {
return true;
Expand All @@ -245,7 +229,7 @@ private boolean notifyRundeck(RundeckClient rundeck, AbstractBuild<?, ?> build,

/**
* Parse the given input (should be in the Java-Properties syntax) and expand Jenkins environment variables.
*
*
* @param input specified in the Java-Properties syntax (multi-line, key and value separated by = or :)
* @param build for retrieving Jenkins environment variables
* @param listener for retrieving Jenkins environment variables and logging the errors
Expand Down Expand Up @@ -291,6 +275,42 @@ private Properties parseProperties(String input, AbstractBuild<?, ?> build, Buil
}
}

private RundeckExecution waitForRundeckExecutionToFinishAndReturnIt(RundeckClient rundeck, BuildListener listener,
RundeckExecution execution) {
listener.getLogger().println("Waiting for Rundeck execution to finish...");
try {
while (ExecutionStatus.RUNNING.equals(execution.getStatus())) {
Thread.sleep(DELAY_BETWEEN_POLLS_IN_MILLIS);
execution = rundeck.getExecution(execution.getId());
}
listener.getLogger().printf("Rundeck execution #%d finished in %s, with status : %s%n", execution.getId(),
execution.getDuration(), execution.getStatus());
} catch (InterruptedException e) {
listener.getLogger().println("Waiting was interrupted. Probably build was cancelled. Reason: " + e);
listener.getLogger().println("Trying to abort Rundeck execution...");
RundeckAbort rundeckAbort = rundeck.abortExecution(execution.getId());
listener.getLogger().printf("Abort status: %s%n", rundeckAbort.getStatus());
execution = rundeck.getExecution(execution.getId());
listener.getLogger().printf("Rundeck execution #%d aborted after %s, with status : %s%n", execution.getId(),
execution.getDuration(), execution.getStatus());
}
return execution;
}

private void getAndPrintRundeckLogsForExecution(RundeckClient rundeck, BuildListener listener, Long executionId) {
listener.getLogger().println("BEGIN RUNDECK LOG OUTPUT");
RundeckOutput rundeckOutput = rundeck.getExecutionOutput(executionId, 0, 0, 0);
if (null != rundeckOutput) {
List<RundeckOutputEntry> logEntries = rundeckOutput.getLogEntries();
if (null != logEntries) {
for (RundeckOutputEntry rundeckOutputEntry : logEntries) {
listener.getLogger().println(rundeckOutputEntry.getMessage());
}
}
}
listener.getLogger().println("END RUNDECK LOG OUTPUT");
}

@Override
public Action getProjectAction(AbstractProject<?, ?> project) {
try {
Expand Down

0 comments on commit 8d618fa

Please sign in to comment.