Itai Ganot
Itai Ganot

Reputation: 6305

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

I'm building a fully automatic build process using Jenkins pipeline as a code.

I wrote the following code which is supposed to return the current date and time in a specific format and then generate the link for creating an event in Mixpanel (annotation).

This part of the code in terms of hierarchy resides in the following
structure:

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

When I run the build in Jenkins, it fails with the following error:

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

I tried taking the get_current_time_date function out of the stage clouse as well but to no avail.

I ran the code in a Groovy IDE and it worked like a charm, so I'm guessing that I've misplaced this code, can you please try and find the issue?

Upvotes: 3

Views: 26219

Answers (2)

sensei
sensei

Reputation: 621

This doesn't really answer your question (and the right answer has already been given), but I feel your way of obtaining the string you need is rather contrived. You could replace your function with this simple line

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

Which will give you the exact same result and avoid your problem altogether.

Upvotes: 1

arasio
arasio

Reputation: 1318

You need to define the method out of the node, or use a closure as the error message says. Jenkins pipeline is based on groovy but it has some constraints with regard to the syntax and usage (See https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md).

Below are samples focused on the essential part from your code.

1: define a method out of the node
node () {
    stage ('blah') {
        echo get_current_time_date()
    }
}

def get_current_time_date() {
    return 'hoge'
}
2: use a closure instead of a method
node () {
   stage ('blah') {
        def get_current_time_date = {
            return 'hoge'
        }

        echo get_current_time_date()
    }
}

Upvotes: 4

Related Questions