New warning after updating to 2.492.2

After updating to Jenkins 2.492.2 I’ve started to see warnings in my pipeline console similar to, “Did you forget the def keyword? WorkflowScript seems to be setting a field named {variableName} (to a value of type Integer) which could lead to memory leaks or other issues..”. Nothing has changed in my pipeline script, and as far as I know I’m not doing anything unusual in the pipeline. It looks similar to…

MyGlobalVariable = 69

pipeline {
  agent {
    label 'master'
  }
  stages {
    stage('First Stage') {
      agent { label 'master }
      steps {
        script {
          println("My Variable = ${MyGlobalVariable}")
        }
      }
    }
  }
}

I’ve tried putting ‘def’ before the global variable, but that causes an error when I try to access the variable (groovy.lang.MissingPropertyException: No such property).

Why am I seeing this warning after upgrading, and how can I resolve it?

1 Like

I also see the same issue. Any help would be appreciated.

See jenkins - How to use environment variables in a groovy function using a Jenkinsfile - Stack Overflow

So basically this should work without a warning:

import groovy.transform.Field

@Field def MyGlobalVariable = 69

pipeline {
  agent {
    label 'master'
  }
  stages {
    stage('First Stage') {
      steps {
        script {
          println("My Variable = ${MyGlobalVariable}")
        }
      }
    }
  }
}

Thank you that fixed it.