variable within a variable

2020-10-22 Thread cw
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 

 
*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 jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/11fcfc93-6e5c-4f50-a637-964e06bf66een%40googlegroups.com.


Re: Automating the Choice Parameter on a job

2020-10-22 Thread Kari Cowan
I couldn't get that to work right but got a little but further using the
JsonSlurper (as opposed to JsonSlurperClassic)

Assuming my data structure is represented in json like:

{
  "name": "templates",
  "tags": [
"qa_tmpl-92",
"qa_tmpl-93",
"qa_tmpl-94"
  ]
}

I was trying something like this:

import groovy.json.JsonSlurper
try {
List artifacts = new ArrayList()
def artifactsUrl = "http://localhost:5000/v2/templates/tags/list";

def artifactsObjectRaw = ["curl", "-s", "-H", "accept:
application/json", "-k", "--url", "${artifactsUrl}"].execute().tags
def jsonSlurper = new JsonSlurper()
def artifactsJsonObject = jsonSlurper.parseText(artifactsObjectRaw)
def dataArray = artifactsJsonObject.data
for(item in dataArray){
if (item.isMetadata== false)
artifacts.add(item.tags)
}
return artifacts
} catch (Exception e) {
print "There was a problem fetching the artifacts"
}

Running in groovysh, it didn't show me an error, but I ended up in the
catch, so I think the way I am trying to do curl, this bit in particular
(${artifactsUrl}"].execute().tags) is wrong.

There was a problem fetching the artifacts ===> null


Can anyone point me in the right direction here?

On Mon, Oct 19, 2020 at 5:07 PM Kari Cowan  wrote:

> Nice. That’s a good tip. Thanks 😊
>
> On Mon, Oct 19, 2020 at 4:36 PM Jan Monterrubio 
> wrote:
>
>> Yep, if docker tags can return you json, you can just slurp stuff and
>> transform it.
>>
>> On Mon, Oct 19, 2020 at 09:27 Kari Cowan  wrote:
>>
>>> This is clever - thanks.
>>>
>>> I don't have a directory of files in my git project- but I am guessing I
>>> could write something to add - like a deployment tag list that matches the
>>> docker registry?
>>>
>>> Ideally I would pull the docker register tag list and use that.
>>>
>>>
>>> On Fri, Oct 16, 2020 at 1:15 PM janmont...@gmail.com <
>>> janmonterru...@gmail.com> wrote:
>>>
  import groovy.json.JsonSlurperClassic
  def jsonSlurper = new JsonSlurperClassic()
 def response = jsonSlurper.parse(new URL("SOME REPO HERE"))
 return response

 It was something like that ^

 You can always start with a "return [ 'a', 'b'] " , we used the
 ActiveChoice reactive parameter


 On Thursday, October 15, 2020 at 11:32:33 PM UTC-5 janmont...@gmail.com
 wrote:

> I’ve used the extended choice parameter to pull a list of files from a
> directory in github and display a set of checkboxes. I’ll remember to grab
> you a snippet tomorrow during work!
>
>
>
> On Thu, Oct 15, 2020 at 17:01 Kari Cowan  wrote:
>
>> I am working on a Jenkins job to rollback a Docker container to the
>> previous version.  This on its own is pretty straight-forward, but what I
>> want to do is get my list of available tags from the registry. As opposed
>> to going to the registry-page and seeing what's available, then 
>> copy/paste
>> a value into a form on Jenkins, I'd like to automate that step somehow - 
>> by
>> grabbing the data from the registry page and displaying the options in a
>> choice parameter in Jenkins on the build screen.
>>
>> For example, call in the registry url by ip, example,
>> 10.11.12.199:5000/v2/some-api/tags/list might display some JSON like:
>> {"name":"some-api","tags":["qa_api-11","qa_api-12","qa_api-13"]}
>>
>> What's the best tool for this?  I'm no Groovy expert but would the
>> Extensible Choice Parameter plugin coupled with a Groovy script be useful
>> here? I'm not really clued up yet if this is possible in Groovy, but am
>> open to suggestions.  Does anyone have any tips on that or other methods
>> that are better?
>>
>>
>>
>> --
>> 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/CAE7w4hg_RpwfVYAh7m9FLc1oxCPZQuqwd-eMpj6kFgBdMsJoOA%40mail.gmail.com
>> 
>> .
>>
> --
 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-users+unsubscr...@googlegroups.com.
 To view this discussion on the web visit
 https://groups.google.com/d/msgid/jenkinsci-users/d99e92f5-f7a8-493c-b90a-997aa6aec02fn%40googlegroups.com
 
 .

>>> --
>>> You received this message b

Re: Automating the Choice Parameter on a job

2020-10-22 Thread Danny Trunk
Hello Kari

As far as I know JsonSlurper isn't Serializable and defining it for re-use
will raise a NotSerializableException and this end up in the catch block.

You could either move the Code insider a @NonCPS annotated method or doing
everything in one statement.

Kari Cowan  schrieb am Do., 22. Okt. 2020, 23:25:

> I couldn't get that to work right but got a little but further using the
> JsonSlurper (as opposed to JsonSlurperClassic)
>
> Assuming my data structure is represented in json like:
>
> {
>   "name": "templates",
>   "tags": [
> "qa_tmpl-92",
> "qa_tmpl-93",
> "qa_tmpl-94"
>   ]
> }
>
> I was trying something like this:
>
> import groovy.json.JsonSlurper
> try {
> List artifacts = new ArrayList()
> def artifactsUrl = "http://localhost:5000/v2/templates/tags/list";
>
> def artifactsObjectRaw = ["curl", "-s", "-H", "accept:
> application/json", "-k", "--url", "${artifactsUrl}"].execute().tags
> def jsonSlurper = new JsonSlurper()
> def artifactsJsonObject = jsonSlurper.parseText(artifactsObjectRaw)
> def dataArray = artifactsJsonObject.data
> for(item in dataArray){
> if (item.isMetadata== false)
> artifacts.add(item.tags)
> }
> return artifacts
> } catch (Exception e) {
> print "There was a problem fetching the artifacts"
> }
>
> Running in groovysh, it didn't show me an error, but I ended up in the
> catch, so I think the way I am trying to do curl, this bit in particular
> (${artifactsUrl}"].execute().tags) is wrong.
>
> There was a problem fetching the artifacts ===> null
>
>
> Can anyone point me in the right direction here?
>
> On Mon, Oct 19, 2020 at 5:07 PM Kari Cowan  wrote:
>
>> Nice. That’s a good tip. Thanks 😊
>>
>> On Mon, Oct 19, 2020 at 4:36 PM Jan Monterrubio 
>> wrote:
>>
>>> Yep, if docker tags can return you json, you can just slurp stuff and
>>> transform it.
>>>
>>> On Mon, Oct 19, 2020 at 09:27 Kari Cowan  wrote:
>>>
 This is clever - thanks.

 I don't have a directory of files in my git project- but I am guessing
 I could write something to add - like a deployment tag list that matches
 the docker registry?

 Ideally I would pull the docker register tag list and use that.


 On Fri, Oct 16, 2020 at 1:15 PM janmont...@gmail.com <
 janmonterru...@gmail.com> wrote:

>  import groovy.json.JsonSlurperClassic
>  def jsonSlurper = new JsonSlurperClassic()
> def response = jsonSlurper.parse(new URL("SOME REPO HERE"))
> return response
>
> It was something like that ^
>
> You can always start with a "return [ 'a', 'b'] " , we used the
> ActiveChoice reactive parameter
>
>
> On Thursday, October 15, 2020 at 11:32:33 PM UTC-5
> janmont...@gmail.com wrote:
>
>> I’ve used the extended choice parameter to pull a list of files from
>> a directory in github and display a set of checkboxes. I’ll remember to
>> grab you a snippet tomorrow during work!
>>
>>
>>
>> On Thu, Oct 15, 2020 at 17:01 Kari Cowan  wrote:
>>
>>> I am working on a Jenkins job to rollback a Docker container to the
>>> previous version.  This on its own is pretty straight-forward, but what 
>>> I
>>> want to do is get my list of available tags from the registry. As 
>>> opposed
>>> to going to the registry-page and seeing what's available, then 
>>> copy/paste
>>> a value into a form on Jenkins, I'd like to automate that step somehow 
>>> - by
>>> grabbing the data from the registry page and displaying the options in a
>>> choice parameter in Jenkins on the build screen.
>>>
>>> For example, call in the registry url by ip, example,
>>> 10.11.12.199:5000/v2/some-api/tags/list might display some JSON
>>> like:
>>> {"name":"some-api","tags":["qa_api-11","qa_api-12","qa_api-13"]}
>>>
>>> What's the best tool for this?  I'm no Groovy expert but would the
>>> Extensible Choice Parameter plugin coupled with a Groovy script be 
>>> useful
>>> here? I'm not really clued up yet if this is possible in Groovy, but am
>>> open to suggestions.  Does anyone have any tips on that or other methods
>>> that are better?
>>>
>>>
>>>
>>> --
>>> 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/CAE7w4hg_RpwfVYAh7m9FLc1oxCPZQuqwd-eMpj6kFgBdMsJoOA%40mail.gmail.com
>>> 
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google
> Groups "Jenki