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