Last active
December 13, 2020 20:19
-
-
Save PramodKumarYadav/9dfdb093ea98428a46fd77443ff2d722 to your computer and use it in GitHub Desktop.
A sample Jenkins file to parse mvn results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String cron_string = BRANCH_NAME == "fix/pipeline-trigger" ? "@hourly" : "" | |
pipeline{ | |
agent any | |
environment { | |
HOST = 'myproject.develop' | |
REMOTE = 'false' | |
HEADLESS = 'true' | |
BROWSER = 'chrome' | |
TEST = 'TestAddressBook*' | |
} | |
triggers { cron(cron_string) } | |
stages{ | |
stage('Environment: List env variables'){ | |
steps{ | |
echo 'List all available env variables that can be used here.' | |
sh 'printenv' | |
} | |
} | |
stage('Fetch: From Bitbucket'){ | |
steps{ | |
echo "Fetch test project from bitbucket. Checkout current branch ${BRANCH_NAME}" | |
git branch: "${BRANCH_NAME}", url: "${GIT_URL}" | |
} | |
} | |
stage('Infra: Install browsers'){ | |
steps { | |
echo 'Install browsers needed for tests.' | |
dir('ci') { | |
sh 'chmod +x ./infra.sh' | |
sh "./infra.sh ${BROWSER}" | |
} | |
} | |
} | |
stage('Test: mvn tests'){ | |
steps{ | |
echo 'Run maven command which will execute the tests and rerun flaky tests (rerun parameter configured=2 in POM)' | |
sh "mvn -settings ./settings.xml clean -Dhost=${HOST} -Dremote=${REMOTE} -Dheadless=${HEADLESS} -Dbrowser=${BROWSER} -Dtest=${TEST} site" | |
} | |
} | |
stage('Result: mvn results'){ | |
steps{ | |
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') { | |
// Reason for not using junit plugin: https://issues.jenkins.io/browse/JENKINS-61263 | |
// Note: mvn gives more accurate test results and a possibility to identify the current status | |
echo 'Capture test results and report pass (green) or failed (yellow) - Also handles Flaky tests.' | |
echo 'STEP 01. Get test result from mvn run.' | |
sh 'chmod +x ./ci/test-result.sh' | |
sh './ci/test-result.sh ${BUILD_URL}consoleText' | |
echo 'STEP 02. Set stage status based on the test result.' | |
script{ | |
TEST_RESULT = sh ( | |
script: "cat ./ci/result-log.txt", | |
returnStdout: true | |
).trim() | |
echo "TEST_RESULT: ${TEST_RESULT}" | |
if (TEST_RESULT.contains('Failures: 0, Errors: 0')) { | |
echo "Tests successful! π" | |
} else { | |
echo "Tests failed! π£" | |
sh "exit 1" | |
} | |
} | |
} | |
} | |
} | |
stage('Report: html site reports'){ | |
steps{ | |
echo 'Publish test report on Jenkins.' | |
publishHTML target: [ | |
reportName : "Test report: Apollo", | |
reportDir: 'target/site', | |
reportFiles: 'surefire-report.html', | |
keepAll: true, | |
alwaysLinkToLastBuild: true, | |
allowMissing: false | |
] | |
} | |
} | |
stage('Archive: as Jenkins artifacts'){ | |
steps{ | |
echo 'Archive test reports on Jenkins.' | |
archiveArtifacts 'target/site/**, target/surefire-reports/**' | |
} | |
} | |
stage('Notify: On Slack/Email'){ | |
steps{ | |
echo 'Notify on Slack and/or email!' | |
} | |
} | |
} | |
post{ | |
always { | |
echo 'Get junit results and build duration' | |
script{ | |
summary = junit testResults: 'target/surefire-reports/TEST-*.xml' | |
CURRENT_BUILD_DURATION = "${currentBuild.durationString}".replace(' and counting', '') | |
// [trigger from timer]: + echo _class:hudson.triggers.TimerTrigger,shortDescription:Started by timer | |
// [trigger from user]: + echo _class:hudson.model.Cause,shortDescription:Started by user Pramod Kumar Yadav,userId:someIDhere,userName:Pramod Yadav | |
// [trigger from branch]: [{"_class":"jenkins.branch.BranchIndexingCause","shortDescription":"Branch indexing"}]}, | |
// Note: Below JSON is without colon but later we use colon. | |
BUILD_CAUSE_JSON = sh (script: "curl --silent ${BUILD_URL}/api/json | tr '{}' '\n' | grep 'shortDescription'", returnStdout: true).trim() | |
// Note: There can be more than one shortDescription (during branch indexing), but only one shortDescriptionwith":" | |
BUILD_USER_NAME = sh (script: "echo $BUILD_CAUSE_JSON | tr ',' '\n' | grep 'shortDescription:'", returnStdout: true).trim().substring(17) | |
echo "BUILD_USER_NAME: $BUILD_USER_NAME" | |
// If something unexpected happens, give a default value to this variable so that it can avoid pipeline failing because of this | |
if (!BUILD_USER_NAME?.trim()) { | |
BUILD_USER_NAME = 'apollo' | |
} | |
} | |
} | |
changed { | |
echo 'This Build was different then before...' | |
slackSend channel: "#apollo-prs-pipelines", | |
color: '#439FE0', | |
message: "\n *Apollo Build Different: * ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>) \n *Trigger*: ${BUILD_USER_NAME}, *Time took*: ${CURRENT_BUILD_DURATION} \n This was a different build than before. Check it out!" | |
} | |
success { | |
echo 'Pipeline succeeded! π' | |
slackSend channel: "#apollo-prs-pipelines", | |
color: 'good', | |
message: "\n *Apollo Build Successful: * ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>) \n *Trigger*: ${BUILD_USER_NAME}, *Time took*: ${CURRENT_BUILD_DURATION} \n *Summary (mvn):* ${TEST_RESULT} \n *Summary (junit):* Tests: ${summary.totalCount}, Passed: ${summary.passCount}, Failed: ${summary.failCount}, Skipped: ${summary.skipCount}" | |
} | |
unstable { | |
script{ | |
// Junit marks flaky tests as unstable. But if there are no failures/errors, we don't want to send a (yellow) warning notification in slack. | |
if (TEST_RESULT.contains('Failures: 0, Errors: 0')) { | |
echo 'Pipeline succeeded! π' | |
slackSend channel: "#apollo-prs-pipelines", | |
color: 'good', | |
message: "\n *Apollo Build Successful: * ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>) \n *Trigger*: ${BUILD_USER_NAME}, *Time took*: ${CURRENT_BUILD_DURATION} \n *Summary (mvn):* ${TEST_RESULT} \n *Summary (junit):* Tests: ${summary.totalCount}, Passed: ${summary.passCount}, Failed: ${summary.failCount}, Skipped: ${summary.skipCount}" | |
} else { | |
echo 'Pipeline unstable! π ' | |
slackSend channel: "#apollo-prs-pipelines", | |
color: 'warning', | |
message: "\n *Apollo Build Unstable: * ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>) \n *Trigger*: ${BUILD_USER_NAME}, *Time took*: ${CURRENT_BUILD_DURATION} \n *Summary (mvn):* ${TEST_RESULT} \n *Summary (junit):* Tests: ${summary.totalCount}, Passed: ${summary.passCount}, Failed: ${summary.failCount}, Skipped: ${summary.skipCount}" | |
} | |
} | |
} | |
failure { | |
echo 'Pipeline failed! π£' | |
slackSend channel: "#apollo-prs-pipelines", | |
color: 'danger', | |
message: "\n *Apollo Build Failed: * ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>) \n *Trigger*: ${BUILD_USER_NAME}, *Time took*: ${CURRENT_BUILD_DURATION}" | |
mail subject: "Apollo Build Failed: ${env.JOB_NAME} ${env.BUILD_NUMBER}", | |
body: "BUILD_URL: $BUILD_URL, \n Time took: ${CURRENT_BUILD_DURATION}", | |
to: '[email protected]' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment