By enclosing the body of the closure that is being executed in the 
constructor in a try/catch I noticed that there is an exception of type 
com.cloudbees.groovy.cps.impl.CpsCallableInvocation getting thrown. For 
some strange reason, new() wasn't propagating this exception and instead 
was returning the last evaluated value.

On Tuesday, August 28, 2018 at 8:19:23 PM UTC+5:30, Hari Krishna Dara wrote:
>
> I have a git project docker-build with the below class:
>
> $ cat src/docker_build/DockerBuild.groovy
>
> package docker_build
>
> class DockerBuild {
>     def image
>     def steps
>     
>     DockerBuild(steps) {
>         this.steps = steps
>         steps.docker.withRegistry('...', '...') {
>             image = steps.docker.image('...')
>             image.pull()
>         }
>     }
> }
>
> I tried to use the above class from a pipeline like this:
>
> docker_build_lib = library(identifier: 'docker_build@master',
>                         retriever: modernSCM( [$class: 'GitSCMSource', 
> remote: '...', credentialsId: '...']))
> docker_build = docker_build_lib.docker_build
>
> pipeline {
>     agent "any"
>
>     stages {
>         stage("Init") {
>             steps {
>                 script {
>                     d = docker_build.DockerBuild.new(this)
>                     echo "GOT: ${d}"
>                 }
>             }
>         }
>     }
> }
>
> The output I get is something like this:
>
> $ docker login -u "..." -p ******** ...
> Login Succeeded
> [Pipeline] {
> [Pipeline] echo
> in withRegistry closure
> [Pipeline] }
> [Pipeline] // withDockerRegistry
> [Pipeline] }
> [Pipeline] // withEnv
> [Pipeline] echo
> GOT: org.jenkinsci.plugins.docker.workflow.Docker$Image@7e65907b
>
> My question is, why is DockerBuild.new() returning an object of type 
> Docker$Image instead of docker_build.DockerBuild? Something weird is going 
> on with the constructor and I am unable to even diagnose it. The output 
> from any echo/println statements that I put in the constructor get lost 
> except those that that get executed before the withRegistry() call. In 
> fact, the output from image.pull() also gets lost. In another pipeline 
> where I am doing a pull within the Jenkinsfile, I see output like this:
>
> + docker pull ...
> Using default tag: latest
> latest: Pulling from ...
> Digest: 
> sha256:f767e25c35c6977a336c080f16bd9c63fcdc04c405eb984d1c178e273d0547b8
> Status: Image is up to date for ...
>
> I am just following the Loading libraries dynamically 
> <https://jenkins.io/doc/book/pipeline/shared-libraries/#loading-libraries-dynamically>
>  
> section and I do want to get this working with dynamically loaded libraries 
> as I want to avoid the global static configuration that is associated with 
> the @Library annotations. I would appreciate any help in diagnosing or 
> getting this working.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/eb37c929-33fc-4ace-85bc-47aae313ff77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to