Jenkins Pipeline

Jenkinsfile

Many service-company clients use Jenkins. A declarative pipeline in the repository (Jenkinsfile) checks out the code, runs Maven with parameters, publishes reports and emails the team.

pipeline {
    agent { label 'selenium-linux' }

    parameters {
        choice(name: 'ENV', choices: ['qa', 'staging'], description: 'Environment')
        choice(name: 'SUITE', choices: ['testng-smoke.xml', 'testng-regression.xml'], description: 'Suite')
        choice(name: 'BROWSER', choices: ['chrome', 'firefox', 'edge'], description: 'Browser')
    }

    tools { jdk 'jdk21'; maven 'maven3' }

    stages {
        stage('Checkout') {
            steps { checkout scm }
        }
        stage('Test') {
            steps {
                sh "mvn -B clean test -Denv=${params.ENV} -DsuiteXml=${params.SUITE} -Dbrowser=${params.BROWSER} -Dheadless=true"
            }
        }
    }

    post {
        always {
            junit 'target/surefire-reports/*.xml'
            publishHTML(target: [reportDir: 'target/extent', reportFiles: 'ShopEaseReport.html',
                                 reportName: 'Extent Report', keepAll: true, alwaysLinkToLastBuild: true])
            archiveArtifacts artifacts: 'target/screenshots/*.png', allowEmptyArchive: true
        }
        failure {
            mail to: '[email protected]',
                 subject: "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
                 body: "See ${env.BUILD_URL}"
        }
    }
}

Console Output

Example

Jenkins console (build #412)

Example output

[Pipeline] stage (Test)
+ mvn -B clean test -Denv=qa -DsuiteXml=testng-regression.xml -Dbrowser=chrome -Dheadless=true
[INFO] Tests run: 214, Failures: 2, Errors: 0, Skipped: 3
[ERROR] BUILD FAILURE
[Pipeline] junit
Recording test results
[Checks API] No suitable checks publisher found.
[Pipeline] publishHTML
[htmlpublisher] Archiving HTML reports...
[Pipeline] mail
Finished: UNSTABLE

Note: With the junit step, Jenkins shows test trends and marks builds with failing tests as UNSTABLE (yellow), separate from compile errors (red).