Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-49762

withMaven fails to detect maven executable in kubernetes container

XMLWordPrintable

      Hi,

      I am using withMaven() to execute maven with a custom settings.xml in a kubernetes pod. The detection of the maven executable is broken at the moment, since

      org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution.readFromProcess("/bin/sh", "-c", "which mvn");
      

      returns two lines of text:

      /usr/bin/mvn
      EXITCODE 0
      

      resulting in [withMaven] using Maven installation provided by the build agent with executable /usr/bin/mvn EXITCODE 0

      The exit code part seems to be generated by:

      org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator.doExec()
      

      I am not sure, why the last line is included, since when I execute /bin/sh -c 'which mvn' in the pipeline, I get only /usr/bin/mvn in return.

      Minimal pipeline example to reproduce:

      def label = "mypod-${UUID.randomUUID().toString()}"
      podTemplate(label: label, containers: [
          containerTemplate(name: 'maven', image: 'maven:3.5.2-jdk-8', ttyEnabled: true, command: 'cat')
        ]) {
        node(label) {
          stage('Build') {
            container('maven') {
              withMaven(mavenSettingsConfig: 'maven-mirror-settings') {
                sh "ls -la ${MVN_CMD_DIR}"
                sh "cat ${MVN_CMD_DIR}/mvn*"
                sh "/bin/sh -c 'which mvn'"
                sh "mvn -v"
              }
            }
          }
        }
      } 
      

      Logs:

      Running in Durability level: MAX_SURVIVABILITY
      [Pipeline] podTemplate
      [Pipeline] {
      [Pipeline] node
      Still waiting to schedule task
      jenkins-slave-rg789-w09w8 is offline
      Running on jenkins-slave-rg789-w09w8 in /home/jenkins/workspace/Microservice
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (Build)
      [Pipeline] container
      [Pipeline] {
      [Pipeline] withMaven
      [withMaven] Options: []
      [withMaven] Available options: 
      [withMaven] using JDK installation provided by the build agent
      [withMaven] using Maven settings provided by the Jenkins Managed Configuration File 'maven-mirror-settings' 
      [withMaven] using Maven settings.xml 'maven-mirror-settings' with NO Maven servers credentials provided by Jenkins
      [withMaven] using Maven global settings provided by the Jenkins global configuration. Maven global settings defined by 'DefaultSettingsProvider', NOT overriding it.
      Executing shell script inside container [maven] of pod [jenkins-slave-rg789-w09w8]
      Executing command: "/bin/sh" "-c" "which mvn" 
      printf "EXITCODE %3d" $?; exit
      /usr/bin/mvn
      EXITCODE   0[withMaven] using Maven installation provided by the build agent with executable /usr/bin/mvn EXITCODE   0
      [Pipeline] {
      [Pipeline] sh
      [Microservice] Running shell script
      + ls -la /home/jenkins/workspace/Microservice@tmp/withMavenef1a673d
      total 60
      drwxr-xr-x 2 10000 10000  4096 Feb 27 10:17 .
      drwxr-xr-x 4 10000 10000  4096 Feb 27 10:17 ..
      -rwxr-xr-x 1 10000 10000   202 Feb 27 10:17 mvn EXITCODE   0
      -rw-r--r-- 1 10000 10000 44289 Feb 27 10:17 pipeline-maven-spy.jar
      -rw-r--r-- 1 10000 10000  3432 Feb 27 10:17 settings.xml
      [Pipeline] sh
      [Microservice] Running shell script
      + cat /home/jenkins/workspace/Microservice@tmp/withMavenef1a673d/mvn EXITCODE   0
      #!/bin/sh -e
      echo ----- withMaven Wrapper script -----
      "/usr/bin/mvn EXITCODE   0" --batch-mode --show-version --settings "/home/jenkins/workspace/Microservice@tmp/withMavenef1a673d/settings.xml"  "$@"
      [Pipeline] sh
      [Microservice] Running shell script
      + /bin/sh -c which mvn
      /usr/bin/mvn
      [Pipeline] sh
      [Microservice] Running shell script
      + mvn -v
      Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T07:58:13Z)
      Maven home: /usr/share/maven
      Java version: 1.8.0_151, vendor: Oracle Corporation
      Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
      Default locale: en, platform encoding: UTF-8
      OS name: "linux", version: "4.4.0-21-generic", arch: "amd64", family: "unix"
      [Pipeline] }
      [Pipeline] // withMaven
      [Pipeline] }
      [Pipeline] // container
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] }
      [Pipeline] // podTemplate
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

      Some snippets from the jenkins log file:

      When running /bin/sh -c 'which mvn' from WithMavenStepExecution.readFromProcess:

      Feb 27, 2018 10:31:37 AM FINEST org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator
      Executing command: "/bin/sh" "-c" "which mvn" 
      printf "EXITCODE %3d" $?; exit
      

      When running /bin/sh -c 'which mvn' from the pipeline:

      Feb 27, 2018 10:31:38 AM FINEST org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator
      Executing command: "nohup" "sh" "-c" "{ while [ -d '/home/jenkins/workspace/Microservice@tmp/durable-e808e5c0' -a \! -f '/home/jenkins/workspace/Microservice@tmp/durable-e808e5c0/jenkins-result.txt' ]; do touch '/home/jenkins/workspace/Microservice@tmp/durable-e808e5c0/jenkins-log.txt'; sleep 3; done } & jsc=durable-370fa533571e05498bc0456bb0ba11e9; JENKINS_SERVER_COOKIE=\$jsc '/home/jenkins/workspace/Microservice@tmp/durable-e808e5c0/script.sh' > '/home/jenkins/workspace/Microservice@tmp/durable-e808e5c0/jenkins-log.txt' 2>&1; echo \$? > '/home/jenkins/workspace/Microservice@tmp/durable-e808e5c0/jenkins-result.txt'; wait" 
      printf "EXITCODE %3d" $?; exit
      

            iocanel Ioannis Canellos
            betermieux Stefan Betermieux
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: