|
111 | 111 | import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
|
112 | 112 | import org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution;
|
113 | 113 | import org.jenkinsci.plugins.workflow.steps.StepContext;
|
| 114 | +import org.kohsuke.accmod.Restricted; |
| 115 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
114 | 116 | import org.springframework.util.ClassUtils;
|
115 | 117 |
|
116 | 118 | @SuppressFBWarnings(
|
@@ -257,23 +259,56 @@ private boolean detectWithContainer() throws IOException {
|
257 | 259 | Launcher launcher1 = launcher;
|
258 | 260 | while (launcher1 instanceof Launcher.DecoratedLauncher) {
|
259 | 261 | 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 | + } |
263 | 273 |
|
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; |
271 | 275 | }
|
| 276 | + |
272 | 277 | launcher1 = ((Launcher.DecoratedLauncher) launcher1).getInner();
|
273 | 278 | }
|
274 | 279 | return false;
|
275 | 280 | }
|
276 | 281 |
|
| 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 | + |
277 | 312 | /**
|
278 | 313 | * Setup the selected JDK. If none is provided nothing is done.
|
279 | 314 | */
|
|
0 commit comments