Skip to content
Permalink
Browse files

[JENKINS-41276] Do not retry if user aborted the build

  • Loading branch information
rsandell committed Jan 25, 2017
1 parent 10b3770 commit fe87fea108a8d3f8608f15c2253090a0099cd1d4
@@ -1,2 +1,4 @@
target
work
.idea
*.iml
@@ -2,7 +2,11 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.AbortException;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.CauseOfInterruption;
import jenkins.model.InterruptedBuildAction;

/**
* @author Kohsuke Kawaguchi
@@ -61,6 +65,18 @@ public void onSuccess(StepContext context, Object result) {
@Override
public void onFailure(StepContext context, Throwable t) {
try {
Run run = context.get(Run.class);
if (run != null && t instanceof FlowInterruptedException) {
InterruptedBuildAction action = run.getAction(InterruptedBuildAction.class);
if (action != null) {
for (CauseOfInterruption cause : action.getCauses()) {
if (cause instanceof CauseOfInterruption.UserInterruption) {
context.onFailure(t);
return;
}
}
}
}
left--;
if (left>0) {
TaskListener l = context.get(TaskListener.class);
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.workflow.steps;

import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;

import static org.junit.Assert.*;

/**
* Tests {@link RetryStep}.
*/
public class RetryStepTest {

@ClassRule
public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule
public JenkinsRule r = new JenkinsRule();

@Test(timeout = 50000)
@Issue("JENKINS-41276")
public void abortShouldNotRetry() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"int count = 0; retry(3) { echo 'trying '+(count++); semaphore 'start'; sleep 10; echo 'NotHere' }", false));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("start/1", b);
b.doStop();
b = r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b));
r.assertLogContains("trying 0", b);
r.assertLogContains("Aborted by anonymous", b);
r.assertLogNotContains("trying 1", b);
r.assertLogNotContains("trying 2", b);
r.assertLogNotContains("NotHere", b);

}

}

0 comments on commit fe87fea

Please sign in to comment.