Create resusable jenkins pipeline script

后端 未结 4 773
余生分开走
余生分开走 2021-01-31 09:08

I am considering to use Jenkins pipeline script recently, one question is that I don\'t figure out a smart to way to create internal reusable utils code, imagine, I have a commo

4条回答
  •  一整个雨季
    2021-01-31 09:45

    Depending on how often you plan on reusing your code, you could also load a function (or a set of functions) as part of another pipeline.

    {
      // ...your pipeline code...
    
      git 'http://urlToYourGit/projectContainingYourScript'
      pipeline = load 'global-functions.groovy'
      pipeline.helloworld()    // Call one of your defined function
    
      // ...some other pipeline code...
    }
    

    This solution might seems a little bit cumbersome compared to StephenKing's one but what I like about this solution is that my global functions are all commited to Git and anybody can easily modify them without (almost) any knowledge of Jenkins, just basics of Groovy.

    In the Groovy script your are loading, make sure you add return this at the very end. This will allow you to make calls later. Otherwise when you set pipeline = load global-functions.groovy, the variable will be set to null.

提交回复
热议问题