I stumbled 
on 
https://stackoverflow.com/questions/55423036/groovy-string-interpolation-when-string-is-defined-before-the-interpolated-varia

and I found this snippet worked just fine after I approved the signature in 
jenkins 

import groovy.text.SimpleTemplateEngine

node("docker") {
    stage("eval") {
        def pathname = "/srv/\${host}/foo/bar"

        def templateMessage = new 
SimpleTemplateEngine().createTemplate(pathname)

        def result = { host -> templateMessage.make(host: host)}
        def HOST = "myhost"

        println(result(HOST))
    }
}




On Friday, October 23, 2020 at 2:28:57 PM UTC-4 cw wrote:

> Thank you, it seems that you're right, it's not completing the expansion, 
> but in the research I did, I wasn't able to make anything else work (one 
> suggestion had to do with double quotes instead of single quotes, but 
> wasn't able to get that to work either).  I don't know if it's related to 
> it being a parameter that was specified at build this is not allowing it to 
> expand?
>
> I looked at the link you sent but didn't really understand "using a 
> closure" as the answer states.  I tried to imitate it using this code below 
> but it still didn't expand:
>
> def fp = { filepath -> "${filepath}" }
> println fp(filepath)
>
> On Friday, October 23, 2020 at 12:13:07 PM UTC-4 [email protected] 
> wrote:
>
>> I think you need to do an "eval" or the equivalent to filepath in order 
>> to expand the inner variable. I believe the ssh command is executing "ls 
>> /srv/jboss/server/${MAL}/log/" but MAL on the far end is empty. So you need 
>> to complete the expansion before calling ssh. 
>>
>> This might get you on the right track -- 
>> https://stackoverflow.com/questions/28997518/groovy-string-interpolation-with-value-only-known-at-runtime
>>
>> On Thursday, October 22, 2020 at 12:14:06 PM UTC-4 cw wrote:
>>
>>> I'm trying to set up a string parameter which will reference a variable 
>>> which will be created within the pipeline.  For instance, when building a 
>>> parameterized build, the user enters a file path in this 
>>> format: /srv/jboss/server/${MAL}/log/
>>>
>>> Where MAL is assigned via the pipeline and then I try to ls -ltr the 
>>> fully qualified path after MAL has been expanded.  However I'm not getting 
>>> it to expand; wondering what I'm doing wrong.
>>>
>>> node {
>>> MALarray = params.multi.split(',')
>>>
>>> MALarray.each { MAL ->
>>>          
>>> if (MAL in ['care''] )  {
>>>             servers = ['xx.xx.xx.xx','xx.xx.xx.xx']
>>>         }
>>>
>>> def soc_remote = [:]
>>> soc_remote.name = "${MAL} SOC"
>>> soc_remote.allowAnyHosts = true
>>>
>>> withCredentials([usernamePassword(credentialsId: 'Credentials', 
>>> usernameVariable: 'USER_ID', passwordVariable: 'USER_PASSWORD')]) {
>>> soc_remote.user = USER_ID
>>> soc_remote.password = USER_PASSWORD
>>> servers.each { server ->
>>>             soc_remote.host = server
>>> stage("Run script on ${server}") {
>>>     try {
>>>     echo("MAL is ${MAL}")
>>>     sshCommand remote: soc_remote, command: "ls -ltr ${filepath}"
>>>     } catch (err) {
>>>                           echo "something failed"
>>>                           echo "${err}"
>>>                           }
>>> }
>>> }
>>>         }
>>> }
>>> } 
>>>
>>> Console output:
>>> *11:59:34* MAL is care 
>>> *11:59:34* [Pipeline] sshCommand 
>>> <https://abaapvsc01amcba.tdbfg.com:9443/job/CraigTest/job/variable%20inside%20variable/2/console#>
>>>  
>>> *11:59:34* Executing command on care SOC[xx.xx.xx.xx]: ls -ltr 
>>> /srv/jboss/server/${MAL}/log/ sudo: false 
>>> *11:59:35* ls: cannot access /srv/jboss/server//log/: No such file or 
>>> directory 
>>> *11:59:35* Failed command care SOC#1002 with status 2: ls -ltr 
>>> /srv/jboss/server/${MAL}/log/  
>>>
>>>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/9c32e793-e83d-451a-b001-2ec7df3cb83an%40googlegroups.com.

Reply via email to