Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

젠킨스 슬랙 메시지 개선 완료 #382

Merged
merged 7 commits into from
Jun 19, 2024
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