From b1ee581dae83a814406c54b216792c88f69ed32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 23 Mar 2023 22:54:49 +0100 Subject: [PATCH] ci: add discord notifications to at build success MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- Jenkinsfile | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 018196348..268239fe9 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -32,6 +32,11 @@ pipeline { description: 'ID of Jenkins credential for Docker registry.', defaultValue: params.DOCKER_CRED_ID ?: 'MISSING' ) + string( + name: 'DISCORD_WEBHOOK_CRED', + description: 'Name of cretential with Discord webhook', + defaultValue: params.DISCORD_WEBHOOK_CRED ?: "", + ) booleanParam( name: 'PUBLISH', description: 'Publish built Docker images.', @@ -61,6 +66,16 @@ pipeline { image.push(env.DOCKER_TAG) } } } + post { + success { script { + if (params.DISCORD_WEBHOOK_CRED) { + discordNotify( + header: 'SpiffWorkflow Docker image published!', + cred: params.DISCORD_WEBHOOK_CRED, + ) + } + } } + } } } // stages post { @@ -68,3 +83,43 @@ pipeline { cleanup { cleanWs() } } // post } // pipeline + +def discordNotify(Map args=[:]) { + def opts = [ + header: args.header ?: 'Deployment successful!', + title: args.title ?: "${env.JOB_NAME}#${env.BUILD_NUMBER}", + cred: args.cred ?: null, + ] + def repo = [ + url: GIT_URL.minus('.git'), + branch: GIT_BRANCH.minus('origin/'), + commit: GIT_COMMIT.take(8), + prev: ( + env.GIT_PREVIOUS_SUCCESSFUL_COMMIT ?: env.GIT_PREVIOUS_COMMIT ?: 'master' + ).take(8), + ] + wrap([$class: 'BuildUser']) { + BUILD_USER_ID = env.BUILD_USER_ID + } + withCredentials([ + string( + credentialsId: opts.cred, + variable: 'DISCORD_WEBHOOK', + ), + ]) { + discordSend( + link: env.BUILD_URL, + result: currentBuild.currentResult, + webhookURL: env.DISCORD_WEBHOOK, + title: opts.title, + description: """ + ${opts.header} + Image: [`${params.DOCKER_NAME}:${params.DOCKER_TAG}`](https://hub.docker.com/r/${params.DOCKER_NAME}/tags?name=${params.DOCKER_TAG}) + Branch: [`${repo.branch}`](${repo.url}/commits/${repo.branch}) + Commit: [`${repo.commit}`](${repo.url}/commit/${repo.commit}) + Diff: [`${repo.prev}...${repo.commit}`](${repo.url}/compare/${repo.prev}...${repo.commit}) + By: [`${BUILD_USER_ID}`](${repo.url}/commits?author=${BUILD_USER_ID}) + """, + ) + } +}