From e40d47e21a2b729de3473b6fd0981014f0d8439c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jackiewicz?= Date: Wed, 6 Mar 2024 08:09:44 +0100 Subject: [PATCH] Fix detection if withMaven is executed within container() (#766) * Fix logging in detectWithContainer method * Fix detection of builds run inside 'container' block * Remove duplicated condition * Keep changes made within JENKINS-53831 * Simplify launcherClassName checking of ContainerExecDecorator * Fix logging info --- .../maven/WithMavenStepExecution2.java | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java b/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java index c79a61e7..e4ed9003 100644 --- a/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java +++ b/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java @@ -197,14 +197,6 @@ protected boolean doStart() throws Exception { withContainer = detectWithContainer(); - if (withContainer) { - console.trace("[withMaven] IMPORTANT \"withMaven(){...}\" step running within a Docker container. See "); - console.traceHyperlink( - "https://github.com/jenkinsci/pipeline-maven-plugin/blob/master/FAQ.adoc#how-to-use-the-pipeline-maven-plugin-with-docker", - "Pipeline Maven Plugin FAQ"); - console.trace(" in case of problem."); - } - setupJDK(); // list of credentials injected by withMaven. They will be tracked and masked in the logs @@ -265,19 +257,23 @@ protected boolean doStart() throws Exception { * "https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerStep.java"> * ContainerStep */ - private boolean detectWithContainer() { + private boolean detectWithContainer() throws IOException { Launcher launcher1 = launcher; while (launcher1 instanceof Launcher.DecoratedLauncher) { String launcherClassName = launcher1.getClass().getName(); - if (launcherClassName.contains("org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator")) { - LOGGER.log(Level.FINE, "Step running within Kubernetes withContainer(): {1}", launcherClassName); - return false; + if (launcherClassName.contains("ContainerExecDecorator")) { + LOGGER.log(Level.FINE, "Step running within Kubernetes container(): {0}", launcherClassName); + return true; } if (launcherClassName.contains("WithContainerStep")) { - LOGGER.log(Level.FINE, "Step running within docker.image(): {1}", launcherClassName); - return true; - } else if (launcherClassName.contains("ContainerExecDecorator")) { - LOGGER.log(Level.FINE, "Step running within docker.image(): {1}", launcherClassName); + LOGGER.log(Level.FINE, "Step running within docker.image(): {0}", launcherClassName); + + console.trace( + "[withMaven] IMPORTANT \"withMaven(){...}\" step running within a Docker container. See "); + console.traceHyperlink( + "https://github.com/jenkinsci/pipeline-maven-plugin/blob/master/FAQ.adoc#how-to-use-the-pipeline-maven-plugin-with-docker", + "Pipeline Maven Plugin FAQ"); + console.trace(" in case of problem."); return true; } launcher1 = ((Launcher.DecoratedLauncher) launcher1).getInner();