该作业的目标是调用在某些机器上运行命令的函数。由于机器将位于不同的云、订阅等中,因此我们有不同的凭据可以访问。我尝试调用我定义的函数,并将其传递给 withCredentials() 一个变量,但我总是遇到错误。
我尝试了几种方法,例如在函数定义之上添加@NONCPS。我试图将定义置于管道范围之外。我尝试过 $variable、${variable} 的变量组合,但它们在序列化时都会产生相同的类似错误。
我也尝试了这里建议的内容:https://community.jenkins.io/t/using-withcredentials-in-freestyle-groovy-script/3051但得到了同样的错误。
示例脚本如下:
def server_inventory='''[
{
"agentName": "agent01",
"machineName": "dev-agent01",
"instanceId": "i-xxxxx",
"cloud": "aws",
"accountName": "DevQa",
"accountId": xxxxxxxx,
"directoryId": "",
"resourceGoup": "",
"region": "us-east-1",
"credential": "awsDevQA"
},
{
"agentName": "agent20",
"machineName": "pp-agent20",
"instanceId": "",
"cloud": "azure",
"accountName": "pp",
"accountId": xxxxxxxx,
"directoryId": "xxxxxx",
"resourceGoup": "rg-pp",
"region": "eastus",
"credential": "azPP"
},
.
.
.
.
'''
def gl_confData = readJSON(text: server_inventory)
stages{
stage('agent commands'){
steps{
script{
def startAzureVm = {vmName, resourceGroup, jenkinsCred, tenantId ->
withCredentials([usernamePassword(credentialsId: jenkinsCred, passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]){
sh script: 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $tenantId'
Sh '<run other commands'>
}
def getVmDetails = {agentName ->
return gl_confData.find{it.agentName == agentName}
}
def job = Jenkins.instance.getItemByFullName("${JOB_NAME}")
def agentParam = job.getProperty(hudson.model.ParametersDefinitionProperty.class).getParameterDefinition('Agent')
def agentChoices = agentParam.getChoices()
agentChoices.each{ str ->
def agent = getVmDetails(str)
if(agent){
if(agent.cloud == 'azure'){
startAzureVm(agent.machineName, agent.resourceGoup, agent.credential, agent.directoryId)
}
}
}
}
}
}
}
}
错误是:
> an exception which occurred: in field
> org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript.script
> in object
> org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript@715df3b8
> in field groovy.lang.Closure.delegate in object
> org.jenkinsci.plugins.workflow.cps.CpsClosure2@4f26f1b8 in field
> groovy.lang.Closure.delegate in object
> org.jenkinsci.plugins.workflow.cps.CpsClosure2@36918bf9 in field
> org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures in object
> org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@7511cc00 in object
> org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@7511cc00 Also:
> org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId:
> 085e099e-d54d-4c29-ad88-7805002639d6 Caused:
> java.io.NotSerializableException: hudson.model.Hudson at
> PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:278)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
> at
> java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
> at
> java.base/java.util.LinkedHashMap.internalWriteEntries(LinkedHashMap.java:334)
> at java.base/java.util.HashMap.writeObject(HashMap.java:1497) at
> PluginClassLoader for
> workflow-support//org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:157)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:231)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1128)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:271)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
> at
> java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
> at PluginClassLoader for
> workflow-cps//com.cloudbees.groovy.cps.SerializableScript.writeObject(SerializableScript.java:26)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:157)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:231)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1128)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:271)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1182)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1140)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:271)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1182)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1140)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:271)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1182)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1140)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1119)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:271)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
> at
> java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
> at
> java.base/java.util.HashMap.internalWriteEntries(HashMap.java:1944)
> at java.base/java.util.HashMap.writeObject(HashMap.java:1497) at
> PluginClassLoader for
> workflow-support//org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:157)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:231)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1128)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:271)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1182)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1140)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:271)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
> at PluginClassLoader for
> workflow-support//org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:116)
> at PluginClassLoader for
> workflow-support//org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.lambda$writeObject$1(RiverWriter.java:147)
> at PluginClassLoader for
> script-security//org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:331)
> at PluginClassLoader for
> workflow-support//org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:146)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:580)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:557)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:540)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:464)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:331)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:295)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.lambda$wrap$4(CpsVmExecutorService.java:140)
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
> at
> hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
> at
> jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
> at
> jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
> at
> jenkins.util.ErrorLoggingExecutorService.lambda$wrap$0(ErrorLoggingExecutorService.java:51)
> at
> java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
> at
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
> at
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$1.call(CpsVmExecutorService.java:53)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$1.call(CpsVmExecutorService.java:50)
> at
> org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
> at
> org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
> at PluginClassLoader for
> workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.lambda$categoryThreadFactory$0(CpsVmExecutorService.java:50)
> at java.base/java.lang.Thread.run(Thread.java:840)
日志告诉您某些对象不可序列化,然后多次提到
workflow-support
。如果您查看代码,这一行将产生一个不可序列化的变量:
def job = Jenkins.instance.getItemByFullName("${JOB_NAME}")
也许会牺牲一点可读性并链接以下调用来获取可以序列化的内容,例如字符串列表:
def agentChoices = Jenkins.het().getItemByFullName("${JOB_NAME}")
.getProperty(hudson.model.ParametersDefinitionProperty.class)
.getParameterDefinition('Agent')
.getChoices()
您的代码可能存在其他问题,如果不运行它很难判断。