Skip to content

add groovy examples for different ways to format shared library steps #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/closureDelegate.groovy
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
*/
42 changes: 42 additions & 0 deletions examples/mockJenkinsExecutor.groovy
Original file line number Diff line number Diff line change
@@ -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",
])
}
}
*/
26 changes: 26 additions & 0 deletions examples/optionalParameters.groovy
Original file line number Diff line number Diff line change
@@ -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"
)
}
}
*/
43 changes: 43 additions & 0 deletions examples/optionalParametersWithClosure.groovy
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
*/
17 changes: 17 additions & 0 deletions examples/requiredParameters.groovy
Original file line number Diff line number Diff line change
@@ -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")
}
}
*/