如何在Jenkins并行管道中使用诱惑力?

时间:2018-10-12 12:12:34

标签: jenkins allure

如何在Jenkins并行管道中使用Allure?

我有一个Jenkins管道来运行并行测试:

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']]
    }
}

以正确的方式执行测试,生成报告,但是所有报告都是相同的(换句话说,我得到2个带有参数'testTemplate2'的测试报告,我期望有'testTemplate1'的测试报告和测试的报告与“ testTemplate2”)。

更新:

我将属性allure.results.directory添加到了行家:

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

我还更改了吸引力配置:

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

我看到两个报告都已成功生成(从控制台日志中):

[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.

但是当我尝试从Jenkins打开报告时遇到404错误时。

有什么办法可以解决这个问题?

1 个答案:

答案 0 :(得分:1)

@yarafed 因此,这就是我的解决方法,正如我之前在评论中提到的那样,我创建了DSL作业,该作业并行生成其他作业。 我不得不删除个人数据和其他一些实现功能,但这是:

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
                }
            }
        }
    }
}