Skip to content

Instantly share code, notes, and snippets.

@slawekzachcial
Last active May 24, 2019 21:39
Show Gist options
  • Save slawekzachcial/42b1627d4db700b2d40eb8829b8571df to your computer and use it in GitHub Desktop.
Save slawekzachcial/42b1627d4db700b2d40eb8829b8571df to your computer and use it in GitHub Desktop.
// Explict call parameters
def containerBuildAndPublish1(image, tag, dockerRegistry, dockerRegistryCredsId) {
//IMPLEMENTATION USAGE EXAMPLE
println "Building ${image}:${tag} and publishing to ${dockerRegistry} using creds ${dockerRegistryCredsId}"
}
// PIPELINE USAGE EXAMPLE: Explict call parameters
containerBuildAndPublish1('myimage', 'latest', 'my.docker.registry:1234', 'jenkins-docker')
containerBuildAndPublish1 'myimage', 'latest', 'my.docker.registry:1234', 'jenkins-docker'
// ----------------------------------------------------------------------------
// Named call parameters with Map
def containerBuildAndPublish2(Map config) {
//IMPLEMENTATION USAGE EXAMPLE
println "Building ${config.image}:${config.tag} and publishing to ${config.dockerRegistry} using creds ${config.dockerRegistryCredsId}"
}
// PIPELINE USAGE EXAMPLE: Named call parameters with Map
containerBuildAndPublish2(image: 'myimage', tag: 'latest', dockerRegistry: 'my.docker.registry:1234', dockerRegistryCredsId: 'jenkins-docker')
containerBuildAndPublish2 image: 'myimage', tag: 'latest', dockerRegistry: 'my.docker.registry:1234', dockerRegistryCredsId: 'jenkins-docker'
// ----------------------------------------------------------------------------
// Named call parameters with Closure
def containerBuildAndPublish3(Closure body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
//IMPLEMENTATION USAGE EXAMPLE
println "Building ${config.image}:${config.tag} and publishing to ${config.dockerRegistry} using creds ${config.dockerRegistryCredsId}"
}
// PIPELINE USAGE EXAMPLE: Named call parameters with Closure
containerBuildAndPublish3 {
image = 'myimage'
tag = 'latest'
dockerRegistry = 'my.docker.registry:1234'
dockerRegistryCredsId = 'jenkins-docker'
}
// ----------------------------------------------------------------------------
// Named call parameters with Closure Dsl (domain-specific language)
class Dsl {
def image
def tag
def dockerRegistry
def dockerRegistryCredsId
def image(val) { image = val }
def tag(val) { tag = val }
def dockerRegistry(val) { dockerRegistry = val }
def dockerRegistryCredsId(val) { dockerRegistryCredsId = val }
}
def containerBuildAndPublish4(Closure body) {
def config = new Dsl()
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
//IMPLEMENTATION USAGE EXAMPLE
println "Building ${config.image}:${config.tag} and publishing to ${config.dockerRegistry} using creds ${config.dockerRegistryCredsId}"
}
// PIPELINE USAGE EXAMPLE: Named call parameters with Closure Dsl (domain-specific language)
containerBuildAndPublish4 {
image 'myimage'
tag 'latest'
dockerRegistry 'my.docker.registry:1234'
dockerRegistryCredsId 'jenkins-docker'
}
@slawekzachcial
Copy link
Author

When defined to be used in Jenkins pipeline, the preferred containerBuildAndPublishX function would be defined in a shared library in vars/containerBuildAndPublish.groovy file and be called call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment