尝试在管道代码中使用函数时在 Jenkins 运行 上出现错误,可能是什么原因?

Getting an error on Jenkins run when trying to use a function in the pipeline code, what could be the reason?

我正在构建一个使用 Jenkins 管道作为代码的全自动构建过程。

我写了下面的代码,它应该return 特定格式的当前日期和时间,然后生成 link 用于在 Mixpanel 中创建事件(注释)。

这部分代码在层级上驻留在下面
结构:

node () {
   stage ('blah'){
    // Mixpanel parameters
            MP_API_KEY = "xxxxxxxxxxxxx3897f6851c95b45f"
            MP_API_SECRET = "xxxxxxxxxxxxx79351b08afde0d24"
            MP_EXPIRE = "1588896000"
            MP_APP_PLATFORM = "Android"
            MP_BASE_URL = "http://mixpanel.com/api/2.0/annotations/create?"
            def get_current_time_date() {
                Date date = new Date();   // given date
                Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
                calendar.setTime(date);   // assigns calendar to given date
                TIMEH = calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
                TIMEM = calendar.get(Calendar.MINUTE);
                TIMES = calendar.get(Calendar.SECOND);
                newdate = date.format( 'yyyy-MM-dd' );
                def result = newdate + '%' + TIMEH + ':' + TIMEM + ':' + TIMES
                result
            }
            RELEASE_DATE =  get_current_time_date()
            MP_RELEASE_NOTES = ""
            DESCRIPTION = "${MP_APP_PLATFORM}%v${MP_VERSION_NAME}${MP_RELEASE_NOTES}"
            REQUEST_URL = "api_key=${MP_API_KEY}&date=${RELEASE_DATE}&description=${DESCRIPTION}&expire=${MP_EXPIRE}"
            REQUEST_URL_NO_AMPERSAND = REQUEST_URL.replaceAll('&','')
            REQUEST_URL_API_SECRET = "${REQUEST_URL_NO_AMPERSAND}${MP_API_SECRET}"
            SIGNATURE = "md5 -q -s ${REQUEST_URL_API_SECRET}".execute().text
            CURL_COMMAND = "${MP_BASE_URL}${REQUEST_URL}&sig=${SIGNATURE}".replaceAll(' ','%20')
            // End of Mixpanel parameters
    }
}

当我 运行 在 Jenkins 中构建时,它失败并出现以下错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 23: Method definition not expected here. Please define the method at an appropriate place or perhaps try using a block/Closure instead. at line: 23 column: 5. File: WorkflowScript @ line 23, column 5.
       def get_current_time_date() {
       ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:946)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:67)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:411)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:374)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:220)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:404)
Finished: FAILURE

我也尝试过将 get_current_time_date 函数从 stage clouse 中移除,但无济于事。

我 运行 Groovy IDE 中的代码,它的效果很好,所以我猜我放错了这段代码,你能试试吗找到问题了吗?

您需要在节点外定义方法,或者如错误消息所述使用闭包。 Jenkins 管道基于 groovy 但它在语法和用法方面有一些限制(参见 https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md)。

以下示例侧重于您代码中的重要部分。

1:在节点外定义一个方法
node () {
    stage ('blah') {
        echo get_current_time_date()
    }
}

def get_current_time_date() {
    return 'hoge'
}
2:使用闭包而不是方法
node () {
   stage ('blah') {
        def get_current_time_date = {
            return 'hoge'
        }

        echo get_current_time_date()
    }
}

这并没有真正回答您的问题(正确答案已经 ),但我觉得您获取所需字符串的方式相当做作。你可以用这个简单的行替换你的函数

RELEASE_DATE = new Date().format('yyyy-MM-dd%H:m:s')

这会给你完全相同的结果并完全避免你的问题。