Creating a remote docker MySQL database record validation check with Jenkins pipeline and Mattermost notification
In this post, a simple health checker for one microservice will be created using the Jenkins pipeline.
Prerequisites:
1. Background
This is a special request from the developer team.
We have a web service, which is running in the remote docker cluster.
2. 1 Configure Mattermost Config
3. 2 Create Job
4. 3 Add Build Trigger
For the upper three steps, please check this article: Creating a web service health check with Jenkins pipeline and Mattermost notification
5. 4 Publish Over SSH Config
6. Full script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
pipeline { agent any environment { stageName = "PSwarm CC-BHP ppc (Duplicated Lots Checking)" ppcMysqlContainer = "a33bdcebf6e4" today = "" httpStatus = "" httpResponse = "" } stages { stage('Check double lots today') { steps { script { def now = new Date() today = now.format("yyyy-MM-dd") echo "mysql/date: ${ppcMysqlContainer} ${today}" def cmd = """ cd /local/home/procon-dev/ucp_bundle && eval "\$(<env.sh)" && cd /local/home/procon-dev/docker-stack //search the service name container_string=`docker ps | grep service-name-mysql` ; //get the first return 12 chars from output as the container id container_name=`echo "\$container_string" | cut -c1-12` ; //execute the SQL validate statement products_output=`docker exec -i \$container_name mysql -uroot -pPassword <<< "use ppc;select line_id from lot_assignment where product_status='IN_PRODUCTION' and date(plan_date) = '${today}' group by line_id having count(*) = 2;"` ; echo "\$products_output"; if [ -z "\$products_output" ] then echo "token""==No_duplicated_products==" else echo "token""==Found_duplicated_products==: \$products_output" fi """ sshPublisher( continueOnError: false, failOnError: true, publishers: [ sshPublisherDesc( configName: 'MyLocalClient', transfers: [ sshTransfer( execCommand: ''' cd /local/home/procon-dev/ucp_bundle && eval "$(<env.sh)" && cd /local/home/procon-dev/docker-stack ''' ), sshTransfer( remoteDirectory: 'docker-stack', removePrefix: '', sourceFiles: '*/**', execCommand: cmd ) ], verbose: true ) ] ) } } } stage('CheckLog') { steps { script { //https://plugins.jenkins.io/groovy-postbuild/ def list = manager.build.logFile.readLines() def JobCount = list.count {it.startsWith("token") && it.contains("token==Found_duplicated_products==")} echo("JobCount ${JobCount}") if (JobCount == 1){ error("Found_duplicated_products") } } } } } // Finishing Up Actions post { success { script { notifySucc(stageName) } } failure { script { notifyError(stageName) } } unstable { mattermostSend channel: "#p-cc-bhp-ppc-app", color: "warning", message: ":warning: $stageName is aborted. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)" } fixed { mattermostSend channel: "#p-cc-bhp-ppc-app", color: "good", message: ":white_check_mark: $stageName is returning to health. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)" } } } def notifySucc(String stageName = '') { def message = ":smiley: $stageName is working. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Detail>)" echo(message) mattermostSend channel: "#p-cc-bhp-ppc-app", color: "good", message: message } def notifyError(String stageName = '') { def message message = ":rage: $stageName found duplicated products in production!!, please (<${env.BUILD_URL}| check detail>). | ${env.BUILD_NUMBER} " echo(message) mattermostSend channel: "#p-cc-bhp-ppc-app", color: "danger", message: message //https://www.jenkins.io/blog/2016/07/18/pipeline-notifications/ emailext ( subject: message, to: "email@test.com;", body: """<p>$stageName Failed: '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p> <p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""" //,recipientProviders: [[$class: 'DevelopersRecipientProvider']] ) } |
Reference:
https://www.jenkins.io/doc/pipeline/steps/publish-over-ssh/
Groovy Postbuild in Jenkins, parsing the log for strings and counting them