Skip to content

Added flag to invoke doStop on WorkflowRun instead of throwing FlowInterruptedException #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ public class SCMSkipBuildStep extends Builder implements SimpleBuildStep {
private SCMSkipMatcher skipMatcher;
private boolean deleteBuild;
private String skipPattern;
private boolean doStopWorkflowRun;

public SCMSkipBuildStep(boolean deleteBuild, String skipPattern) {
this(deleteBuild, skipPattern, false);
}

public SCMSkipBuildStep(boolean deleteBuild, String skipPattern, boolean doStopWorkflowRun) {
this.deleteBuild = deleteBuild;
this.skipPattern = skipPattern;
this.doStopWorkflowRun = doStopWorkflowRun;
if (this.skipPattern == null) {
this.skipPattern = SCMSkipConstants.DEFAULT_PATTERN;
}
Expand All @@ -41,7 +47,7 @@ public SCMSkipBuildStep(boolean deleteBuild, String skipPattern) {

@DataBoundConstructor
public SCMSkipBuildStep() {
this(false, null);
this(false, null, false);
}

public boolean isDeleteBuild() {
Expand All @@ -66,6 +72,15 @@ public void setSkipPattern(String skipPattern) {
this.skipMatcher.setPattern(this.skipPattern);
}

public boolean isDoStopWorkflowRun() {
return doStopWorkflowRun;
}

@DataBoundSetter
public void setDoStopWorkflowRun(boolean doStopWorkflowRun) {
this.doStopWorkflowRun = doStopWorkflowRun;
}

@Override
public void perform(
@NonNull Run<?, ?> run,
Expand All @@ -78,7 +93,7 @@ public void perform(
SCMSkipTools.tagRunForDeletion(run, deleteBuild);

try {
SCMSkipTools.stopBuild(run);
SCMSkipTools.stopBuild(run, doStopWorkflowRun);
} catch (AbortException | FlowInterruptedException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public class SCMSkipBuildWrapper extends BuildWrapper {
private SCMSkipMatcher skipMatcher;
private boolean deleteBuild;
private String skipPattern;
private boolean doStopWorkflowRun;

@DataBoundConstructor
public SCMSkipBuildWrapper(boolean deleteBuild, String skipPattern) {
public SCMSkipBuildWrapper(boolean deleteBuild, String skipPattern, boolean doStopWorkflowRun) {
this.deleteBuild = deleteBuild;
this.skipPattern = skipPattern;
this.doStopWorkflowRun = doStopWorkflowRun;
if (this.skipPattern == null) {
this.skipPattern = SCMSkipConstants.DEFAULT_PATTERN;
}
Expand Down Expand Up @@ -59,14 +61,23 @@ public void setSkipPattern(String skipPattern) {
this.skipMatcher.setPattern(this.skipPattern);
}

public boolean isDoStopWorkflowRun() {
return doStopWorkflowRun;
}

@DataBoundSetter
public void setDoStopWorkflowRun(boolean doStopWorkflowRun) {
this.doStopWorkflowRun = doStopWorkflowRun;
}

@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener)
throws IOException, FlowInterruptedException {
if (SCMSkipTools.inspectChangeSetAndCause(build, skipMatcher, listener)) {
SCMSkipTools.tagRunForDeletion(build, deleteBuild);

try {
SCMSkipTools.stopBuild(build);
SCMSkipTools.stopBuild(build, doStopWorkflowRun);
} catch (AbortException | FlowInterruptedException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,27 @@ private static boolean inspectChangeSet(
* @throws ServletException when build stopping fails
* @throws FlowInterruptedException to terminate pipeline build
*/
public static void stopBuild(Run<?, ?> run) throws IOException, ServletException, FlowInterruptedException {
public static void stopBuild(Run<?, ?> run, boolean doStopWorkflowRun) throws IOException, ServletException, FlowInterruptedException {
run.setDescription("SCM Skip - build skipped");
run.setResult(Result.ABORTED);
run.save();

LOGGER.log(Level.FINE, () -> "Stopping build: '" + run.getId() + "'");

if (run instanceof WorkflowRun) {
throw new FlowInterruptedException(Result.NOT_BUILT, true, new CauseOfInterruption() {
@Override
public String getShortDescription() {
return "Skipped because of SCM message";
}
});
if(doStopWorkflowRun) {
LOGGER.log(Level.FINE, () -> "Forcing doStop on: '" + run.getId() + "'");
WorkflowRun build = (WorkflowRun) run;
build.doStop();
}
else {
throw new FlowInterruptedException(Result.NOT_BUILT, true, new CauseOfInterruption() {
@Override
public String getShortDescription() {
return "Skipped because of SCM message";
}
});
}
} else if (run instanceof AbstractBuild) {
AbstractBuild<?, ?> build = (AbstractBuild<?, ?>) run;
build.doStop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public void testConfigRoundtrip() throws Exception {

BuildWrapperDescriptor descriptor = new SCMSkipBuildWrapper.DescriptorImpl();

project.getBuildWrappersList().add(new SCMSkipBuildWrapper(false, null));
project.getBuildWrappersList().add(new SCMSkipBuildWrapper(false, null, false));

project = jenkins.configRoundtrip(project);

jenkins.assertEqualDataBoundBeans(
new SCMSkipBuildWrapper(false, null),
new SCMSkipBuildWrapper(false, null, false),
project.getBuildWrappersList().get(0));
}

Expand Down Expand Up @@ -85,8 +85,12 @@ private WorkflowJob preparePipelineJob(URL pipelineFile, String... commitMessage
}

private FreeStyleProject createFreestyleProject(boolean delete) throws IOException {
return createFreestyleProject(delete, false);
}

private FreeStyleProject createFreestyleProject(boolean delete, boolean doForceDelete) throws IOException {
FreeStyleProject project = jenkins.createFreeStyleProject();
SCMSkipBuildWrapper builder = new SCMSkipBuildWrapper(delete, null);
SCMSkipBuildWrapper builder = new SCMSkipBuildWrapper(delete, null, doForceDelete);
project.getBuildWrappersList().add(builder);
FakeChangeLogSCM fakeScm = new FakeChangeLogSCM();
fakeScm.addChange().withMsg("Some change [ci skip] in code.");
Expand Down Expand Up @@ -149,6 +153,27 @@ public void testScriptedPipelineMultilineDelete() throws Exception {
assertEquals(List.of(), job.getBuilds());
}

@Test
public void testScriptedPipelineMultilineDoStopWorkflowRun() throws Exception {
String agentLabel = "test-agent";
jenkins.createOnlineSlave(Label.get(agentLabel));
WorkflowJob job = preparePipelineJob(
getClass().getClassLoader().getResource("testDoStopWorkflowRun.Jenkinsfile"),
"Some change [skip ci] in code.\n Additional line.");

QueueTaskFuture<WorkflowRun> future = job.scheduleBuild2(0);
Assert.assertNotNull(future);

WorkflowRun completedBuild = jenkins.assertBuildStatus(Result.ABORTED, future);
Assert.assertEquals(completedBuild.getDescription(), "SCM Skip - build skipped");

String expectedString =
"SCM Skip: Pattern .*\\[(ci skip|skip ci)\\].* matched on message: " + "Some change [skip ci] in code.";
jenkins.assertLogContains(expectedString, completedBuild);
jenkins.assertLogContains("before skip", completedBuild);
jenkins.assertLogNotContains("after skip", completedBuild);
}

@Test
public void testPipelineUserIdCause() throws Exception {
String agentLabel = "test-agent";
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/testDoStopWorkflowRun.Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pipeline {
agent any

stages {
stage('Build') {
steps {
echo 'before skip'
scmSkip(skipPattern: '.*\\[(ci skip|skip ci)\\].*', doStopWorkflowRun: true)
echo 'after skip'
}
}
}
}