Skip to content

Commit

Permalink
Merge pull request #382 from KGU-C-Lab/refactor/#381
Browse files Browse the repository at this point in the history
젠킨스 슬랙 메시지 개선 완료
  • Loading branch information
limehee authored Jun 19, 2024
2 parents 5d5b88d + 75535d5 commit ed944e8
Showing 1 changed file with 80 additions and 15 deletions.
95 changes: 80 additions & 15 deletions jenkins/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pipeline {
agent any

environment {
JENKINS_DOMAIN = credentials('jenkins_domain')

SLACK_WEBHOOK_URL = credentials('slack_webhook_url')
SLACK_COLOR_SUCCESS = credentials('slack_color_success')
SLACK_COLOR_FAILURE = credentials('slack_color_failure')
Expand Down Expand Up @@ -134,33 +136,96 @@ pipeline {
post {
failure {
script {
sendSlackNotification("${env.GIT_CHANGELOG}\n:scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE)
sendSlackBuildNotification(":scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE)
}
}

success {
script {
sendSlackNotification("${env.GIT_CHANGELOG}\n:rocket: Deployment completed successfully.", env.SLACK_COLOR_SUCCESS)
sendSlackBuildNotification(":rocket: Deployment completed successfully", env.SLACK_COLOR_SUCCESS)
}
}
}
}

def sendSlackNotification(message, color) {
withEnv([
"SLACK_WEBHOOK_URL=${env.SLACK_WEBHOOK_URL}"
]) {
def payload = """{
"attachments": [
{
"color": "${color}",
"text": "${message.replaceAll('"', '\\"').replaceAll('\n', '\\\\n')}"
}
def sendSlackNotification(String message, String color) {
def payload = [
attachments: [
[
color: color,
text: message.replaceAll('"', '\\"').replaceAll('\n', '\\\\n')
]
]
]

withEnv(["SLACK_WEBHOOK_URL=${env.SLACK_WEBHOOK_URL}"]) {
def payloadJson = groovy.json.JsonOutput.toJson(payload)
sh """
curl -X POST -H 'Content-type: application/json' --data '${payloadJson}' ${SLACK_WEBHOOK_URL}
"""
}
}


def sendSlackBuildNotification(String message, String color) {
def jobUrl = "${env.JENKINS_DOMAIN}/job/${env.JOB_NAME}"
def consoleOutputUrl = "${jobUrl}/${env.BUILD_NUMBER}/console"

def payload = [
blocks: [
[
type: "section",
text: [
type: "mrkdwn",
text: message
]
]
],
attachments: [
[
color: color,
blocks: [
[
type: "section",
text: [
type: "mrkdwn",
text: "*Change Log:*\n${env.GIT_CHANGELOG}"
]
],
[
type: "actions",
elements: [
[
type: "button",
text: [
type: "plain_text",
text: "Job",
emoji: true
],
url: jobUrl,
value: "click_job"
],
[
type: "button",
text: [
type: "plain_text",
text: "Console Output",
emoji: true
],
url: consoleOutputUrl,
value: "click_console_output"
]
]
]
].findAll { it != null }
]
}"""
]
]

withEnv(["SLACK_WEBHOOK_URL=${env.SLACK_WEBHOOK_URL}"]) {
def payloadJson = groovy.json.JsonOutput.toJson(payload)
sh """
curl -X POST --data-urlencode 'payload=${payload}' ${SLACK_WEBHOOK_URL}
curl -X POST -H 'Content-type: application/json' --data '${payloadJson}' ${SLACK_WEBHOOK_URL}
"""
}
}
Expand Down Expand Up @@ -319,7 +384,7 @@ def performHealthCheck() {
echo "Public IP address: ${PUBLIC_IP}"

def start_time = System.currentTimeMillis()
def timeout = start_time + 240000 // 4 minutes
def timeout = start_time + 150000 // 2.5 minutes

while (System.currentTimeMillis() < timeout) {
def elapsed = (System.currentTimeMillis() - start_time) / 1000
Expand Down

0 comments on commit ed944e8

Please sign in to comment.