最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

groovy - Using methods from shared scripts in methods defined outside pipeline block in jenkins - Stack Overflow

programmeradmin2浏览0评论

In my various pipelines I have been using shared scripts like this:

@Library('SharedScripts') _
def myUtils= new shared.scripts.myUtils()

pipeline{
   ....
   // some stage 
   myFunction()
}

def myFunction(){
   // some computation 
   myUtils.foo()
}

myUtils.groovy file resides in src/shared/scripts/ folder and the method myFunction defined outside of the pipeline block makes some additional computation than call the method foo() from the myUtils script.

myUtils looks like this:

package shared.scripts

def foo(){
//some stuff
}

After the latest Jenkins updates, Jenkins issues a warning with "Did you fet the "def" keyword? " for my myUtils= new shared.scripts.myUtils() call. However, def myUtils= new shared.scripts.myUtils() makes myUtils unaccessible by the methods defined outside of the pipeline block so I have to move the additional functionality of myFunction inside the pipeline block.

@Library('SharedScripts') _
myUtils= new shared.scripts.myUtils()

pipeline{
   ....
   // some stage 
   // myFunction()
   // some computation 
   myUtils.foo()
}

I would rather prefer to keep my myFunction as it is now, for better visibility and not to have to refactor many of my pipelines. Is there any other clever workaround so I can keep my method myFunction defined outside of the pipeline block and still use myUtils script and also not get the warning about def keyword ?

In my various pipelines I have been using shared scripts like this:

@Library('SharedScripts') _
def myUtils= new shared.scripts.myUtils()

pipeline{
   ....
   // some stage 
   myFunction()
}

def myFunction(){
   // some computation 
   myUtils.foo()
}

myUtils.groovy file resides in src/shared/scripts/ folder and the method myFunction defined outside of the pipeline block makes some additional computation than call the method foo() from the myUtils script.

myUtils looks like this:

package shared.scripts

def foo(){
//some stuff
}

After the latest Jenkins updates, Jenkins issues a warning with "Did you fet the "def" keyword? " for my myUtils= new shared.scripts.myUtils() call. However, def myUtils= new shared.scripts.myUtils() makes myUtils unaccessible by the methods defined outside of the pipeline block so I have to move the additional functionality of myFunction inside the pipeline block.

@Library('SharedScripts') _
myUtils= new shared.scripts.myUtils()

pipeline{
   ....
   // some stage 
   // myFunction()
   // some computation 
   myUtils.foo()
}

I would rather prefer to keep my myFunction as it is now, for better visibility and not to have to refactor many of my pipelines. Is there any other clever workaround so I can keep my method myFunction defined outside of the pipeline block and still use myUtils script and also not get the warning about def keyword ?

Share Improve this question asked Mar 28 at 11:49 Mehmet Hakan KurtoğluMehmet Hakan Kurtoğlu 931 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You may want to use @Field:

@Library('SharedScripts') _

import groovy.transform.Field

@Field def myUtils = new shared.scripts.myUtils()

pipeline{
   ....
   // some stage 
   myFunction()
}

def myFunction(){
   // some computation 
   myUtils.foo()
}
发布评论

评论列表(0)

  1. 暂无评论