在Jenkins共享库中使用Docker进行构建

时间:2018-08-10 13:09:51

标签: jenkins groovy jenkins-pipeline jenkins-docker

我正在尝试将Jenkins管道定义为(共享)对象,如here一样。但是我想增加在Docker容器中运行构建操作的功能。

我的Jenkinsfile的工作方式如下:

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

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

如您所见,docker instance是在Jenkinsfile中创建的,并传递给构建器对象以创建构建器。到目前为止,它仍然有效。阶段是inside容器执行的。

现在,我想将Docker实例的创建转移到Pipeline类SconsPipeline.groovy。我尝试用以下方法做到这一点:

// 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)
    }

但是詹金斯无法找到正确的对象:

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

所以我的问题是如何在共享库的对象代码中使用docker-workflow

1 个答案:

答案 0 :(得分:0)

应该试试:

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)
}