我是Groovy / Jenkins的新手,正在建立Jenkins管道。在我尝试修复脚本中内置的循环之前,它一直运行良好。以下是更改之前的代码:
pipeline {
agent any
parameters {
choice name: 'TargetEnvironment', description: '<b>Mandatory</b>: environment against which to book trades and roll the date.', choices: getChoices("${JENKINS_ENV_CHOICES_PATH}")
string name: 'NumberOfDaysToRoll', trim: true, defaultValue: '', description: '<i>Mandatory</i>: The total number of days you wish to roll the dates and book trades.'
booleanParam name: 'RunPaymentWindow', description: '<i>Optional</i>: Select to run payment window following final date roll.', defaultValue: false
string name: 'Email', trim: true, defaultValue: '', description: '<i>Optional</i>: e-mail(s) to notify upon completion.<br>Several e-mails can be set with semi-colon ; separation.'
}
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '5', numToKeepStr: '')
}
environment {
GENERATE_MESSAGES_FULL_PATH = "${env.GENERATED_MESSAGES_DIRECTORY}/${env.GENERATED_MESSAGES_ARCHIVE_NAME}"
}
stages {
stage('Set-up') {
steps {
//Checking mandatory parameters
script {
if (params.TargetEnvironment.isEmpty()) {
error 'You must set an Environment'
}
if (params.NumberOfDaysToRoll.isEmpty()) {
error 'You must set a number of days to rollover the date'
}
currentBuild.displayName = params.TargetEnvironment
//REMOVE BELOW LINE WHEN RUNNING PIPELINE FROM GIT
git branch: 'MUREX-15906', changelog: false, credentialsId: 'fb4d5781-fa37-40e5-8dd1-ceff1ae7058d', poll: false, url: 'git@github.aus.thenational.com:Murex/mx-automation.git'
}
}
}
stage('Trade inserts and date rollover') {
steps {
script {
for (int i = 1; i <=params.NumberOfDaysToRoll; i++) {
String mxName = params.TargetEnvironment.replaceFirst("mx", "").toUpperCase();
env.QueueManager = 'devtest';
env.QueuesSuffix = mxName;
if (mxName.equals("T00")) {
env.QueueManager = 'sit';
env.QueuesSuffix = ' ';
} else if (mxName.equals("STG")) {
env.QueueManager = 'stg'
env.QueuesSuffix = ' ';
} else if (mxName.startsWith("AS") || mxName.startsWith("PC") || mxName.startsWith("UAT")) {
env.QueueManager = 'stg'
env.QueuesSuffix = mxName;
}
String getDateOutput = env.WORKSPACE + "/get_date_output.txt"
String scriptPath = env.WORKSPACE + "/scripts/get_global_fo_desk_date.sh"
String scriptExec = scriptPath + " " + getDateOutput
sh "chmod 777 " + env.WORKSPACE
sh "chmod 755 " + env.WORKSPACE + "/scripts"
sh "chmod 755 " + scriptPath
echo "[INFO] Retrieving generation date from [" + params.TargetEnvironment + "] via [" + scriptExec + "]"
build job: 'execute_remote_script',
parameters: [string(name: 'MX_ENV', value: params.TargetEnvironment),
string(name: 'SCRIPT_PATH', value: scriptExec)]
env.environmentDate = sh returnStdout: true, script: "cat " + getDateOutput
env.environmentDate = env.environmentDate.trim()
sh "chmod 755 " + env.WORKSPACE
env.interfaceDate = sh returnStdout: true, script: "date -d " + env.environmentDate + " +%Y-%m-%d"
env.interfaceDate = env.interfaceDate.trim()
if (env.interfaceDate.isEmpty()) {
error 'Failed to retrieve environment date for interface'
} else {
echo "[INFO] Environment date for interface found as [" + env.interfaceDate + "]"
}
echo "[INFO] Turning on queue-transfer"
build job: 'queue-transfer-start-stop',
parameters: [string(name: 'Environment', value: params.TargetEnvironment),
string(name: 'Action', value: start),
string(name: 'Email', value: params.Email)]
echo "[INFO] Restarting workflows"
sh "$MX_APP/mxmlexchange/xmlrequestscript_launchsheets.sh"
//echo "[INFO] Beginning trade insertions"
//build job: 'atum',
//parameters: [string(name: 'QueueManager', value: env.QueueManager),
// string(name: 'QueuesSuffix', value: env.QueuesSuffix),
// string(name: 'MessagesPath', value: '/export/mx/murex_share/stats/prod_incoming_messages/data/' + env.interfaceDate + '-mxpc-incoming_messages-masked.tgz'),
// string(name: 'Environment', value: params.TargetEnvironment),
// string(name: 'ExcludeInterface', value: ' '),
// string(name: 'Email', value: params.Email)]
sh "chmod 755 " + env.WORKSPACE + "/scripts/date_roll.sh"
echo "[INFO] Rolling date"
build job: 'execute_remote_script',
parameters: [string(name: 'MX_ENV', value: params.TargetEnvironment),
string(name: 'SCRIPT_PATH', value: env.WORKSPACE + "/scripts/date_roll.sh")]
}
}
}
}
stage('Run payment window (optional)') {
steps {
script {
if (params.RunPaymentWindow) {
build job: 'execute_remote_script',
parameters: [string(name: 'MX_ENV', value: params.TargetEnvironment),
string(name: 'SCRIPT_PATH', value: ". ~/nab_env.ksh; $MX_CUSTOMER_BIN/mx_run_ps.ksh \"PC_SET_FIX_WIN\" \"PC_PROC\"")]
}
}
}
}
}
post {
always {
script {
if (! params.Email.isEmpty()) {
mail to: params.Email,
subject: "Pipeline [${currentBuild.fullDisplayName}] [${currentBuild.currentResult}]",
body: "URL: ${currentBuild.absoluteUrl}\nParameters: {$params}\nRan on : ${env.NODE_NAME}"
}
}
//deleteDir()
}
}
}
def getChoices(path) {
def choices = []
String file = new File(path).text
def content = file.split("\n")
for(c in content) {
choices.add(c)
}
return choices
}
现在,您可以看到我的循环中有一个错误,因为params.NumberOfDaysToRoll是一个字符串。因此,要解决此问题,我对循环进行了以下更改:
stage('Trade inserts and date rollover') {
steps {
script {
def executionLoops = params.NumberOfDaysToRoll.toInteger()
echo "Execution loops defined as: " + executionLoops
for (int i = 1; i <=executionLoops; i++) {
...
进行此更改后,出现错误:
groovy.lang.MissingPropertyException: No such property: start for class: WorkflowScript
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:293)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetProperty(GroovyInterceptor.java:68)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:354)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:291)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:295)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:271)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:271)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:271)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:74)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
at sun.reflect.GeneratedMethodAccessor255.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
即使我按照以下说明注释了错误区域,我也会因其他原因而收到另一个错误:
build job: 'queue-transfer-start-stop',
parameters: [string(name: 'Environment', value: params.TargetEnvironment),
string(name: 'Action', value: start),
string(name: 'Email', value: params.Email)
]
如果您有任何想法请告诉我。谢谢。