Team, I have a Jenkins job that runs every 5minutes and sends a slack notify to channel. but I want to not send slack if there is 0 returned in count variable.
how to achieve this? like with if else
in Jenkins pipeline?
current code
stage('Run') {
when { expression { !params.Refresh } }
steps {
script {
sh "ls -lrt src/jenkins/pipelines/sre/"
sh("cd src/jenkins/pipelines/sre/program-pod-stuck/ && python3 stuckPodInfo.py")
count = sh(script: """cd src/jenkins/pipelines/sre/program-pod-stuck/ && cat count""", returnStdout: true).trim()
slackSend(channel: "#team-alerts", message: "${count} pods from user workflow pods are stuck. <${env.BUILD_URL}console|Details>")
println count
}
}
proposed code to run conditionally.
stage('Run') {
when { expression { !params.Refresh } }
steps {
script {
sh "ls -lrt src/jenkins/pipelines/sre/"
sh("cd src/jenkins/pipelines/sre/program-pod-stuck/ && python3 stuckPodInfo.py")
count = sh(script: """cd src/jenkins/pipelines/sre/program-pod-stuck/ && cat count""", returnStdout: true).trim()
if $count != '0' {
slackSend(channel: "#team-alerts", message: "${count} pods from user workflow pods are stuck. <${env.BUILD_URL}console|Details>")
} else {
echo 'no pods stuck, suppressing slack alert'
}
println count
}
}