diff --git a/examples/closureDelegate.groovy b/examples/closureDelegate.groovy new file mode 100644 index 0000000..25bb7fe --- /dev/null +++ b/examples/closureDelegate.groovy @@ -0,0 +1,29 @@ +#!/usr/bin/env groovy + +def call(Closure body) { + def config = [:] + body.resolveStrategy = Closure.DELEGATE_FIRST + body.delegate = config + body() + + // config stored as a map + print(config) + + // refer to values in the map + print(config.myArg) +} + +/* +// Example Usage +stage('Hello') { + steps { + // must be in a script block for closure delegates + script { + closureDelegate() { + myArg = "this" + otherArg = "that" + } + } + } +} +*/ \ No newline at end of file diff --git a/examples/mockJenkinsExecutor.groovy b/examples/mockJenkinsExecutor.groovy new file mode 100644 index 0000000..8285b6e --- /dev/null +++ b/examples/mockJenkinsExecutor.groovy @@ -0,0 +1,42 @@ +#!/usr/bin/env groovy + +/* +This allows injecting a jenkins executor into the pipeline step in order to mock behavior in tests. + +Mocking can also be done by using the BasePipelineSpecification helper without needing to inject +the jenkins executor in a pipeline step. + +For example: + +helper.registerAllowedMethod('isUnix', {return false}) +helper.registerAllowedMethod("sh", [String.class], { cmd -> + if (cmd.contains("aws")) { + failBuild() + } +}) + + */ +def call(executor, map) { + + String JSON = map.getOrDefault("JSON", "defaultJson") + String credentialsToken = map.getOrDefault("credentialsToken", "defaultCreds") + String user = map.getOrDefault("user", "defaultUser") + String repository = map.getOrDefault("repository", "defaultRepo") + String hash = map.getOrDefault("hash", "defaultHash") + + script { + executor.echo "curl -X POST -m 10 -d ${JSON} -u ${credentialsToken} https://api.github.com/repos/${user}/${repository}/statuses/${hash}" + } +} + +/* +// Example Usage +stage('Hello') { + steps { + injectJenkinsExecutor(this, [ + JSON: "myJson", + credentialsToken: "myUser", + ]) + } +} +*/ \ No newline at end of file diff --git a/examples/optionalParameters.groovy b/examples/optionalParameters.groovy new file mode 100644 index 0000000..b872157 --- /dev/null +++ b/examples/optionalParameters.groovy @@ -0,0 +1,26 @@ +#!/usr/bin/env groovy + + +def call(Map map) { + String name = map.getOrDefault("name", "defaultName") + String repository = map.getOrDefault("repository", "defaultRepository") + String hash = map.getOrDefault("hash", "defaultHash") + echo "name=$name, repository=$repository, hash=$hash" +} + +/* +// Example Usage +stage('Hello') { + steps { + optionalParameters( + name: "myJson", + repository: "myRepo", + hash: "myHash" + ) + + optionalParameters( + name: "myJson" + ) + } +} +*/ \ No newline at end of file diff --git a/examples/optionalParametersWithClosure.groovy b/examples/optionalParametersWithClosure.groovy new file mode 100644 index 0000000..1da9603 --- /dev/null +++ b/examples/optionalParametersWithClosure.groovy @@ -0,0 +1,43 @@ +#!/usr/bin/env groovy + +def call(final Map config, final Closure body) { + final String username = config.getOrDefault("name", "defaultUser") + final String email = config.getOrDefault("email", "defaultEmail") + final String path = config.getOrDefault("path", "defaultPath") + + println(username) + println(email) + println(path) + + echo "before closure" + + // body.call() can be called with 0 or more arguments + String combinedArg = "username=$username, email=$email, path=$path" + body.call(combinedArg) + + echo "after closure" +} + +/* + +// example usage +stage('Hello') { + steps { + + // works without specifying the extra closure argument + optionalParametersWithClosure(name: "nameIsJenkins", email: "mySpecialEmail", path: "myPath") { + echo "hello" + } + + // allows user to optionally use the combined arg passed to the closure body.call() + optionalParametersWithClosure(name: "nameIsJenkins", email: "mySpecialEmail", path: "myPath") { myStr -> + echo "$myStr" + } + + // map allows optional arguments + optionalParametersWithClosure(email: "mySpecialEmail") { + echo "hello" + } + } +} + */ \ No newline at end of file diff --git a/examples/requiredParameters.groovy b/examples/requiredParameters.groovy new file mode 100644 index 0000000..ae89bf2 --- /dev/null +++ b/examples/requiredParameters.groovy @@ -0,0 +1,17 @@ +#!/usr/bin/env groovy + + +def call(String JSON, String user, String repository, String hash, String credentialsToken) { + script { + echo "curl -X POST -m 10 -d ${JSON} -u ${credentialsToken} https://api.github.com/repos/${user}/${repository}/statuses/${hash}" + } +} + +/* +// Example Usage +stage('Hello') { + steps { + requiredParameters("myJson", "myUser", "myRepo", "myHash", "myCreds") + } +} +*/ \ No newline at end of file