np2807
np2807

Reputation: 1160

How to access Global variable/ functions in vars/*.groovy Jenkins Library without call method?

Noob in Shared library, I am puzzled with Jenkins document section 'Loading Libraries dynamically'.

Followed the Stackoverflow_answer, but I have some different needs, just wanted to call a function from library to pipeline with an argument.

Note: Jenkins library configuration is correct and library access is already checked with another example with call method

vars/foo.groovy contains function

//{Root}/vars/foo.groovy
def Foo_Func(Body){
    Body= Body + "This is a Message from Shared Lib."
    return Body
}

Body Variable is already defined into main Pipeline 'bar.jenkinsfile' My real problem is how to call the function from foo.groovy without using call method, I have tried following -

//somefolder_in_scm/bar.jenkinsfile
@Library('jenkins-shared-libs') _
def Body_Main=""
deg SUBJECT="Title 1"
def NativeReceivers = "[email protected]"

pipeline{
    node any
    stage{
        script {
            /*Some script*/
        }
    }
    post {
        always {
            script {
                foo.Foo_Func(Body_Main)
                // send email
                emailext attachLog: true,
                    mimeType: 'text/html',
                    subject: SUBJECT,
                    body: Body_Main,
                    to: NativeReceivers
            }
        }
    }
}

Since I have used _, I expect that no import needed. Error which is occurred after triggering pipeline,

groovy.lang.MissingMethodException: No signature of method: java.lang.Class.Foo_Func() is applicable for argument types: 

In the error, why function Foo_Func is treated as a class? It might possible that the argument need to be given in different way. Any help is appreciated.

Upvotes: 1

Views: 2317

Answers (2)

David
David

Reputation: 88

I am writing an shared lib too. I think the problem is in the:

def Foo_Func(Body)

what works for me is:

def Foo_Func(Map Body)

so if you try:

def Foo_Func(String Body)

it should work. I think it can't find the function with the right signature.

Upvotes: 0

Hank Ratzesberger
Hank Ratzesberger

Reputation: 29

Have you tried declaring a Field ?

@groovy.transform.Field def myVar = "something"


script.myScript.myVar

Assuming your file is myScript.groovy.

Upvotes: 0

Related Questions