Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-41953

printlns in shared library vars classes are ignored

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Do
    • Icon: Minor Minor
    • None
    • Jenkins 2.19.2, "Pipeline: Shared Groovy Libraries" 2.4.

      I'm iterating through building a Jenkins pipeline shared library, so my Jenkinsfile is a little cleaner.

      I'm using the following page for guidance: https://jenkins.io/doc/book/pipeline/shared-libraries/ .

      I first defined several methods in individual files, like "vars/methodName.groovy", with a "call()" method in the code. This works ok, and I particularly note that "println" calls in these methods are seen in the Jenkins console output.

      I then decided I wanted to save some state between method calls, so I added a new file in "vars" named "uslutils.groovy" that begins like this (minus some required imports):

      class uslutils implements Serializable {

      I defined some "with<property>" methods that set a property and return this.

      I then wrote a "public String toString()" method in "uslutils" that looks something like this:

      public String toString() {
      println "Inside uslutils.toString()."
      return "[currentBuild[${currentBuild}] mechIdCredentials[${mechIdCredentials}] " +
      "baseStashURL[${baseStashURL}] jobName[${jobName}] codeBranch[${codeBranch}] " +
      "buildURL[${buildURL}] pullRequestURL[${pullRequestURL}] qBotUserID[${qBotUserID}] " +
      "qBotPassword[${qBotPassword}]]"
      }

      Then, inside my Jenkinsfile, after setting the uslutils properties, I have the following lines:

      println "uslutils.qBotPassword[${uslutils.qBotPassword}]"
      println "uslutils[${uslutils}]"
      println "uslutils.toString()[${uslutils.toString()}]"

      Here are corresponding lines of output from the build:

      [Pipeline] echo
      uslutils.qBotPassword[...]
      [Pipeline] echo
      uslutils.toString()[[currentBuild[org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper@41fb2c94] mechIdCredentials[121447d5-0fe4-470d-b785-6ce88225ef01] baseStashURLhttps://... jobName[unified-service-layer-build-pipeline] codeBranch[master] codeURL[ssh://git@...] buildURLhttp://... pullRequestURL[] qBotUserID[...] qBotPassword[...]]

      As you can see, the line attempting to print "uslutils[${uslutils}]" was simply ignored. The line attempting to print "uslutils.toString()[${uslutils.toString()}]" did render, but also note that the "Inside uslutils.toString()." did not render.

      It's hard enough to debug pipelines without making it impossible to print debugging statements in shared library methods.

            Unassigned Unassigned
            dkarr David Karr
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: