Hi -

Yes, I have done this JSON parse. Example on my Github:
https://github.com/macg33zr/jenkins-experimental-pipelines/blob/master/json-parse-pipeline.groovy

It is a bit ugly as it needs script approvals (or turn off pipeline sandbox 
or put the code in a trusted global pipeline library).

You need a NonCPS method like this:

import groovy.json.JsonSlurperClassic


@NonCPS
def parseJsonToMap(String json) {
    final slurper = new JsonSlurperClassic()
    return new HashMap<>(slurper.parseText(json))
}


Another way to go would be a command line tool to parse JSON like this one:


https://stedolan.github.io/jq/


Then just run a 'sh' command to get the data out. I haven't used that myself.


Have fun!

--Bill


On Thursday, 9 March 2017 21:51:20 UTC, jeremy....@wonderful.fr wrote:
>
> Hi Bill.
>
> I've tried many things with no luck.
>
> So I've got this JSON credential file whose content looks like this:
>
> {
>   "sshUserKey":"sshuserval",
>   "sshHostKey":"sshhostval"
> }
>
>
> I've successfully managed to open this file in my pipeline:
>
> withCredentials([file(credentialsId: 'secrettest', variable: 
> 'testMasterCred')]) {
>     sh "cat ${testMasterCred}";
> }
>
>
> The cat command shows effectively the content of the JSON file.
>
> Then, how would you parse this JSON?
> I've tried readJSON file: $testMasterCred; but this doesn't work, and 
> throws the following message: No such property: $testMasterCred for class: 
> groovy.lang.Binding
>
> I've got the feeling that I'm not very far from the truth.
> There's not much help on the pipeline-utility-steps-plugin readme 
> regarding this.
>
> Would you have an example about how I could get the parsing right?
>
> Thanking you.
>
> Regards.
>
> Le jeudi 9 mars 2017 10:06:44 UTC+1, Bill Dennis a écrit :
>>
>> It can be any format file you like XML, properties, txt whatever you need 
>> for some sort of configuration (except large binary files I guess). 
>>
>> There is a CloudBees article here that should help:
>>
>> https://support.cloudbees.com/hc/en-us/articles/203802500-Injecting-Secrets-into-Jenkins-Build-Jobs
>>
>> The article shows creating these globally but you can create them scoped 
>> on a folder.
>>
>> Then to use in the pipelines I suggest to drop into the pipeline syntax 
>> link on  a pipeline job that drops into the snipper generator in the 
>> Jenkins pipeline UI and go through the 'withCredentials' snippet generator. 
>> It found it best to experiment around a bit to figure it out.
>>
>> --Bill
>>
>>
>> On Thursday, 9 March 2017 08:41:23 UTC, jeremy....@wonderful.fr wrote:
>>>
>>> Hi Bill.
>>>
>>> Thanks so much for your reply.
>>>
>>> I like this credential file option. That would mean I can create a file 
>>> with all the environment variables I need for my branches inside (one per 
>>> branch I guess). And if I could scope it inside my project folder even 
>>> better.
>>>
>>> I've tried to google information about how to use credential files, but 
>>> without much success. Would you have an example of how you'd write one?
>>> Is it a key / value format? bash variables declarations? JSON? XML?
>>>
>>> Thank you for your time and your help.
>>>
>>> Regards.
>>>
>>> Jeremy.
>>>
>>> Le mercredi 8 mars 2017 10:05:02 UTC+1, Bill Dennis a écrit :
>>>>
>>>> Just some other things I thought of -
>>>>
>>>> If you use the credentials file feature you can put all those sensitive 
>>>> properties in a properties file stored as 'jenkins credentials'. 
>>>>
>>>> Then pull that props file into your workspace using 'withCredentials' 
>>>> in the pipeline.
>>>>
>>>> Next thing is to grab the pipeline utility steps plugin which has a 
>>>> readProperties step (it is not one of the standard pipe plugins - you will 
>>>> need to add it).
>>>> https://plugins.jenkins.io/pipeline-utility-steps
>>>>
>>>> Then you have the file properties loaded as Java properties and you can 
>>>> use them as before.
>>>>
>>>> I did this move from Freestyle too and there is a lot to learn but it 
>>>> is worth it. Another recommendation is to look at the declarative pipeline 
>>>> not just scripted pipeline. Declarative has post build handling in the 
>>>> pipeline which you may miss from FreeStyle jobs. In scripted pipeline you 
>>>> have to do a lot of try-catch handling for build errors.
>>>>
>>>> Bill
>>>>
>>>>
>>>> On Wednesday, 8 March 2017 08:45:03 UTC, Bill Dennis wrote:
>>>>>
>>>>> If you put the pipeline / branch jobs inside a folder, you can scope 
>>>>> the credentials to just that folder. Pretty sure that is available in 
>>>>> Jenkins OSS and not just Enterprise - you need the CloudBees Folders 
>>>>> plugin. Have a look on here, it might have some clues: 
>>>>> https://support.cloudbees.com/hc/en-us/articles/204264974-How-inject-your-Maven-settings-xml-at-folder-level-with-the-Credentials-plugin
>>>>>
>>>>> I am not sure if this helps in your branch scenario. I put all my 
>>>>> credentials globally then realised I could scope them to the folder level 
>>>>> - 
>>>>> I missed it due to some nuances in the credentials UI.
>>>>>
>>>>> Bill
>>>>>
>>>>>

-- 
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/e4e7c949-a7db-4316-ba5e-ada3af6bbbc7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to