Skip to content

Commit

Permalink
Merge pull request #363 from hashar/T282893
Browse files Browse the repository at this point in the history
Cancel pending triggered build when parent project is cancelled
  • Loading branch information
gounthar authored Nov 22, 2023
2 parents b735ef8 + 36afdb3 commit b7c8232
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
try {
if (future != null ) {
listener.getLogger().println("Waiting for the completion of " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()));
Run startedRun = future.waitForStart();
Run startedRun;
try {
startedRun = future.waitForStart();
} catch (InterruptedException x) {
listener.getLogger().println( "Build aborting: cancelling queued project " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()) );
future.cancel(true);
throw x; // rethrow so that the triggering project get flagged as cancelled
}

listener.getLogger().println(HyperlinkNote.encodeTo('/' + startedRun.getUrl(), startedRun.getFullDisplayName()) + " started.");

Run completedRun = future.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@


import java.util.ArrayList;
import java.util.concurrent.Future;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
Expand All @@ -70,10 +71,13 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;


public class TriggerBuilderTest {

@Rule
Expand Down Expand Up @@ -224,6 +228,34 @@ public void testNonBlockingTrigger() throws Exception {
"Triggering projects: project1, project2, project3");
}

@Test
public void testCancelsDownstreamBuildWhenInterrupted() throws Exception{
r.jenkins.setNumExecutors(1); // the downstream-project would be in the build queue

FreeStyleProject triggerProject = r.createFreeStyleProject("upstream-project");
FreeStyleProject downstreamProject = r.createFreeStyleProject("downstream-project");

TriggerBuilder triggerBuilder = new TriggerBuilder(createTriggerConfig("downstream-project"));

triggerProject.getBuildersList().add(triggerBuilder);

QueueTaskFuture<FreeStyleBuild> parentBuild = triggerProject.scheduleBuild2(0);
parentBuild.waitForStart();

// Now cancel the trigger project and let it finishes
triggerProject.getLastBuild().getExecutor().interrupt();
parentBuild.get();

assertLines(triggerProject.getLastBuild(),
"Waiting for the completion of downstream-project",
"Build aborting: cancelling queued project downstream-project",
"Build was aborted",
"Finished: ABORTED"
);
assertNull("No downstream build has been run", downstreamProject.getLastBuild());
assertEquals("No build left in queue", 0, r.jenkins.getQueue().countBuildableItems());
}

@Test
public void testConsoleOutputWithCounterParameters() throws Exception{
r.createFreeStyleProject("project1");
Expand Down

0 comments on commit b7c8232

Please sign in to comment.