2

I am trying to define Jenkins pipelines as (shared) objects, as done here. But I want to add the capability to run build actions inside a docker container.

My Jenkinsfile works like this:

@Library('ci-scons-jenkins') _

def image = docker.image("praqma/native-scons")
org.ipiq.buildci.scons.SConsPipeline.builder(this, steps, image).buildDefaultPipeline().execute()

As you can see, the docker instance is created in the Jenkinsfile and passed to the builder object to create the builder. So far, it works. Stages are executed inside the container.

Now I want to transfer the creation of the Docker instance to the Pipeline class SconsPipeline.groovy. I tried to do that with:

// I hoped it would import `Docker`
import org.jenkinsci.plugins.docker.workflow.*

class SConsPipeline implements Serializable {

    def script
    def stages
    def image
    DSL steps

    static builder(script, DSL steps) {
        // create the image to use in this build instead of using a parameter
        def docker = Docker(script)
        image = docker.image("praqma/native-scons")
        return new Builder(script, steps, image)
    }

But Jenkins fails to find the correct object:

groovy.lang.MissingMethodException: No signature of method: java.lang.Class.Docker() is applicable for argument types: (WorkflowScript) values: 

So my question is how to use docker-workflow inside an object code in a shared library.

Natxo.Piq
  • 105
  • 2
  • 9
  • My first guess is that the plugin is not being imported correctly, or that you have not instantiated an object from the class within the plugin to invoke in your method. – Matthew Schuchard Aug 10 '18 at 14:42
  • Thanks for the response Matt. From the error I get it seems that way. I browsed trough the source code of [the plugin](https://github.com/jenkinsci/docker-workflow-plugin) but I fail to see the problem. Anyway, I have a HUGE road ahead with Groovy and Jenkins, so I expect at the end I will find my error. Thanks and BR. – Natxo.Piq Aug 14 '18 at 06:47
  • Actually, upon second glance, it appears that you did both of those correctly. It appears your real problem is that you are invoking the `Docker()` constructor with incorrect input types. – Matthew Schuchard Aug 14 '18 at 13:45

1 Answers1

0

Should try :

import org.jenkinsci.plugins.docker.workflow.*
import org.jenkinsci.plugins.workflow.cps.CpsScript


static builder(script, DSL steps) {
    // create the image to use in this build instead of using a parameter
    def docker script.getClass().getClassLoader().loadClass("org.jenkinsci.plugins.docker.workflow.Docker").getConstructor(CpsScript.class).newInstance(script);
    image = docker.image("praqma/native-scons")
    return new Builder(script, steps, image)
}
Dams
  • 1