diff --git a/README.md b/README.md new file mode 100644 index 0000000..4349568 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# External Jenkins pipeline helpers + +## Usage: +Inside Jenkins pipeline script run two following commands: +```groovy +// This shell command will download groovy helpers file +sh "curl -L http://[git-url]/[organization]/[repo-name]/raw/branch/master/helpers.groovy -o helpers.groovy" + +// This will load script to the pipeline +load "helpers.groovy" +``` +> This can be used in scripting pipeline syntax only! + +After all you will have in your pipeline all functions that `helpers.groovy` provides: +- stageWithChecks +- setSuccessCheck +- withGiteaCreds \ No newline at end of file diff --git a/helpers.groovy b/helpers.groovy index ce93f11..284bfde 100644 --- a/helpers.groovy +++ b/helpers.groovy @@ -1,10 +1,50 @@ +/* -------------------------------------------------------------------------- */ +/** +* +* Helper function that emits `Success` status. +* +*/ def setSuccessCheck(String name, String title) { - publishChecks name: name, title: title, summary: '— Successful', conclusion: "SUCCESS" + publishChecks( + name: name, + title: title, + summary: '— Successful', + conclusion: "SUCCESS" + ) } - +/* -------------------------------------------------------------------------- */ +/** +* Replacement for `stage` function for default pipeline. +* +* Has the same arguments but does side-effects to the stage: +* 1. Runs stage with Gitea checks. It will automatically set check status +* as running and failed if stage is failed. +* +* 2. If stage was successful it will emit `Success` state as a check. +* +*/ def stageWithChecks(String name, Closure body) { stage(name) { body() setSuccessCheck(name, name) } -} \ No newline at end of file +} +/* -------------------------------------------------------------------------- */ +/** +* Wrapper function for `withCredentials` and `gitUsernamePassword` tool. +* +* By passing `credentialsId` and `gitToolName` it will execute next provided +* closure with already activated credentials on git commands. +* +*/ +def withGiteaCreds(String credentialsId, String gitToolName, Closure body) { + withCredentials([ + gitUsernamePassword( + credentialsId: credentialsId, + gitToolName: gitToolName + ) + ]) { + body() + } +} +/* -------------------------------------------------------------------------- */ \ No newline at end of file