Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include an external code file in Jenkins Workflow

I'm trying to include external code in my Workflow script but I'm missing something. The first step seems to work. If the path is invalid this fails:

evaluate( new File('/home/larry.fast/Wkflo.groovy'))

But I've tried a number of syntactic variations on the following and haven't found an incantation that works. All attempts have produced variants on "unable to resolve class mycode.Wkflo.

def z = mycode.Wkflo()

Wkflo.groovy contains:

package mycode;
def hello() {
  echo "Hello from workflow"
}

I've also tried variations like run(File()) or removing the package declaration.

like image 756
Jeremy Woodland Avatar asked Nov 14 '15 20:11

Jeremy Woodland


People also ask

How do I catch exceptions in Jenkins pipeline?

try/catch is scripted syntax. So any time you are using declarative syntax to use something from scripted in general you can do so by enclosing the scripted syntax in the scripts block in a declarative pipeline. So your try/catch should go inside stage >steps >script.

How do I use hidden files in Jenkins?

Secret file - click the Choose file button next to the File field to select the secret file to upload to Jenkins. SSH Username with private key - specify the credentials Username, Private Key and optional Passphrase into their respective fields.


1 Answers

Jenkins Workflow documentation now includes a section on "Loading Script Text" https://github.com/jenkinsci/workflow-plugin/blob/master/TUTORIAL.md but I found it hard to follow. Here's a simple example that demonstrates how to create complete workflows and other methods in external files.

Workflow code in Jenkins:

// Loading code requires a NODE context
// But we want the code accessible outside the node Context
// So declare extcode (object created by the LOAD operation) outside the Node block.
def extcode 

node {
    // paths are relative to your workspace
    // you may need to run git to populate your workspace
    git url: 'ssh://mygitrepo'
    extcode = load 'myExtCode.wkflo'

    // or you can directly access the file system
    // Eg. when developing your code in a local git clone
    extcode = load '/some/absolute/path/myExtCode.wkflo'

    // extcode is now a groovy object containing everything declared in the file
    extcode.hello('world')
}
// If your code contains Stage and Node blocks, 
// you'll want to initiate that code outside of the node() block above
extcode.extMain()

------ myExtCode.wkflo

// Any command placed at the root of the file get executed during the load operation
echo "hello from loading myExtCode"

// Methods declared in external code are accessible
//   directly from other code in the external file
//   indirectly via the object created by the load operation
//   eg.  extcode.hello('use this syntax from your main code')
def hello(whom) {
    echo "Hello ${whom}"
}

// Complete workflows should be created inside a controlling method
// else they will run nested inside the node() block when the load takes place (edit: added missing "def" keyword
def extMain() {
    stage 'External Code is running'
    node() {
        hello('from external node block')
    }
}

// !!Important Boilerplate!!     
// The external code must return it's contents as an object
return this;
like image 115
Jeremy Woodland Avatar answered Oct 04 '22 23:10

Jeremy Woodland