Дженкинс - общая библиотека с copyArtifacts, NotSerializableException WorkflowJob - PullRequest
1 голос
/ 13 мая 2019

На Дженкинс вер. 2.164.2, плагин copyArtifacts 1.42, Я получаю старую добрую ошибку NotSerializableException WorkflowJob при запуске декларативного конвейера, вызывающего общий сценарий lib, когда не сериализуемый (Jenkins.instance.getItem (jobName)) передается по вызову плагина copyArtifacts.

Ошибка исчезает, когда я комментирую либо раздел copyArtifacts, либо четыре строки def (от def job до def requiredBuildNumber) - артефакт успешно копируется, если я заменяю requiredBuildNumber.toString () числом, например, selector: specific('256') .

Я играл с ним пару дней и с удовольствием посоветую, пожалуйста!

Мой Jenkinsfile:

pipeline {
agent { label 'windows' }

parameters {
    string(defaultValue: "JOB-NAME", description: '', name: 'getInfoFromJobName')
    string(defaultValue: "2", description: '', name: 'numberOfBuildsBack')
}

options {
    buildDiscarder(logRotator(numToKeepStr: '30'))
    disableConcurrentBuilds()
    timestamps()
    ansiColor('xterm')
}


stages {
    stage('Get artifacts') {
        steps {
            cleanWs()
            checkout scm

            getLabInfoFromPrevLab jobName: "${params.getInfoFromJobName}",
                    numberOfBuildsBack: "${params.numberOfBuildsBack}",
                    getSuccessfulBuild: false
        }
    }

Где общая библиотека содержит скрипт \ vars \ getLabInfoFromPrevLab.groovy:

def call(Map params = [:]) {

def jobName = params.jobName
def numberOfBuildsBack = params.get('numberOfBuildsBack', 0)
def getSuccessfulBuild = params.get('getSuccessfulBuild', true)


def job = Jenkins.instance.getItem(jobName)
def lastReferencedBuild = job.getLastBuild()
def lastReferencedBuildNumber = lastReferencedBuild?.number.toInteger()
def requiredBuildNumber = lastReferencedBuildNumber - numberOfBuildsBack.toInteger()

copyArtifacts(projectName: jobName,
        selector: specific(requiredBuildNumber.toString())
)

}

Полная ошибка:

an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@27356c83
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@79592d5f
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@33d11a06
in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
in object org.jenkinsci.plugins.workflow.cps.CpsThread@52a3b953
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@1caca328
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@1caca328
Caused: java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowJob
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926)
at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
at java.util.HashMap.internalWriteEntries(HashMap.java:1785)
at java.util.HashMap.writeObject(HashMap.java:1362)
at sun.reflect.GeneratedMethodAccessor139.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:156)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:191)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1028)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
at java.util.TreeMap.writeObject(TreeMap.java:2438)
at sun.reflect.GeneratedMethodAccessor176.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:156)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:191)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1028)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.lambda$writeObject$0(RiverWriter.java:144)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136)
at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:143)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:500)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:476)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:463)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:387)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:259)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:247)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
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:748)

Ответы [ 2 ]

1 голос
/ 13 мая 2019

Конкретно на ваш вопрос: в соответствии с официальными общими документами Документация ,

Библиотечные классы не могут напрямую вызывать шаги

И в соответствии с ссылкой на шаги конвейера , копии артефактов являются шагом.

Для решения этой проблемы в документации предлагается несколько вариантов. Я лично предпочитаю использовать статические методы в разделяемой библиотеке, и поэтому можно ли передать скрипт в библиотеку и там, вызвать шаг:

Ваш вызов функции:

getLabInfoFromPrevLab
    script: this,
    jobName: "${params.getInfoFromJobName}",
    numberOfBuildsBack: "${params.numberOfBuildsBack}",
    getSuccessfulBuild: false

И реализация функции:

def call(Map parms = [:]) {
    def jobName = params.jobName
    def numberOfBuildsBack = params.get('numberOfBuildsBack', 0)
    def getSuccessfulBuild = params.get('getSuccessfulBuild', true)

    params.script.copyArtifacts(projectName: jobName,
        selector: specific(requiredBuildNumber.toString())
)

Отказ от ответственности: это текущая реализация не проверено

1 голос
/ 13 мая 2019

Попытайтесь отбросить все переменные, использованные объектом hudson перед копированием. Артефакты: job = lastReferencedBuild = lastReferencedBuildNumber = null

...