Skip to content

Commit b57d3dd

Browse files
cronikbguerin
andauthored
Revert to 1376.v18876d10ce9c detectWithContainer logic (#788)
* Revert to 1376.v18876d10ce9c detectWithContainer logic Restores with container detection handling back to 1376.v18876d10ce9c. This version contained special handling for kubernetes-plugin as well as naming convention for other plugins to trigger special with container handling. * Add tests for detectWithContainer * source format --------- Co-authored-by: Benoit GUERIN <[email protected]>
1 parent c11858d commit b57d3dd

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
112112
import org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution;
113113
import org.jenkinsci.plugins.workflow.steps.StepContext;
114+
import org.kohsuke.accmod.Restricted;
115+
import org.kohsuke.accmod.restrictions.NoExternalUse;
114116
import org.springframework.util.ClassUtils;
115117

116118
@SuppressFBWarnings(
@@ -257,23 +259,56 @@ private boolean detectWithContainer() throws IOException {
257259
Launcher launcher1 = launcher;
258260
while (launcher1 instanceof Launcher.DecoratedLauncher) {
259261
String launcherClassName = launcher1.getClass().getName();
260-
// ToDo: add support for container() step (ContainerExecDecorator) from Kubernetes plugin
261-
if (launcherClassName.contains("WithContainerStep")) {
262-
LOGGER.log(Level.FINE, "Step running within docker.image(): {0}", launcherClassName);
262+
Optional<Boolean> withContainer = isWithContainerLauncher(launcherClassName);
263+
if (withContainer.isPresent()) {
264+
boolean result = withContainer.get();
265+
if (result) {
266+
console.trace(
267+
"[withMaven] IMPORTANT \"withMaven(){...}\" step running within a Docker container. See ");
268+
console.traceHyperlink(
269+
"https://github.com/jenkinsci/pipeline-maven-plugin/blob/master/FAQ.adoc#how-to-use-the-pipeline-maven-plugin-with-docker",
270+
"Pipeline Maven Plugin FAQ");
271+
console.trace(" in case of problem.");
272+
}
263273

264-
console.trace(
265-
"[withMaven] IMPORTANT \"withMaven(){...}\" step running within a Docker container. See ");
266-
console.traceHyperlink(
267-
"https://github.com/jenkinsci/pipeline-maven-plugin/blob/master/FAQ.adoc#how-to-use-the-pipeline-maven-plugin-with-docker",
268-
"Pipeline Maven Plugin FAQ");
269-
console.trace(" in case of problem.");
270-
return true;
274+
return result;
271275
}
276+
272277
launcher1 = ((Launcher.DecoratedLauncher) launcher1).getInner();
273278
}
274279
return false;
275280
}
276281

282+
/**
283+
* Check if the launcher class name is a known container launcher.
284+
* @param launcherClassName launcher class name
285+
* @return empty if unknown and should keep checking, true if it is a container launcher, false if it is not.
286+
*/
287+
@Restricted(NoExternalUse.class)
288+
protected static Optional<Boolean> isWithContainerLauncher(String launcherClassName) {
289+
// kubernetes-plugin container step execution does not require special container handling
290+
if (launcherClassName.contains("org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator")) {
291+
LOGGER.log(Level.FINE, "Step running within Kubernetes withContainer(): {1}", launcherClassName);
292+
return Optional.of(false);
293+
}
294+
295+
// for plugins that require special container handling should include this name launcher naming convention
296+
// since there is no common interface to detect if a step is running within a container
297+
if (launcherClassName.contains("ContainerExecDecorator")) {
298+
LOGGER.log(Level.FINE, "Step running within container exec decorator: {0}", launcherClassName);
299+
return Optional.of(true);
300+
}
301+
302+
// detect docker.image().inside {} or withDockerContainer step from docker-workflow-plugin, which has the
303+
// launcher name org.jenkinsci.plugins.docker.workflow.WithContainerStep.Decorator
304+
if (launcherClassName.contains("WithContainerStep")) {
305+
LOGGER.log(Level.FINE, "Step running within docker.image(): {0}", launcherClassName);
306+
return Optional.of(true);
307+
}
308+
309+
return Optional.empty();
310+
}
311+
277312
/**
278313
* Setup the selected JDK. If none is provided nothing is done.
279314
*/

pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2Test.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,35 @@ public void testEscapeWindowsBatchChars() {
3131

3232
assertThat(actualEscapedMavenConfig).isEqualTo(expectedEscapedMavenConfig);
3333
}
34+
35+
@Test
36+
public void testIsWithContainerLauncherUnknown() {
37+
assertThat(WithMavenStepExecution2.isWithContainerLauncher("hudson.SomeUnknownLauncher"))
38+
.isEmpty();
39+
}
40+
41+
@Test
42+
public void testIsWithContainerLauncherWithContainerStep() {
43+
assertThat(WithMavenStepExecution2.isWithContainerLauncher(
44+
"org.jenkinsci.plugins.docker.workflow.WithContainerStep.Decorator"))
45+
.contains(true);
46+
}
47+
48+
@Test
49+
public void testIsWithContainerLauncherKubernetesPluginContainerExecDecorator() {
50+
assertThat(WithMavenStepExecution2.isWithContainerLauncher(
51+
"org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator"))
52+
.contains(false);
53+
}
54+
55+
@Test
56+
public void testIsWithContainerLauncherCustomContainerExecDecorator() {
57+
// for plugins that supporting launching their own containerized steps
58+
assertThat(WithMavenStepExecution2.isWithContainerLauncher("com.jenkins.plugins.custom.ContainerExecDecorator"))
59+
.contains(true);
60+
assertThat(
61+
WithMavenStepExecution2.isWithContainerLauncher(
62+
"com.jenkins.plugins.kubernetes.pipeline.MyContainerExecDecorator.MyContainerDecoratedLauncher"))
63+
.contains(true);
64+
}
3465
}

0 commit comments

Comments
 (0)