Skip to content

Commit

Permalink
[JENKINS-41276] Do not retry if user aborted the build
Browse files Browse the repository at this point in the history
  • Loading branch information
rsandell committed Jan 25, 2017
1 parent 10b3770 commit fe87fea
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,2 +1,4 @@
target
work
.idea
*.iml
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
@@ -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.