scripted pipeline: bash: date -> MissingPropertyException: No such property: ... for class: groovy.lang.Binding

2,667 views
Skip to first unread message

b o b i

unread,
May 2, 2019, 8:16:24 AM5/2/19
to Jenkins Users
Jenkins 2.174, in a scripted pipeline the following

sh """#!/bin/bash
           echo "TTTTTTT"
           mytime="`date '+%Y_%m_%d__%H_%M_%S'`"
           echo ${mytime}
        """

produces

groovy.lang.MissingPropertyException: No such property: mytime for class: groovy.lang.Binding
	at groovy.lang.Binding.getVariable(Binding.java:63)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
	at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:289)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:293)
...

How can I have current time expanded as a string without the above error?

Daniel Butler

unread,
May 2, 2019, 8:27:40 AM5/2/19
to jenkins...@googlegroups.com
Replace ${mytime} with \$mytime

The mytime variable you've created is a bash one.

Regards 
Daniel 


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3b826017-27f1-45c9-925c-f396b13bcea9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

b o b i

unread,
May 2, 2019, 8:35:58 AM5/2/19
to Jenkins Users
Thank you, that work.

Could you explain me why "\$" worked, i.e. where it is documented? (I lost pretty much time with such "nonsense"). (a bash variable should be expanded inside a bash script as $var or "${var}" etc., i.e. why in a bash, the vars are not bashingly treated?)

On Thursday, May 2, 2019 at 2:27:40 PM UTC+2, Daniel Butler wrote:
Replace ${mytime} with \$mytime

The mytime variable you've created is a bash one.

Regards 
Daniel 
On Thu, 2 May 2019, 1:16 pm b o b i, <bobis...@gmail.com> wrote:
Jenkins 2.174, in a scripted pipeline the following

sh """#!/bin/bash
           echo "TTTTTTT"
           mytime="`date '+%Y_%m_%d__%H_%M_%S'`"
           echo ${mytime}
        """

produces

groovy.lang.MissingPropertyException: No such property: mytime for class: groovy.lang.Binding
	at groovy.lang.Binding.getVariable(Binding.java:63)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
	at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:289)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:293)
...

How can I have current time expanded as a string without the above error?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkins...@googlegroups.com.

b o b i

unread,
May 2, 2019, 9:11:19 AM5/2/19
to Jenkins Users
On Thursday, May 2, 2019 at 2:35:58 PM UTC+2, b o b i wrote:
Could you explain me why "\$" worked, i.e. where it is documented? (I lost pretty much time with such "nonsense"). (a bash variable should be expanded inside a bash script as $var or "${var}" etc., i.e. why in a bash, the vars are not bashingly treated?)

The answer is probably the following:

Triple-double-quoted strings behave like double-quoted strings, with the addition that they are multiline

Double-quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.. If the GString is ever passed to a method taking a String, the expression value inside the placeholder is evaluated to its string representation (by calling toString() on that expression) and the resulting String is passed to the method... If you need to escape the $ or ${} placeholders in a GString so they appear as is without interpolation, you just need to use a \ backslash character to escape the dollar sign:

Because the template string itself will be parsed by Groovy before it is passed to the templating framework

Mark Waite

unread,
May 2, 2019, 9:12:46 AM5/2/19
to Jenkins Users


On Thursday, May 2, 2019 at 6:35:58 AM UTC-6, b o b i wrote:
Thank you, that work.

Could you explain me why "\$" worked, i.e. where it is documented? (I lost pretty much time with such "nonsense"). (a bash variable should be expanded inside a bash script as $var or "${var}" etc., i.e. why in a bash, the vars are not bashingly treated?)


The pipeline domain specific language is based on groovy.  Groovy does not expand variable references in strings surrounded by single quote characters (ASCII 0x27).  Groovy expands variable references in strings surrounded by double quote characters (ASCII 0x22).  A groovy variable reference in a string can have the same syntax as a bash variable reference.

Thus, your text:

sh """
  echo ${mytime}
"""

attempts to resolve the groovy variable 'mytime'.

If you had used:
sh '''
  echo ${mytime}
'''

it would attempt to resolve the shell variable 'mytime'.


Mark Waite

Slide

unread,
May 2, 2019, 9:13:18 AM5/2/19
to Jenkins User Mailing List
You are correct.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/246dda31-10a1-4a58-b31f-97f43a0f319f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Reply all
Reply to author
Forward
0 new messages