在Jenkins Groovy中将闭包作为参数传递给另一个闭包

时间:2020-04-11 10:20:17

标签: groovy jenkins-pipeline closures jenkins-groovy jenkins-job-dsl

我需要将一个闭包作为参数传递给另一个闭包,不确定下面的方法是否应该起作用,或者使它起作用的方式是什么

Closure Build(String imageName, String testCommand){
    Closure testImage = { imageName ->
        sh('''cd ''' + dir + ''' \
            && if make image ''' + imageName''' 2>&1 | grep -m 1 "No rule to make target"; then exit 1; fi''',
            returnStatus: true
        )
    }

    Closure runTests = { imageName ->
        makeResult = runWithCommand(imageName, testImage(imageName))
        if (makeResult != 0) {
        runWithCommand(imageName, testCommand)
        }
    }
}

def runWithCommand(image_ref, t_command) {

    sh'''#!/bin/bash
        docker run --rm -i '''image_ref ''' /bin/bash -c \'''' + t_command + '''\'
    '''
}

在上面的代码中,testImage是一个闭包,可以通过groovy传递给另一个函数,不确定它是否正确,并且可以正常工作吗?

0 个答案:

没有答案
相关问题