DKay
DKay

Reputation: 53

How to use allure with Jenkins parallel pipelines?

How to use Allure with Jenkins parallel pipelines?

I have a Jenkins pipeline to run parallel tests:

def testName="ExampleTest"
pipeline {
    agent any
    stages {
        stage ('Checkout test') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/devel']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab', url: 'git@git.***']]])
            }
        }
        stage ('Test Template') {
            parallel {
                stage ('testTemplate1') {
                    steps {
                        runTestByName (testName,STAGE_NAME)
                    }
                }
                stage ('testTemplate2') {
                    steps {
                        runTestByName (testName,STAGE_NAME)
                    }
                }
            }
        }
    }
}

void runTestByName (testName,testTemplate) {
    stage (testName + ':' + testTemplate) {
        withEnv(["JAVA_HOME=${env.JAVA_HOME}", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]) {
            withCredentials([usernamePassword(credentialsId: 'credentials', passwordVariable: 'PASSWORD', usernameVariable: 'LOGIN')]) {
                withMaven(jdk: '', maven: 'maven-default', mavenSettingsFilePath: '/var/jenkins_home/secrets/settings.xml') {
            sh "mvn -X -Dtest="+testName+" -Dtemplate="+testTemplate+" test "
                }
            }
        }
    }
    stage ('Reporting:' + testName + ':' + testTemplate) {
        allure includeProperties: false, jdk: '', results: [[path: 'target/allure-results']]
    }
}

Tests are executed in correct way, reports are generated, but all reports are the same (in other words I get 2 reports for the test with parameter 'testTemplate2', I expect report for tests with 'testTemplate1' and report for tests with 'testTemplate2').

Update:

I added property allure.results.directory to maven:

sh "mvn -X -Dtest="+testName+" -Dtemplate="+testTemplate+" -Dallure.results.directory=target/allure-results/${testTemplate} test "

I also changed allure configuration:

allure ([
                includeProperties: false,
                jdk: '',
                results: [[path: "target/allure-results/${testTemplate}"]],
                report: "allure-report/${testTemplate}"
            ])

I see that both reports are successfully generated (from console log):

[test-parallel@2] $ /var/jenkins_home/tools/ru.yandex.qatools.allure.jenkins.tools.AllureCommandlineInstallation/allure-default/bin/allure generate /var/jenkins_home/workspace/test-parallel@2/target/allure-results/testTemplate1 -c -o /var/jenkins_home/workspace/test-parallel@2/allure-report/testTemplate1
Report successfully generated to /var/jenkins_home/workspace/test-parallel@2/allure-report/testTemplate1
Allure report was successfully generated.
Creating artifact for the build.
Artifact was added to the build.

[test-parallel@2] $ /var/jenkins_home/tools/ru.yandex.qatools.allure.jenkins.tools.AllureCommandlineInstallation/allure-default/bin/allure generate /var/jenkins_home/workspace/test-parallel@2/target/allure-results/testTemplate2 -c -o /var/jenkins_home/workspace/test-parallel@2/allure-report/testTemplate2
Report successfully generated to /var/jenkins_home/workspace/test-parallel@2/allure-report/testTemplate2
Allure report was successfully generated.
Creating artifact for the build.
Artifact was added to the build.

But when I get 404 error when try to open reports from Jenkins.

Is there any way to resolve this problem?

Upvotes: 2

Views: 3760

Answers (1)

DKay
DKay

Reputation: 53

@yarafed So here is my workaround, as I mentioned before in comments, I've created DSL job which spawns other jobs in parallel. I had to delete personal data and some other implementation features, but here it is:

def testName='Test'
def tags = ['tag1', 'tag2', 'tag3']
def threads = 4
def parallelStagesMap = tags.collectEntries {
    ["${it}" : generateStage(it,testName,threads)]
}
def generateStage(tag, testName,threads) {
    return {
        stage("stage: ${tag}") {
            runTestByName (testName,tag,threads)
        }
    }
}
void runTestByName (testName, tag, threads) {
    def jobName=testName.concat('-').concat(tag).replace(':','-')
    jobDsl scriptText: '''
    freeStyleJob("''' + jobName + '''"){
        scm {
            git {
                branch('devel')
                remote {
                    name('remote')
                    url('<git link here>')
                    credentials ('<credentials id here>')
                }
            }
        }
        wrappers {
            credentialsBinding {
                usernamePassword('LOGIN','PASSWORD','credentials')
            }
        }
        steps {
            maven {
                goals ('clean')
                goals ('test')
                mavenInstallation('maven-default')
                injectBuildVariables(true)
                property('test',"''' + testName + '''")
                property('parallel','methods')
                property('threadcount',"''' + threads + '''")
                property('tag',"''' + tag + '''")
                property('user','$USER')
                property('password','$PASSWORD')
            }
        }
        publishers { 
            allure (['target/allure-results']) {}
        }
    }
    '''
    build job:jobName
}
pipeline {
    agent any
    stages {
        stage ('Test') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }
    }
}

Upvotes: 2

Related Questions