I have the following array which I would like to access the key-value pairs in my Jenkinsfile.
[
{
"AppName": "app1",
"version": "1.0.0.1"
},
{
"AppName": "app2",
"version": "1.0.0.2"
},
{
"AppName": "app3",
"version": "1.0.0.3"
}
]
I would like to get the app name and version and download the applications from my repository via a curl command. I already tried this solution but doesn't work. I get the error on my jenkins console hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String
. I don't know what else I am doing wrong. My plan is to download the apps with following URL for each app name and version from the array.
sh "curl https://example.com/${AppName}/${version}/${AppName}-${version}.zip -O"
Here is my jenkinsfile
node {
stage ('checkou') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [[path: 'projectA/testProjectfile.json']]]], submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://myrepository.com/apps/projectA.git']]])
}
stage ('read file') {
def data = readFile(file: "${WORKSPACE}/projectA/testProjectfile.json")
echo "$data"
def list = new JsonSlurper().parseText( data )
list.each { println it }
}
stage ('download app') {
sh "curl https://example.com/${AppName}/${version}/${AppName}-${version}.zip -O"
}
}
That is my Jenkinsfile and here is the log when I run the pipeline
[Pipeline] echo
[
{
"AppName": "app1",
"version": "1.0.0.1"
},
{
"AppName": "app2",
"version": "1.0.0.2"
},
{
"AppName": "app3",
"version": "1.0.0.3"
}
]
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)
at net.sf.json.groovy.JsonSlurper.parseText(JsonSlurper.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)