简体   繁体   中英

How to create global function in Jenkins shared library

I am writing a shared library for Jenkins and am running across a bit of an organizational issue.

I have a number of pipeline scripts in var, however I'm finding there are a number of repeating functions and the code is not very dry.

One solution for this has been to create helper functions inside var like var/log.groovy, var/formatter.groovy. This has worked fine and I've been calling these functions from within my pipeline scripts like var/myPipeline.groovy.

I would just like to organize my var folder a bit better and keep my helper functions inside var/utils/log.groovy for example.

The problem is I'm not sure how to access them from my pipeline scripts inside var when I put them inside a sub-directory.

How can I access them? Or is there a better way to organize my global functions?

You can put them in src in a package structure that makes sense organizationally. Them import the right things in your var scripts.

in /src/com/yourco/Formatter.groovy

package com.yourco

class Formatter {
  def static String formatThis(String something) {
    "this is ${something}"
  }
}

In your var

import com.yourco.Formatter
..
..
..
   echo Formatter.formatThis('test')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM