Skip to main content

How do I access Pipeline DSLs from inside a Groovy class?

Comments

1 comment

  • Markri Please Update

    Hi,

    I tried using the steps variable in my pipeline library (also tried passing the "this" reference), but I can't seem to get it working properly due to some serialization errors. Here's my (redacted) code

    class JobUpdater implements Serializable {

    def updateJenkinsJob(steps){
    jobConfig = "mavenJob('myjob') {\n" +
    " scm {\n" +
    " git {\n" +
    " branch('tags/rc_1')\n" +
    " }\n" +
    " }\n" +
    " triggers {\n" +
    " snapshotDependencies(false)\n" +
    " scm('')\n" +
    " }\n" +
    "}"

    steps.jobDsl(scriptText: jobConfig)
    }
    }

    And my pipeline script in Jenkins (with or without sandbox tick) is:

    @Library("my-library@feature-jobupdater") import org.mycorp.JobUpdater

    node('linux') {
    release = new JobUpdater()
    release.updateJenkinsJob(steps)
    }

    For some reason I keep getting following exception

    an exception which occurred:
    ....
    Caused: java.io.NotSerializableException: hudson.model.Hudson

    At first I thought it was related to the "jobDsl" step (https://www.jenkins.io/doc/pipeline/steps/job-dsl/) using the hudson.model.Hudson class, however when digging into https://github.com/jenkinsci/job-dsl-plugin I can't find any reference to that class. So it must be related to the usage of "steps" (in my limited vision on Jenkins)

    If I move the jobDsl() call to the pipeline script with some ugly code crafting, then it seems to be working (although still very annoyingly with script approvals for each update type).

    BTW: Adding the @NonCPS annotation on the updateJenkinsJob doesn't seem to make a difference as well.

    Any clues on this?

    0

Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.