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 ?
1 Answer
Reset to default 2You 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()
}