From db5491ae0f52268c89493365be1e38184a411828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Wed, 19 Jun 2024 21:46:00 +0900 Subject: [PATCH 1/7] =?UTF-8?q?refactor(Slack):=20=EC=A0=A0=ED=82=A8?= =?UTF-8?q?=EC=8A=A4=20=EB=B9=8C=EB=93=9C=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 84 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index f0ce1ac78..1cd3f349e 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -134,13 +134,13 @@ pipeline { post { failure { script { - sendSlackNotification("${env.GIT_CHANGELOG}\n:scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE) + sendSlackNotification(":scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE, true) } } success { script { - sendSlackNotification("${env.GIT_CHANGELOG}\n:rocket: Deployment completed successfully.", env.SLACK_COLOR_SUCCESS) + sendSlackNotification(":rocket: Deployment completed successfully", env.SLACK_COLOR_SUCCESS, false) } } } @@ -165,6 +165,77 @@ def sendSlackNotification(message, color) { } } +def sendSlackNotification(String message, String color, boolean isFailure = false) { + def jobUrl = "${env.BUILD_URL}" + def consoleOutputUrl = "${jobUrl}console" + def buildLog = isFailure ? getBuildLog(10) : "" + + 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}" + ] + ], + isFailure ? [ + type: "section", + text: [ + type: "mrkdwn", + text: "*Reason:*\n```${buildLog}```" + ] + ] : null, + [ + 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 -H 'Content-type: application/json' --data '${payloadJson}' ${SLACK_WEBHOOK_URL} + """ + } +} + def getChangeLog() { def previousCommit = env.GIT_PREVIOUS_SUCCESSFUL_COMMIT ?: 'HEAD~1' def currentCommit = env.GIT_COMMIT ?: 'HEAD' @@ -182,6 +253,15 @@ def getChangeLog() { return changeLog } +def getBuildLog(lines) { + def log = sh( + script: "tail -n ${lines} ${env.WORKSPACE}/target/build.log", + returnStdout: true + ).trim() + + return log.replaceAll('"', '\\"').replaceAll('\n', '\\\\n') +} + def backupPostgres() { def BACKUP_FILE = "postgres_backup_${new Date().format('yyyy-MM-dd_HH-mm-ss')}.sql" withEnv([ From a815832b99197259aa728b2609eff6c4cfd0e9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Wed, 19 Jun 2024 21:51:50 +0900 Subject: [PATCH 2/7] =?UTF-8?q?fix(Slack):=20=EC=8A=AC=EB=9E=99=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=A0=84=EC=86=A1=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=EB=AA=85=20=EC=A4=91=EB=B3=B5=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 1cd3f349e..54b273b14 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -134,13 +134,13 @@ pipeline { post { failure { script { - sendSlackNotification(":scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE, true) + sendSlackBuildNotification(":scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE, true) } } success { script { - sendSlackNotification(":rocket: Deployment completed successfully", env.SLACK_COLOR_SUCCESS, false) + sendSlackBuildNotification(":rocket: Deployment completed successfully", env.SLACK_COLOR_SUCCESS, false) } } } @@ -165,7 +165,7 @@ def sendSlackNotification(message, color) { } } -def sendSlackNotification(String message, String color, boolean isFailure = false) { +def sendSlackBuildNotification(String message, String color, boolean isFailure = false) { def jobUrl = "${env.BUILD_URL}" def consoleOutputUrl = "${jobUrl}console" def buildLog = isFailure ? getBuildLog(10) : "" From 7547ef1714b7b76bb6db347b424cfe3706cc9c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Wed, 19 Jun 2024 22:00:19 +0900 Subject: [PATCH 3/7] =?UTF-8?q?fix(Slack):=20=EC=8A=AC=EB=9E=99=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=A0=84=EC=86=A1=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=EB=AA=85=20=EC=A4=91=EB=B3=B5=20=EB=B0=8F=20?= =?UTF-8?q?base=20url=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=98=A4=EB=A5=98?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 54b273b14..4e9ad44ed 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -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') @@ -166,8 +168,8 @@ def sendSlackNotification(message, color) { } def sendSlackBuildNotification(String message, String color, boolean isFailure = false) { - def jobUrl = "${env.BUILD_URL}" - def consoleOutputUrl = "${jobUrl}console" + def jobUrl = "${env.JENKINS_DOMAIN}/job/${env.JOB_NAME}" + def consoleOutputUrl = "${jobUrl}/${env.BUILD_NUMBER}/console" def buildLog = isFailure ? getBuildLog(10) : "" def payload = [ From 38d7d2139bfe05d719686fb72104e49ad79bcb10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Wed, 19 Jun 2024 22:13:09 +0900 Subject: [PATCH 4/7] =?UTF-8?q?refactor(Jenkins):=20=ED=97=AC=EC=8A=A4=20?= =?UTF-8?q?=EC=B2=B4=ED=81=AC=20=EC=8B=9C=EA=B0=84=204=EB=B6=84->2.5?= =?UTF-8?q?=EB=B6=84=20=EC=B6=95=EC=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 4e9ad44ed..585256fc9 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -401,7 +401,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 From 97be3b239f4cc4798bbefb1cf7c26c4f5df4d3e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Wed, 19 Jun 2024 23:00:26 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor(Slack):=20=EC=A0=A0=ED=82=A8?= =?UTF-8?q?=EC=8A=A4=20=EB=B9=8C=EB=93=9C=20=EB=A1=9C=EA=B7=B8=20=EB=8C=80?= =?UTF-8?q?=EC=8B=A0=20=EC=8B=A4=ED=8C=A8=20=EB=8B=A8=EA=B3=84=EB=A5=BC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 585256fc9..3846792f1 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -197,7 +197,7 @@ def sendSlackBuildNotification(String message, String color, boolean isFailure = type: "section", text: [ type: "mrkdwn", - text: "*Reason:*\n```${buildLog}```" + text: "*Failed at stage:*\n```${env.CURRENT_STAGE}```" ] ] : null, [ @@ -255,15 +255,6 @@ def getChangeLog() { return changeLog } -def getBuildLog(lines) { - def log = sh( - script: "tail -n ${lines} ${env.WORKSPACE}/target/build.log", - returnStdout: true - ).trim() - - return log.replaceAll('"', '\\"').replaceAll('\n', '\\\\n') -} - def backupPostgres() { def BACKUP_FILE = "postgres_backup_${new Date().format('yyyy-MM-dd_HH-mm-ss')}.sql" withEnv([ From 7689d12be395c8477d96c411d67ddb9351bc851a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Wed, 19 Jun 2024 23:23:50 +0900 Subject: [PATCH 6/7] =?UTF-8?q?refactor(Slack):=20=EC=A0=A0=ED=82=A8?= =?UTF-8?q?=EC=8A=A4=20=EB=B9=8C=EB=93=9C=20=EB=A1=9C=EA=B7=B8=20=EB=8C=80?= =?UTF-8?q?=EC=8B=A0=20=EC=8B=A4=ED=8C=A8=20=EB=8B=A8=EA=B3=84=EB=A5=BC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 3846792f1..82057d28a 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -136,13 +136,13 @@ pipeline { post { failure { script { - sendSlackBuildNotification(":scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE, true) + sendSlackBuildNotification(":scream_cat: Deployment failed.", env.SLACK_COLOR_FAILURE) } } success { script { - sendSlackBuildNotification(":rocket: Deployment completed successfully", env.SLACK_COLOR_SUCCESS, false) + sendSlackBuildNotification(":rocket: Deployment completed successfully", env.SLACK_COLOR_SUCCESS) } } } @@ -167,10 +167,9 @@ def sendSlackNotification(message, color) { } } -def sendSlackBuildNotification(String message, String color, boolean isFailure = false) { +def sendSlackBuildNotification(String message, String color) { def jobUrl = "${env.JENKINS_DOMAIN}/job/${env.JOB_NAME}" def consoleOutputUrl = "${jobUrl}/${env.BUILD_NUMBER}/console" - def buildLog = isFailure ? getBuildLog(10) : "" def payload = [ blocks: [ @@ -193,13 +192,6 @@ def sendSlackBuildNotification(String message, String color, boolean isFailure = text: "*Change Log:*\n${env.GIT_CHANGELOG}" ] ], - isFailure ? [ - type: "section", - text: [ - type: "mrkdwn", - text: "*Failed at stage:*\n```${env.CURRENT_STAGE}```" - ] - ] : null, [ type: "actions", elements: [ From 75535d5b0fd342a851638fdbd1e80541ed553d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Wed, 19 Jun 2024 23:44:50 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor(Slack):=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=EB=A5=BC=20JSON=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=EB=A1=9C=20=EC=A0=95=EC=9D=98=ED=95=98=EC=97=AC=20=EC=89=BD?= =?UTF-8?q?=EA=B2=8C=20=EB=B3=80=EA=B2=BD=20=EA=B0=80=EB=8A=A5=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins/Jenkinsfile | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 82057d28a..ffe491337 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -148,25 +148,25 @@ pipeline { } } -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 --data-urlencode 'payload=${payload}' ${SLACK_WEBHOOK_URL} + 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"