簡體   English   中英

在 Jenkins 共享庫中使用 Docker 構建

[英]Build with Docker in a Jenkins shared library

我正在嘗試將 Jenkins 管道定義為(共享)對象,就像這里所做的那樣。 但我想添加在 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實例是在Jenkinsfile中創建的,並傳遞給builder對象來創建builder。 到目前為止,它有效。 階段在容器inside執行。

現在我想將 Docker 實例的創建轉移到管道類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

應該試試 :

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM