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']]
)
}