1
def jobs = [
    [
        "name" : "LegacyUnitTests",
        "runFolder" : "recipes",
        "run"   : { -> sh "docker-compose -f docker-compose/docker-compose-maven.yml -f docker-compose/docker-compose.yml run maven-unit-test" },
        "report": { -> junit testResults: "docker-compose/volmnt/reports/*.xml" },
    ],
    [
        "name" : "AutomationTests",
        "runFolder" : "automation",
        "run"   : { -> sh "../run_automation_tests.sh" },
        "report": { ->
            junit testResults: "reports/junit/*.xml"
            allure([
                includeProperties: false,
                jdk: '',
                properties: [],
                reportBuildPolicy: 'ALWAYS',
                results: [[path: '/reports/allure']]
            ])
        }
    ]
]

def parallelStagesMap = jobs.collectEntries {
    ["${it.name}" : generateStage(it)]
}

def generateStage(job) {
    return {
        stage(job.name) {
            dir(job.runFolder) {
                try {
                    job.run.call()
                    setSuccess(this, job.name)
                }
                catch (err) {
                    setFailed(this, job.name)
                }
            }
       }
   }
}

pipeline {
    agent {
        node {
            label 'pr-slave-test'
            customWorkspace "/var/lib/jenkins/workspace/${JOB_NAME}_${BUILD_NUMBER}"
        }
    }
    post {
        always {
            //Reports
            script {
                jobs.each {
                    j -> j.report.call()
                }
            }//Cleanup
            cleanWs()
        }
    }
    stages {
        stage('Pre-Build') {
            steps {
                 script {
                    //set status as pending
                    jobs.each {
                        j -> setPending(this, j.name)
                    }
                    //checkout all repo   
                 }
            }
        }
        stage('Build') {
            steps {
                dir () {
                    sh "echo \"Building\""
                }
            }
        }
        stage('CI') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }
    }
}

I have a declarative pipeline like this and we are running the jobs defined in the jobs map in parallel. I have a requirement now to split and run our unit tests in parallel and i wish to implement that without adding another entry in jobs map instead i like to add another "run" in the LegacyUnitTests entry itself and run the "run" codes in parallel. Can someone help me how to achieve this?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.