pipeline {
agent any
environment {
stageName = "Daily Data Report"
matterMostChannel = "#jenkins-job-debug"
mysqlContainer = "a33bdcebf6e4"
execOutput = ""
datePeriod = ""
}
stages {
stage('Load part data') {
steps {
script {
//ae-clip timezone -1
// the day before yesterday 23:00::00 ---- yesterday 23:00::00
def now = new Date()
def dateMinusOne = now - 1
dayMinusOne = dateMinusOne.format("yyyy-MM-dd")
def dayStart = dayMinusOne + " 00:00:00"
def dayEnd = dayMinusOne + " 23:59:59"
datePeriod = dayStart + "--" + dayEnd
stageName = stageName + " [" + datePeriod + "]"
echo "mysql/date: ${mysqlContainer} ${datePeriod}"
echo "BEGINBEGINBEGIN"
def cmd = """
cd /local/home/mobabel-dev/ucp_bundle &&
eval "\$(<env.sh)" &&
cd /local/home/mobabel-dev/docker-stack
container_string=`docker ps | grep service-mysql-name` ;
container_name=`echo "\$container_string" | cut -c1-12` ;
exec_output=`docker exec -i \$container_name mysql -uroot <<< "use databasename;\
select * from my_data where created_date >= '${dayStart}' and created_date <= '${dayEnd}';"` ;
if [ -z "\$exec_output" ]
then
echo "token""==No_data=="
else
echo "token""BEGINBEGINBEGIN\n""\$exec_output"
echo "token""ENDENDEND\n"
fi
"""
sshPublisher(
continueOnError: false,
failOnError: true,
publishers: [
sshPublisherDesc(
configName: 'MyServerClient',
transfers: [
sshTransfer(
execCommand: '''
cd /local/home/mobabel-dev/ucp_bundle &&
eval "$(<env.sh)" &&
cd /local/home/mobabel-dev/docker-stack
'''
),
sshTransfer(
remoteDirectory: 'docker-stack',
removePrefix: '',
execCommand: cmd
)
],
verbose: true
)
]
)
}
}
}
stage('CheckLog') {
steps {
script {
def list = manager.build.logFile.readLines()
def BeginCount = list.count {it.startsWith("token") && it.contains("BEGINBEGINBEGIN")}
def EndCount = list.count {it.startsWith("token") && it.contains("ENDENDEND")}
echo("BeginCount: ${BeginCount}/EndCount: ${EndCount}")
if (BeginCount == 1 && EndCount == 1){
// enable "staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getText java.io.File"
// in https://jenkins.server/scriptApproval/
def logContent = manager.build.logFile.text
def startIndex = logContent.indexOf('tokenBEGINBEGINBEGIN') + 'tokenBEGINBEGINBEGIN'.length()
def endIndex = logContent.indexOf('tokenENDENDEND')
def rawOutput = logContent.substring(startIndex, endIndex)
//execOutput = rawOutput.replace("\n", "<br/>\n")
execOutput = 'Please see attachment.'
writeFile file: "dailylog.txt", text: rawOutput
}
}
}
}
}
// Finishing Up Actions
post {
success {
script {
notifySucc(stageName, matterMostChannel, execOutput)
}
}
failure {
script {
notifyError(stageName, matterMostChannel)
}
}
unstable {
script {
notifyUnstable(stageName, matterMostChannel)
}
}
fixed {
script {
notifyFixed(stageName, matterMostChannel)
}
}
}
}
def notifySucc(String stageName = '', String matterMostChannel = '', String execOutput = '') {
def message = "$stageName"
mattermostSend channel: matterMostChannel, color: "good", message: message
emailext (
subject: message,
to: "your_mail@mail.com",
body: """<p>$stageName':</p>
<p>$execOutput</p>""",
attachmentsPattern: 'dailylog.txt'
)
}
def notifyError(String stageName = '', String matterMostChannel = '') {
def message = ":rage: $stageName failed, please (<${env.BUILD_URL}| check detail for line id>). | ${env.BUILD_NUMBER} "
echo(message)
mattermostSend channel: matterMostChannel, color: "danger", message: message
emailext (
subject: message,
to: "your_mail@mail.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>"""
)
}
def notifyUnstable(String stageName = '', String matterMostChannel = '') {
def message = ":warning: $stageName is aborted. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
echo(message)
mattermostSend channel: matterMostChannel, color: "warning", message: message
}
def notifyFixed(String stageName = '', String matterMostChannel = '') {
def message = ":white_check_mark: $stageName is returning to health. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
echo(message)
mattermostSend channel: matterMostChannel, color: "good", message: message
}