|
| 1 | +pipeline { |
| 2 | + agent any |
| 3 | + options { |
| 4 | + // Running builds concurrently could cause a race condition with |
| 5 | + // building the Docker image. |
| 6 | + disableConcurrentBuilds() |
| 7 | + buildDiscarder(logRotator(numToKeepStr: '5')) |
| 8 | + } |
| 9 | + environment { |
| 10 | + // Some branches have a "/" in their name (e.g. feature/new-and-cool) |
| 11 | + // Some commands, such as those tha deal with directories, don't |
| 12 | + // play nice with this naming convention. Define an alias for the |
| 13 | + // branch name that can be used in these scenarios. |
| 14 | + BRANCH_ALIAS = sh( |
| 15 | + script: 'echo $BRANCH_NAME | sed -e "s#/#-#g"', |
| 16 | + returnStdout: true |
| 17 | + ).trim() |
| 18 | + //spawns GITHUB_USR and GITHUB_PSW environment variables |
| 19 | + GITHUB=credentials('38b2e4a6-167a-40b2-be6f-d69be42c8190') |
| 20 | + } |
| 21 | + stages { |
| 22 | + stage('Lint & Format') { |
| 23 | + // Run PyLint and Black to check code quality. |
| 24 | + when { |
| 25 | + changeRequest target: 'dev' |
| 26 | + } |
| 27 | + steps { |
| 28 | + sh 'docker build \ |
| 29 | + --build-arg github_api_key=$GITHUB_PSW \ |
| 30 | + --file test/Dockerfile \ |
| 31 | + --target code-checker \ |
| 32 | + -t precise:${BRANCH_ALIAS} .' |
| 33 | + sh 'docker run precise:${BRANCH_ALIAS}' |
| 34 | + } |
| 35 | + } |
| 36 | + stage('Run Tests') { |
| 37 | + // Run the unit and/or integration tests defined within the repository |
| 38 | + when { |
| 39 | + anyOf { |
| 40 | + branch 'dev' |
| 41 | + branch 'master' |
| 42 | + changeRequest target: 'dev' |
| 43 | + } |
| 44 | + } |
| 45 | + steps { |
| 46 | + echo 'Building Precise Testing Docker Image' |
| 47 | + sh 'docker build \ |
| 48 | + --build-arg github_api_key=$GITHUB_PSW \ |
| 49 | + --file test/Dockerfile \ |
| 50 | + --target test-runner \ |
| 51 | + -t precise:${BRANCH_ALIAS} .' |
| 52 | + echo 'Precise Test Suite' |
| 53 | + timeout(time: 5, unit: 'MINUTES') |
| 54 | + { |
| 55 | + sh 'docker run \ |
| 56 | + -v "$HOME/allure/precise/:/root/allure" \ |
| 57 | + precise:${BRANCH_ALIAS}' |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + post { |
| 63 | + cleanup { |
| 64 | + sh( |
| 65 | + label: 'Docker Container and Image Cleanup', |
| 66 | + script: ''' |
| 67 | + docker container prune --force; |
| 68 | + docker image prune --force; |
| 69 | + ''' |
| 70 | + ) |
| 71 | + } |
| 72 | + failure { |
| 73 | + // Send failure email containing a link to the Jenkins build |
| 74 | + // the results report and the console log messages to Mycroft |
| 75 | + // developers, the developers of the pull request and the |
| 76 | + // developers that caused the build to fail. |
| 77 | + echo 'Sending Failure Email' |
| 78 | + emailext ( |
| 79 | + subject: "FAILURE - Precise Build - ${BRANCH_NAME} #${BUILD_NUMBER}", |
| 80 | + body: """ |
| 81 | + <p> |
| 82 | + Follow the link below to see details regarding |
| 83 | + the cause of the failure. Once a fix is pushed, |
| 84 | + this job will be re-run automatically. |
| 85 | + </p> |
| 86 | + <br> |
| 87 | + <p><a href='${BUILD_URL}'>Jenkins Build Details</a></p> |
| 88 | + <br>""", |
| 89 | + replyTo: 'devops@mycroft.ai', |
| 90 | + to: 'chris.veilleux@mycroft.ai', |
| 91 | + recipientProviders: [ |
| 92 | + [$class: 'RequesterRecipientProvider'], |
| 93 | + [$class:'CulpritsRecipientProvider'], |
| 94 | + [$class:'DevelopersRecipientProvider'] |
| 95 | + ] |
| 96 | + ) |
| 97 | + } |
| 98 | + success { |
| 99 | + // Send success email containing a link to the Jenkins build |
| 100 | + // and the results report to Mycroft developers, the developers |
| 101 | + // of the pull request and the developers that caused the |
| 102 | + // last failed build. |
| 103 | + echo 'Sending Success Email' |
| 104 | + emailext ( |
| 105 | + subject: "SUCCESS - Precise Tests - Build ${BRANCH_NAME} #${BUILD_NUMBER}", |
| 106 | + body: """ |
| 107 | + <p> |
| 108 | + Build completed without issue. No further action required. |
| 109 | + Build details can be found by following the link below. |
| 110 | + </p> |
| 111 | + <br> |
| 112 | + <p> |
| 113 | + <a href='${BUILD_URL}'> |
| 114 | + Jenkins Build Details |
| 115 | + </a> |
| 116 | +  (Requires account on Mycroft's Jenkins instance) |
| 117 | + </p> |
| 118 | + <br>""", |
| 119 | + replyTo: 'devops@mycroft.ai', |
| 120 | + to: 'chris.veilleux@mycroft.ai', |
| 121 | + recipientProviders: [ |
| 122 | + [$class: 'RequesterRecipientProvider'], |
| 123 | + [$class:'CulpritsRecipientProvider'], |
| 124 | + [$class:'DevelopersRecipientProvider'] |
| 125 | + ] |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments