Last active
January 20, 2022 03:12
-
-
Save jubel-han/12a7f3c86d6e1b2a5124b24ccea60580 to your computer and use it in GitHub Desktop.
jenkins build pipeline example with slack notify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node { | |
try { | |
notifyBuild('STARTED') | |
stage('Prepare code') { | |
gitCheckThatOut('branch', 'repo URL') | |
} | |
stage('Testing') { | |
echo 'Testing' | |
echo 'Testing - publish coverage results' | |
} | |
stage('Staging') { | |
echo 'Deploy Stage' | |
} | |
// @todo add checkpoint | |
stage('Deploy') { | |
echo 'Deploy - Backend' | |
echo 'Deploy - Frontend' | |
} | |
} catch (e) { | |
// If there was an exception thrown, the build failed | |
currentBuild.result = "FAILED" | |
throw e | |
} finally { | |
// Success or failure, always send notifications | |
notifyBuild(currentBuild.result) | |
} | |
} | |
/** | |
* Clean a Git project workspace. | |
* Uses 'git clean' if there is a repository found. | |
* Uses Pipeline 'deleteDir()' function if no .git directory is found. | |
*/ | |
def gitClean() { | |
timeout(time: 60, unit: 'SECONDS') { | |
if (fileExists('.git')) { | |
echo 'Found Git repository: using Git to clean the tree.' | |
// The sequence of reset --hard and clean -fdx first | |
// in the root and then using submodule foreach | |
// is based on how the Jenkins Git SCM clean before checkout | |
// feature works. | |
sh 'git reset --hard' | |
// Note: -e is necessary to exclude the temp directory | |
// .jenkins-XXXXX in the workspace where Pipeline puts the | |
// batch file for the 'bat' command. | |
//sh 'git clean -fd -e "src/vendor/"' | |
//sh 'git submodule foreach --recursive git reset --hard' | |
//sh 'git submodule foreach --recursive git clean -ffdx' | |
} | |
else | |
{ | |
echo 'No Git repository found: using deleteDir() to wipe clean' | |
deleteDir() | |
} | |
} | |
} | |
// define the slack notify build function | |
def notifyBuild(def buildStatus) { | |
// set default of build status | |
buildStatus = buildStatus ?: 'STARTED' | |
env.JOB_DISPLAYNAME = Jenkins.instance.getJob("${env.JOB_NAME}").displayName | |
env.PREVIOUS_BUILD_RESULT = currentBuild.rawBuild.getPreviousBuild()?.getResult().toString() | |
def colorMap = [ 'STARTED': '#F0FFFF', 'SUCCESS': '#008B00', 'FAILURE': '#FF0000' ] | |
// Define messages contents | |
def subject = "Pipeline: ${env.JOB_DISPLAYNAME} - #${env.BUILD_NUMBER} ${buildStatus}" | |
def summary = "${subject} (${env.BUILD_URL})" | |
def colorName = colorMap[buildStatus] | |
// Send notifications only status changed. | |
if ("${buildStatus}" != "${env.PREVIOUS_BUILD_RESULT}") { | |
slackSend (color: colorName, message: summary, channel: "#devops-on-duty") | |
} | |
} | |
def gitCheckThatOut(String branch, String vcsUrl) { | |
branch = branch ?: 'master' | |
// cleanup | |
gitClean() | |
// checkout | |
git branch: "${branch}", url: "${vcsUrl}" | |
// get last tag | |
sh "git describe --abbrev=0 --tags > .git/tagName" | |
tagName = readFile('.git/tagName') | |
echo "${tagName}" | |
// set DisplayName | |
currentBuild.displayName = tagName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment