Jenkins kubernetes-plugin Parallelise declarative Jenkinsfile

4/29/2019

Using kubernetes-plugin Parallelise declarative Jenkinsfile how do you run stages in parallel, when each stage uses the same container? (eg: sonar_qube analysis and unit testing both run on a maven container.

I have tried the following in Jenkinsfile:

def label = "my-build-${UUID.randomUUID().toString()}"

podTemplate(label: label, containers: [
  containerTemplate(name: 'maven', image: 'maven:3.5.3-jdk-8-alpine', command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'maven2', image: 'maven:3.5.3-jdk-8-alpine', command: 'cat', ttyEnabled: true),
],
volumes: [
  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
  hostPathVolume(mountPath: '/root/.m2/repository', hostPath: '/root/.m2'),
  hostPathVolume(mountPath: '/tmp', hostPath: '/tmp')
]) {

node(label) {
    def myRepo = checkout scm
    def gitCommit = myRepo.GIT_COMMIT
    def gitBranch = myRepo.GIT_BRANCH

    def didFail = false
    def throwable = null

    try {        
        stage('Tests In Parallel') {
            parallel StaticCodeAnalysis: {
                container('maven'){
                    withSonarQubeEnv('sonarqube-cluster') {
                      // requires SonarQube Scanner for Maven 3.2+
                      sh " mvn help:evaluate -Dexpression=settings.localRepository"

                      sh "mvn clean"
                      sh "mvn compiler:help jar:help resources:help surefire:help clean:help install:help deploy:help site:help dependency:help javadoc:help spring-boot:help org.jacoco:jacoco-maven-plugin:prepare-agent"
                      sh "mvn org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_AUTH_TOKEN -Dsonar.exclusions=srcgen/**/* -Dmaven.test.skip=true"
                    }
                }
            }, unitTests: {
                container('maven2') {
                  sh "mvn clean package"

                  junit allowEmptyResults: true, testResults: '**/surefire-reports/*.xml'
                }
            },
            failFast: true
        }       
    } catch (e) {
        didFail = true
        throwable = e
    } finally {
        sh "rm -rf /tmp/${label}"
    }
    if (didFail) {
        echo "Something went wrong."
        error throwable
    }        

  }
}

All seems to work, and on the blue ocean UI I can see the two stages running correctly at the same time. However when one of the stages in parralell stage finishes, the other fails with 'java.lang.NoClassDefFoundError's' for classes that have definitely already been used and where there as stage was running.

It almost looks like all slaves that a spun up use the same workspace directoy ie: /home/jenkins/workspace/job_name/

The maven commands create folder - /home/jenkins/workspace/job_name/target/classes - but when you see the failing, its ask if the container that was been used lots all its content from the classes folder.

-- Melissa
jenkins
jenkins-kubernetes
kubernetes-jenkins-plugin
parallel-processing

0 Answers