Unexpected behavior instantiating a class from dynamically loaded library

10 views
Skip to first unread message

Hari Krishna Dara

unread,
Aug 28, 2018, 10:49:23 AM8/28/18
to Jenkins Users
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 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.

Hari Krishna Dara

unread,
Aug 28, 2018, 12:36:41 PM8/28/18
to Jenkins Users

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.
Reply all
Reply to author
Forward
0 new messages