String interpolation with readFileFromWorkspace

717 views
Skip to first unread message

milki milk

unread,
Oct 24, 2015, 9:06:27 PM10/24/15
to job-dsl-plugin
I want my shell steps to be generated from files in the workspace. readFileFromWorkspace would work but it returns a Java String rather than a groovy GString. This means I can't also interpolate variables from my groovy script into the file string.

A workaround would be to define all my variables as EnvironmentVariables and let Jenkins handle the variables. However, it would be great if the groovy could just interpolate the variables directly. Another workaround is to move the file contents into my script as strings, but that is a less scalable/less testable method.

Does any have a solution for providing script content outside of the script using readFileFromWorkspace and resolving groovy variables within the script?

-milki

Daniel Spilker

unread,
Oct 26, 2015, 4:35:01 AM10/26/15
to job-dsl...@googlegroups.com
I have seen that people use MessageFormat or String.format to create script template. But that usually ends up in an escaping hell. For example for GString you would have to escape all dollar signs, so every shell variable you use in a script. And you probably have to escape backslashes as well and if you need to escape the backslashes in a normal script, e.g. in grep, you have to add double escaping.

So, in my point of view, using environment variable is a nice and clean solution to pass variables to script. Another solution would be to add DSL support for the Managed Script Plugin (https://wiki.jenkins-ci.org/display/JENKINS/Managed+Script+Plugin), because that allows to pass parameters to scripts.

Daniel


--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/bf90b74c-a68f-45da-816c-5e8f9f2498a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Harmel-Law

unread,
Oct 26, 2015, 4:50:14 AM10/26/15
to job-dsl...@googlegroups.com
Thanks Daniel,

+1 for supporting the managed script plugin.  I might actually get round to implementing this myself (I'll post back here if I go start).  We have this requirement at the moment ourselves...

Cheers, Andrew

Daniel Spilker

unread,
Oct 26, 2015, 12:36:47 PM10/26/15
to job-dsl...@googlegroups.com
I have this on my todo list for ages and it's a bit tricky to get the managed scripts created with the DSL, so I started working on this:

https://github.com/jenkinsci/job-dsl-plugin/pull/648

Daniel

milki milk

unread,
Oct 27, 2015, 2:05:58 AM10/27/15
to job-dsl-plugin
On Monday, October 26, 2015 at 1:35:01 AM UTC-7, Daniel Spilker wrote:
I have seen that people use MessageFormat or String.format to create script template. But that usually ends up in an escaping hell. For example for GString you would have to escape all dollar signs, so every shell variable you use in a script. And you probably have to escape backslashes as well and if you need to escape the backslashes in a normal script, e.g. in grep, you have to add double escaping.

Indeed, if I inline my scripts as a GString, there is escaping and double escaping.
 
So, in my point of view, using environment variable is a nice and clean solution to pass variables to script. Another solution would be to add DSL support for the Managed Script Plugin (https://wiki.jenkins-ci.org/display/JENKINS/Managed+Script+Plugin), because that allows to pass parameters to scripts.

This sounds nice, but we have all the information in the groovy script already to be able to generate the script directly. Requiring a Jenkins plugin to do the variable interpolation outside of the script seems less than ideal.

With that said, both EnvInject and Managed Script do fulfill the need. EnvInject can provide variables for the entire build while Managed Script can provide parameters when the script is called. The easiest route is EnvInject, which is my current workaround.

Thanks for the input,
milki
Reply all
Reply to author
Forward
0 new messages