In relation to Jenkins DSL, what is the difference between:
def cwd = pwd()
and
cwd = pwd()
?
The def keyword is used to define an untyped variable or a function in Groovy, as it is an optionally-typed language.
Posted on June 18, 2019 by admin. Variables in a Jenkinsfile can be defined by using the def keyword. Such variables should be defined before the pipeline block starts. When variable is defined, it can be called from the Jenkins declarative pipeline using ${...} syntax.
Variables in Groovy can be defined in two ways − using the native syntax for the data type or the next is by using the def keyword. For variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is required by the Groovy parser.
def is an alias for Object , so the first 2 signatures are identical. the difference between the 1st two and the 3rd is, that you can return null or an instance of any class from the 1 and 2, whereas you can return only null from the 3rd one. Follow this answer to receive notifications.
It's a difference of scope. When you assign a value to a variable without a "def" or other type, in a Groovy script, it's added to the "binding", the global variables for the script. That means it can be accessed from all functions within the script. It's a lot like if you had the variable defined at the top of the script.
You could wind up with unexpected behavior if multiple threads are acting on the script.
def a = { x = 1 println x } def b = { x = 2 println x } new Thread(a).start() new Thread(b).start()
... could produce two ones, two twos, or a mix.
In contrast, using "def" makes a local variable:
def a = { def x = 1 println x } def b = { def x = 2 println x } new Thread(a).start() new Thread(b).start()
... will always print a 1 and a 2, in arbitrary order.
It's a good question, but it's more a Groovy question.
From what I understand, defining a variable without def
keyword will work from a script, but not if you were in a class method. Example from this blog post :
class MyTest { def testMethod() { y = 3 println y } } t = new MyTest() t.testMethod()
Variable t
will be defined without problem but y
definition will throw an exception.
What it means is that in our context (Jenkins pipeline) you could always define your variable without the def
keyword because you are always in a script context and your variables will be bound to the script. However, I think it is good practice to use def
keyword because it shows you know when you instantiate your variables, and it also can avoid some problems of duplicate variables definitions (if you define them with the def
keyword at least compilation will fail if you defined the same variable twice).
Finally, from Groovy documentation :
When using def in Groovy, the actual type holder is Object (so you can assign any object to variables defined with def, and return any kind of object if a method is declared returning def).
So you might want to be specific and specify the type of variable you are defining. In your case you could define cwd
as :
String cwd = pwd()
It would forbid you to do things like :
def cwd = pwd() cwd = 1000 // Valid code String cwd2 = pwd() cwd2 = 1000 // Will fail compilation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With