pipeline {
agent any
environment {
stageName = "Service Name "
ppcUrl = "https://test.com/api/servicename"
keycloakUrl = "https://keycloak.test.com/auth/realms/realm_name/protocol/openid-connect/token"
ACCESS_TOKEN = ""
httpStatus = ""
httpResponse = ""
}
stages {
stage ('Get Service keycloak token') {
steps {
script {
final def (String response, int code) =
sh(script: "curl --insecure -s -w '\\n%{response_code}' \
--location --request POST $keycloakUrl \
--header 'Authorization: Basic d2ViX2FwcDp3ZWJfYXBw' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=user' \
--data-urlencode 'password=passwprd' \
--data-urlencode 'scope=realm_nameprofile email' \
", returnStdout: true)
.trim()
.tokenize("\n")
echo "HTTP response status code: $code"
if (code == 200) {
def jsonObj = readJSON text: response
ACCESS_TOKEN = jsonObj['access_token']
if (ACCESS_TOKEN == "") {
httpStatus = "403"
error("notify token retrieving error")
}
}
}
}
}
stage ('Check Service Health') {
steps {
script {
httpStatus = sh(script: "curl --insecure -w '%{http_code}' $ppcUrl -o /dev/null \
--header 'Accept: application/json' \
--header 'Authorization: Bearer ${ACCESS_TOKEN}' \
", returnStdout: true)
if (httpStatus != "200" && httpStatus != "201" ) {
echo "Service error with status code = ${httpStatus} when calling ${ppcUrl}"
error("notify error")
} else {
echo "Service OK with status: ${httpStatus}"
}
}
}
}
}
// Finishing Up Actions
post {
success {
script {
notifySucc(stageName, httpStatus)
}
}
failure {
script {
notifyError(stageName, httpStatus)
}
}
unstable {
mattermostSend channel: "#mattermost_channel_id", color: "warning", message: ":warning: $stageName health checking is aborted. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
}
fixed {
mattermostSend channel: "#mattermost_channel_id", color: "good", message: ":white_check_mark: $stageName is returning to health. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
}
}
}
def notifySucc(String stageName = '', String httpStatus = '') {
def message = ":smiley: $stageName is working. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
echo(message)
mattermostSend channel: "#mattermost_channel_id", color: "good", message: message
}
def notifyError(String stageName = '', String httpStatus = '') {
def message
if (httpStatus == "401") {
message = ":sweat: $stageName unauthorized ($httpStatus), please update token and session. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
}
else if (httpStatus == "403") {
message = ":dizzy_face: $stageName can not get token from Keycloak ($httpStatus), please update token and session. | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
} else{
message = ":rage: $stageName is down with status: $httpStatus | ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
}
echo(message)
mattermostSend channel: "#mattermost_channel_id", color: "danger", message: message
emailext (
subject: message,
to: "youremail@test.com;someone@test.com;",
body: """<p>$stageName Health Check 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']]
)
}