Getting an error on Jenkins run when trying to use a function in the pipeline code, what could be the reason?
<p>I'm building a fully automatic build process using Jenkins pipeline as a code.</p>

<p>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).</p>

<p>This part of the code in terms of hierarchy resides in the following<br>
structure:</p>

<pre><code>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}&amp;date=${RELEASE_DATE}&amp;description=${DESCRIPTION}&amp;expire=${MP_EXPIRE}"
            REQUEST_URL_NO_AMPERSAND = REQUEST_URL.replaceAll('&amp;','')
            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}&amp;sig=${SIGNATURE}".replaceAll(' ','%20')
            // End of Mixpanel parameters
    }
}
</code></pre>

<p>When I run the build in Jenkins, it fails with the following error:</p>

<pre><code>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
</code></pre>

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

<p>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?</p>