[27b1ca1] | 1 | #!groovy |
---|
| 2 | |
---|
[d29a394] | 3 | import groovy.transform.Field |
---|
[27b1ca1] | 4 | |
---|
| 5 | // For skipping stages |
---|
| 6 | import org.jenkinsci.plugins.pipeline.modeldefinition.Utils |
---|
| 7 | |
---|
| 8 | // Global for the stage name |
---|
| 9 | StageName = '' |
---|
| 10 | |
---|
| 11 | // wrapper around stage declaretion to be more verbose |
---|
| 12 | // and allow showing as skipped in the UI |
---|
[1483a16] | 13 | def BuildStage(String name, boolean run, Closure block ) { |
---|
[27b1ca1] | 14 | StageName = name |
---|
| 15 | echo " -------- ${StageName} -------- " |
---|
| 16 | if(run) { |
---|
| 17 | stage(name, block) |
---|
| 18 | } else { |
---|
| 19 | stage(name) { Utils.markStageSkippedForConditional(STAGE_NAME) } |
---|
| 20 | } |
---|
[8ca82de] | 21 | } |
---|
| 22 | |
---|
[4011b98] | 23 | //=========================================================================================================== |
---|
| 24 | // Common compilation routines |
---|
| 25 | //=========================================================================================================== |
---|
[6c9e0bc] | 26 | def Clean() { |
---|
[f78ead5] | 27 | BuildStage('Cleanup', true) { |
---|
[4011b98] | 28 | // clean the build by wipping the build directory |
---|
| 29 | dir(BuildDir) { |
---|
| 30 | deleteDir() |
---|
| 31 | } |
---|
| 32 | } |
---|
| 33 | } |
---|
| 34 | |
---|
[6c9e0bc] | 35 | def Checkout(commitHash = null) { |
---|
[d29a394] | 36 | BuildStage('Checkout', true) { |
---|
| 37 | //checkout the source code and clean the repo |
---|
[68f2e42] | 38 | if(commitHash) { |
---|
[032fd93] | 39 | final scmVars = checkout([$class: 'GitSCM', branches: [[name: commitHash ]]]) |
---|
| 40 | echo GitLogMessage(scmVars.GIT_COMMIT, scmVars.GIT_PREVIOUS_COMMIT) |
---|
| 41 | } else { |
---|
[d29a394] | 42 | |
---|
[032fd93] | 43 | final scmVars = checkout scm |
---|
| 44 | echo GitLogMessage(scmVars.GIT_COMMIT, scmVars.GIT_PREVIOUS_COMMIT) |
---|
| 45 | } |
---|
[d29a394] | 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | //=========================================================================================================== |
---|
| 50 | //Routine responsible of sending the email notification once the build is completed |
---|
| 51 | //=========================================================================================================== |
---|
| 52 | @NonCPS |
---|
| 53 | def SplitLines(String text) { |
---|
| 54 | def list = [] |
---|
| 55 | |
---|
| 56 | text.eachLine { |
---|
| 57 | list += it |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | return list |
---|
| 61 | } |
---|
| 62 | |
---|
[6a531ab] | 63 | PrevGitOldRef = '' |
---|
| 64 | PrevGitNewRef = '' |
---|
| 65 | def GitLogMessage(String oldRef = '', String newRef = '') { |
---|
| 66 | if (!PrevGitOldRef || !oldRef) return "\nERROR retrieveing git information!\n" |
---|
| 67 | PrevGitOldRef = oldRef |
---|
| 68 | |
---|
| 69 | if (!PrevGitNewRef || !newRef) return "\nERROR retrieveing git information!\n" |
---|
| 70 | PrevGitNewRef = newRef |
---|
| 71 | |
---|
[d29a394] | 72 | def revText = sh(returnStdout: true, script: "git rev-list ${oldRef}..${newRef}").trim() |
---|
| 73 | def revList = SplitLines( revText ) |
---|
| 74 | |
---|
| 75 | def gitUpdate = "" |
---|
| 76 | revList.each { rev -> |
---|
| 77 | def type = sh(returnStdout: true, script: "git cat-file -t ${rev}").trim() |
---|
| 78 | gitUpdate = gitUpdate + " via ${rev} (${type})" |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | def rev = oldRef |
---|
| 82 | def type = sh(returnStdout: true, script: "git cat-file -t ${rev}").trim() |
---|
| 83 | gitUpdate = gitUpdate + " from ${rev} (${type})" |
---|
| 84 | |
---|
| 85 | def gitLog = sh(returnStdout: true, script: "git rev-list --format=short ${oldRef}...${newRef}").trim() |
---|
| 86 | |
---|
| 87 | def gitDiff = sh(returnStdout: true, script: "git diff --stat --color ${newRef} ${oldRef}").trim() |
---|
| 88 | gitDiff = gitDiff.replace('[32m', '<span style="color: #00AA00;">') |
---|
| 89 | gitDiff = gitDiff.replace('[31m', '<span style="color: #AA0000;">') |
---|
| 90 | gitDiff = gitDiff.replace('[m', '</span>') |
---|
| 91 | |
---|
| 92 | return """ |
---|
| 93 | <pre> |
---|
| 94 | The branch ${env.BRANCH_NAME} has been updated. |
---|
| 95 | ${gitUpdate} |
---|
| 96 | </pre> |
---|
| 97 | |
---|
| 98 | <p>Check console output at ${env.BUILD_URL} to view the results.</p> |
---|
| 99 | |
---|
| 100 | <p>- Status --------------------------------------------------------------</p> |
---|
| 101 | |
---|
| 102 | <p>BUILD# ${env.BUILD_NUMBER} - ${currentBuild.result}</p> |
---|
| 103 | |
---|
| 104 | <p>- Log -----------------------------------------------------------------</p> |
---|
| 105 | |
---|
| 106 | <pre> |
---|
| 107 | ${gitLog} |
---|
| 108 | </pre> |
---|
| 109 | |
---|
| 110 | <p>-----------------------------------------------------------------------</p> |
---|
| 111 | <pre> |
---|
| 112 | Summary of changes: |
---|
| 113 | ${gitDiff} |
---|
| 114 | </pre> |
---|
| 115 | """ |
---|
| 116 | } |
---|
| 117 | |
---|
[8ca82de] | 118 | return this; |
---|