Ich baue einen vollautomatischen Build-Prozess mit Jenkins-Pipeline als Code.Fehler beim Ausführen von Jenkins beim Versuch, eine Funktion im Pipeline-Code zu verwenden, was könnte der Grund sein?
Ich schrieb den folgenden Code, der das aktuelle Datum und die Uhrzeit in einem bestimmten Format zurückgeben soll und dann den Link zum Erstellen eines Ereignisses in Mixpanel (Annotation) generieren soll.
Dieser Teil des Codes in Bezug auf die Hierarchie befindet sich in der folgenden
Struktur:
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
}
}
Als ich den Build in Jenkins ausführen, schlägt es mit dem folgenden Fehler:
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
Ich habe versucht, die Funktion get_current_time_date aus der Stage Clouse herauszunehmen, aber ohne Erfolg.
Ich lief den Code in einer Groovy IDE und es funktionierte wie ein Charme, also ich vermute, dass ich diesen Code verlegt habe, können Sie bitte versuchen, das Problem zu finden?