Tinyden
Tinyden

Reputation: 574

Jenkinsfile syntax error: No such property

I'm trying to test a binary with latest commit id in Jenkins. Error happens at send slack message stage:

def kResPath = "/tmp/res.json"  // global variable, where json file is dumped to; declared at the very beginning of Jenkinsfile

def check_result_and_notify(tier, result) {
    def kExpected = 3
    def kSuccessSlackColor = "#00CC00"

    message = "Test ${tier} result: ${result}\n"
  
    def test_result = readJSON file: kResPath
    // 0 means index, "benchmarks", "real-time" is the key
    real_time = test_result["benchmarks"][0]["real_time"]
    if (real_time > kExpected) {
        message += String.format("real time = %f, expected time = %f", real_time, kExpected)
    }

    slackSend(color: ${kSuccessSlackColor}, message: message.trim(), channel: "test-result")
}

The json file looks like:

{
    "benchmarks": [
    {
        "real_time": 4,
    },
    {
        "real_time": 5,
    }
    ],
}

The error message I've received is hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: kResPath for class: WorkflowScript

Can someone tell me what's wrong with my code?

Is there any way I could test it locally so that I don't need to commit it every single time? I googled and find it needs server id and password, which I don't think accessible to me :(

Upvotes: 0

Views: 73

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28864

Your kResPath variable is undefined in the scope of that function or method (unsure which based on context). You can pass it as an argument:

def check_result_and_notify(tier, result, kResPath) {
  ...
}

check_result_and_notify(myTier, myResult, kResPath)

and even specify a default if you want:

def check_result_and_notify(tier, result, kResPath = '/tmp/res.json')

Upvotes: 2

Related Questions