diff --git a/.groovylintrc.json b/.groovylintrc.json index e30e5f6..f5b873d 100644 --- a/.groovylintrc.json +++ b/.groovylintrc.json @@ -1,6 +1,9 @@ { "extends": "recommended", "rules": { + "DuplicateStringLiteral": { + "enabled": false + }, "Indentation": { "enabled": false } diff --git a/vars/getCurrentTag.groovy b/vars/addCurrentGitTag.groovy similarity index 61% rename from vars/getCurrentTag.groovy rename to vars/addCurrentGitTag.groovy index 71776aa..52ca9b5 100644 --- a/vars/getCurrentTag.groovy +++ b/vars/addCurrentGitTag.groovy @@ -10,5 +10,14 @@ String call(Map config = [:]) { script: "git describe --tags --always --dirty=${runConfig.dirtyFlag}", returnStdout: true ).trim() - return gitTag + + gitHash = sh( + script: 'git rev-parse --short HEAD', + returnStdout: true + ).trim() + + runConfig.tag = gitTag + runConfig.hash = gitHash + + return runConfig } diff --git a/vars/buildStep.groovy b/vars/buildStep.groovy index 2c282de..c5ffc53 100644 --- a/vars/buildStep.groovy +++ b/vars/buildStep.groovy @@ -1,24 +1,29 @@ #!/usr/bin/env groovy -void call(Map config = [:]) { +void call(Map config = [:], Closure body = { }) { Map defaults = [ saveArtifaces: false, buildTest: 'Makefile', - buildCmd: 'make all' + buildCmd: 'make all', + stepName: 'Build' ] Map runConfig = defaults + config - if (runConfig.saveArtifacts) { - assert runConfig.artifacts : 'Error: saveArtifacts is set to true; must specify artifacts!' - } - if ( fileExists(runConfig.buildTest)) { - sh """ - ${runConfig.buildCmd} - """ - } else { - error("ERROR: File ${runConfig.buildTest} does not exist in workspace.") - } - if (runConfig.saveArtifacts) { - artifactsList = runConfig.artifacts.inspect().replaceAll('[\\[\\]\'\"]', '') - archiveArtifacts artifacts: artifactsList, followSymlinks: false + stage(runConfig.stepName) { + printBanner(runConfig.stepName) + if (runConfig.saveArtifacts) { + assert runConfig.artifacts : 'Error: saveArtifacts is set to true; must specify artifacts!' + } + if ( fileExists(runConfig.buildTest)) { + sh """ + ${runConfig.buildCmd} + """ + } else { + error("ERROR: File ${runConfig.buildTest} does not exist in workspace.") + } + if (runConfig.saveArtifacts) { + artifactsList = runConfig.artifacts.join(',') + archiveArtifacts artifacts: artifactsList, followSymlinks: false + } + body() } } diff --git a/vars/checkoutStep.groovy b/vars/checkoutStep.groovy index 88d620d..569c87d 100644 --- a/vars/checkoutStep.groovy +++ b/vars/checkoutStep.groovy @@ -1,5 +1,16 @@ #!/usr/bin/env groovy -void call() { - checkout scm +void call(Map config = [:], Closure body = { }) { + Map defaults = [ + stepName: 'Checkout', + useJobSCM: true + ] + runConfig = defaults + config + stage(runConfig.stepName) { + printBanner(runConfig.stepName) + if (runConfig.useJobSCM) { + checkout scm + } + body() + } } diff --git a/vars/packageStep.groovy b/vars/packageStep.groovy index 09c2bf6..7553a92 100644 --- a/vars/packageStep.groovy +++ b/vars/packageStep.groovy @@ -1,21 +1,33 @@ #!/usr/bin/env groovy -void call(Map config = [:]) { +void call(Map config = [:], Closure body = { }) { Map defaults = [ name: env.BUILD_TAG, - archiveCmd: 'tar zcvf', + archiveCmd: 'tar', + archiveParams: 'zcvf', extension: 'tgz', - artifacts: [] + artifacts: [], + stepName: 'Package', + separator: '-' ] - Map runConfig = defaults + config + stage(runConfig.stepName) { + printBanner(runConfig.stepName) + if (runConfig.artifacts.size() < 1) { + assert runConfig.artifacts : 'Error: No named artifacts to package!' + } + archiveName = runConfig.name + runConfig.separator + runConfig.tag + runConfig.separator + runConfig.extension + echo "Will tag result as: ${archiveName}" - String archiveName = "${runConfig.name}-${runConfig.tag}.${runConfig.extension}" - - echo "Will tag result as: ${archiveName}" - - sh """ - ${runConfig.archiveCmd} ${archiveName} ${runConfig.artifacts.join(' ')} - """ - archiveArtifacts artifacts: archiveName, followSymlinks: false + sh """ + if command -v "${runConfig.archiveCmd}"; then + ${runConfig.archiveCmd} ${runConfig.archiveParams} ${archiveName} ${runConfig.artifacts.join(' ')} + else + echo "Error: ${runConfig.archiveCmd} is not a valid executable!" + exit 2 + fi + """ + archiveArtifacts artifacts: archiveName, followSymlinks: false + body() + } } diff --git a/vars/printBanner.groovy b/vars/printBanner.groovy new file mode 100644 index 0000000..30f4565 --- /dev/null +++ b/vars/printBanner.groovy @@ -0,0 +1,28 @@ + +void call(String stepName) { + red = '\033[31m' + blue = '\033[34m' + normal = '\033[0m' + + stepBanner = "║ $blue$stepName$red ║" + bannerWidth = stepName.length() + 6 + + top = '╔' + ('═' * bannerWidth) + '╗' + bottom = '╚' + ('═' * bannerWidth) + '╝' + ansiColor('xterm') { + echo "$red$top\n$stepBanner\n$bottom$normal" + } +} + + // fg_black = '\x1b[30m' + // fg_red = "\x1b[31m" + // fg_green = "\x1b[32m" + // fg_yellow = "\x1b[33m" + // fg_blue = "\x1b[34m" + // fg_magenta = "\x1b[35m" + // fg_cyan = "\x1b[36m" + // fg_white = "\x1b[37m" + // fg_default = "\x1b39m" + // fg_reset = "\x1b[0m" + +// ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ diff --git a/vars/publishStep.groovy b/vars/publishStep.groovy new file mode 100644 index 0000000..623366b --- /dev/null +++ b/vars/publishStep.groovy @@ -0,0 +1,12 @@ +#!/usr/bin/env groovy + +void call(Map config = [:], Closure body = { }) { + Map defaults = [ + stepName: 'Publish' + ] + runConfig = defaults + config + stage(runConfig.stepName) { + printBanner(runConfig.stepName) + body() + } +}