Skip to content
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

[JENKINS-49073] Propagate the downstream result, not just FAILURE #24

Merged
merged 2 commits into from
Jul 12, 2019
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.33</version>
<version>3.43</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import jenkins.util.Timer;
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
import org.jenkinsci.plugins.workflow.steps.StepContext;

@Extension
Expand Down Expand Up @@ -49,7 +50,8 @@ public void onCompleted(Run<?,?> run, @Nonnull TaskListener listener) {
trigger.context.onFailure(trigger.interruption);
}
} else {
trigger.context.onFailure(new AbortException(run.getFullDisplayName() + " completed with status " + run.getResult() + " (propagate: false to ignore)"));
Result result = run.getResult();
trigger.context.onFailure(new FlowInterruptedException(result != null ? result : /* probably impossible */ Result.FAILURE, new DownstreamFailureCause(run)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One side effect of using FlowInterruptedException instead of AbortException is that retry will no longer retry the build step if it fails (it ignores FlowInterruptedException because of JENKINS-44379 ). This was reported as a bug in JENKINS-60354.

I'm not really sure how to fix this. I think FlowInterruptedException is overloaded with too many meanings at this point. One approach would be to factor out a ResultCarryingException interface, have FlowInterruptedException and some new FooException implement that interface, and then go through and switch everything that currently just uses FlowInterruptedException for a result to use ResultCarryingException when inspecting exception and FooException when creating them, and only use FlowInterruptedException when we only want to capture actual Pipeline interruption.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might rather suggest amending RetryStep to check for the presence of a CauseOfInterruption.

Copy link
Member

@dwnusbaum dwnusbaum Dec 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might rather suggest amending RetryStep to check for the presence of a CauseOfInterruption.

Good idea, and much simpler to implement, although I do feel like it would be good to separate the different use cases of FlowInterruptedException at some point so that it is easier to reason about changes like this in isolation.

I think catchError and warnError probably have the same issue as retry and would also need to be updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it looks like WorkflowRun.doTerm does not set a CauseOfInterruption when it aborts the build, so that would need to be tested and maybe changed since it should be rethrown by retry. WorkflowRun.doKill does not set a CauseOfInterruption either, but I don't think it matters in that case because of the way the build is aborted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps just add a fatal flag to FlowInterruptedException, used by true build interruptions, and have steps like retry only ignore it in this mode. I guess that boils down to a similar proposal.

}
}
run.getActions().removeAll(run.getActions(BuildTriggerAction.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* The MIT License
*
* Copyright 2019 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.support.steps.build;

import hudson.model.Run;
import javax.annotation.CheckForNull;
import jenkins.model.CauseOfInterruption;

/**
* Indicates that an upstream build failed because of a downstream build’s status.
*/
public final class DownstreamFailureCause extends CauseOfInterruption {

private static final long serialVersionUID = 1;

private final String id;

DownstreamFailureCause(Run<?, ?> downstream) {
id = downstream.getExternalizableId();
}

public @CheckForNull Run<?, ?> getDownstreamBuild() {
return Run.fromExternalizableId(id);
}

@Override public String getShortDescription() {
Run<?, ?> downstream = getDownstreamBuild();
if (downstream != null) {
return downstream.getFullDisplayName() + " completed with status " + downstream.getResult() + " (propagate: false to ignore)";
} else {
return "Downstream build was not stable (propagate: false to ignore)";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License

Copyright 2019 CloudBees, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core">
<j:set var="ds" value="${it.downstreamBuild}"/>
<j:choose>
<j:when test="${ds != null}">
<a class="model-link inside" href="${rootURL}/${ds.url}">${ds.fullDisplayName}</a> was not stable
</j:when>
<j:otherwise>
<j:out value="${it.shortDescription}"/>
</j:otherwise>
</j:choose>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.BooleanParameterDefinition;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Computer;
import hudson.model.Executor;
Expand Down Expand Up @@ -62,6 +65,7 @@
import org.jvnet.hudson.test.MockFolder;
import org.jvnet.hudson.test.MockQueueItemAuthenticator;
import org.jvnet.hudson.test.SleepBuilder;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.recipes.LocalData;

Expand Down Expand Up @@ -178,7 +182,21 @@ public void abortBuild() throws Exception {
fb.getExecutor().interrupt();

j.assertBuildStatus(Result.ABORTED, j.waitForCompletion(fb));
j.assertBuildStatus(Result.FAILURE,q.get());
j.assertBuildStatus(Result.ABORTED, q.get());
}

@Issue("JENKINS-49073")
@Test public void downstreamUnstable() throws Exception {
FreeStyleProject ds = j.createFreeStyleProject("ds");
ds.getBuildersList().add(new TestBuilder() {
@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.setResult(Result.UNSTABLE);
return true;
}
});
WorkflowJob us = j.createProject(WorkflowJob.class, "us");
us.setDefinition(new CpsFlowDefinition("build 'ds'", true));
j.assertBuildStatus(Result.UNSTABLE, us.scheduleBuild2(0));
}

@Test
Expand Down