Jenkins: Allure reports plugin в Scripted pipeline

Автор: | 26/09/2018
 

Allure – фреймворк для создания репортов о результатах выполнения тестов.

Для Jenkins имеется плагин allure-jenkins-plugin, который сегодня и будем подключать в Scripted Pipeline Jenkins-а. Документация тут>>> и тут>>>.

Устанавливаем плагин:

Переходим в http://jenkinsurl/configureTools/, настраиваем Allure Commandline:

Переходим в http://jenkinsurl/pipeline-syntax/, и генерируем скрипт:

В результате весь скрипт наших автотестов выглядит сейчас так:

#!/usr/bin/env groovy

node {

    stage('Run tests') {

        dir ('ciscripts') {
            git branch: "${DEPLOYMENT_REPO_BRANCH}", url: "${DEPLOYMENT_REPO_URL}", credentialsId: "jenkins-github"
        }
    
        def tests = load 'ciscripts/api_tests/main.groovy'
    
        try {
            
            tests.notifySlack()
    
            tests.runTests("${XML_CONFIG}")
            
            
        } catch (e) {
            currentBuild.result = 'FAILURE'
            throw e
        } finally {
            
            tests.notifySlack(currentBuild.result)
        
            stage('Reports') {
                allure([
                    includeProperties: false,
                    jdk: '',
                    properties: [],
                    reportBuildPolicy: 'ALWAYS',
                    results: [[path: 'target/allure-results']]
                ])
            }
        }
    }
}

stage('Reports') вынесен в try/catch и выполняется в finally, что бы сгенерировать репорты в любом случае, даже если stage('Run tests') завершится с ошибкой.

Скрипт ciscripts/api_tests/main.groovy: (настройка уведомлений в Slack есть в посте Jenkins: уведомление в Slack из Jenkins Scripted Pipeline):

#!/usr/bin/env groovy

def notifySlack(String buildStatus = 'STARTED') {
    
    // Build status of null means success.
    buildStatus = buildStatus ?: 'SUCCESS'

    def color

    if (buildStatus == 'STARTED') {
        color = '#D4DADF'
    } else if (buildStatus == 'SUCCESS') {
        color = '#BDFFC3'
    } else if (buildStatus == 'UNSTABLE') {
        color = '#FFFE89'
    } else {
        color = '#FF9FA1'
    }

    def msg = "${buildStatus}: `${env.JOB_NAME}` #${env.BUILD_NUMBER}:\n${env.BUILD_URL}allure/"

    slackSend(color: color, message: msg)
}

def runTests(xml_config='1') {

    docker.image('maven').inside('-v /var/run/docker.sock:/var/run/docker.sock -v /home/admin/.m2:/root/.m2') {

    git branch: "${APITESTS_REPO_BRANCH}", url: "${APITESTS_REPO_URL}", credentialsId: "jenkins-github"

        sh "mvn test -Dxml.file=${xml_config}"
    }
}

return this

Запускаем билд:

И результаты:

 

Готово.