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

Import shared Groovy code

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Icon: Minor Minor
    • pipeline
    • None
    • Jenkins: 1.625.3
      Workflow plugin: 1.11

      It should be possible to import shared Groovy code from the SCM source in a Workflow job. For instance, if my workflow script (flow.groovy) needs to access variables from another script (constants.groovy) and they are both in the SCM repo that the Workflow job checked out, it should be simple to do. It's not and currently the unabashedly terrible workaround is to do this:

      def constants
      
      stage('Import dependencies')
      node{  
        git(url: 'ssh://git@mygitserver.com:7999/ex/workflow.git', credentialsId: '60cda0cc-f13d-448c-9947-28b926a20628')
        constants = load('constants.groovy')
      }
      

      This works, but the fact that another node has to be spun up and another git checkout command is required is awful. It should be pretty straightfoward to have Jenkins simply load one of these files. Something like this:

      constants = import("constants.groovy")
      

      I'm pretty sure Groovy won't let you use "import" since it's a resered keyword, but I don't have any particularly strong feelings about the semantics of this command. I just want a way to load these files easily.

            jglick Jesse Glick
            monitorjbl Taylor Jones
            Votes:
            6 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: