0

I have the following groovy code in my jenkins pipeline job:

stage 'Get ddplist file for rel:' + item            
myUrl ='http://mygitrepo.net/cgit/testing.git/plain/' +item + '.ddplist '
def data = new URL(myUrl).getText()
echo data

That returns the following output:

Entering stage Get ddpfile for rel:20170815.2 Proceeding [Pipeline] echo source=http://mygitrepo.net/cgit/repo1/snapshot/source-v20170815.2.tar.bz2 ltp=http://mygitrepo.net/cgit/repo2.git/snapshot/ltp-v20170815.1.tar.bz2 car=http://mygitrepo.net/cgit/repo3.git/snapshot/car-v20170815.1.tar.bz2

Question

How can I "query" this string to find out what the source file is or the ltp file?

What I've tried so Far

I tried adding the following two lines of code, like this:

 def mymap = data.split("\n").collect{ it.split("=", 2) }.collectEntries()
 echo mymap

But I'm getting the error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getText java.net.URL
    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:190)

Any suggestions would be appreciated.

EDIT 1

SO while the answer I chose below technically worked in the sense that it displayed the echo results.. I wasn't actually able to use the data anywhere because of serialization errors. Specifically I was getting the following output from jenkins:

[ssh-agent] Started.
[Pipeline] {
[Pipeline] echo
SRC: http://mygitrepo.net/cgit/repo1/snapshot/abc.tar.bz
[Pipeline] echo
INV: http://mygitrepo.net/cgit/repo2/snapshot/def.tar.bz2
[Pipeline] echo
CTG: testvalue
[Pipeline] echo
ANSIBLE_HOST: 10.1.1.1
[Pipeline] echo
ANSIBLE_DIR: /etc/ansible/mytestdirectory
[Pipeline] stage (Copy new code to Ansible)
Entering stage Copy new code to Ansible
Proceeding
[Pipeline] sh
[workspace] Running shell script
[Pipeline] }
[Pipeline] }
[Pipeline] End of Pipeline
java.io.NotSerializableException: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream

using the following code:

        myUrl = "http://mygitrepo/cgit/testing.git/plain/" + item + ".dpplist"
        // Requires script approval
        def inStream = new URL(myUrl).openStream() //open up the dpplist for this release
        def prop = new Properties()
        prop.load(inStream) //create a properties object out of it.
        src= prop.src
        inv= prop.inv
        ctg=prop.ctg
        sshagent(['johndoe']) {
                rbdbox(ANSIBLE_HOST,ANSIBLE_DIR, src, inv, ctg)
         } 

def rbdbox(ANSIBLE_HOST,ANSIBLE_DIR, SRC, INV, CTG) {

    echo "SRC: ${SRC}"
    echo "INV: ${INV}"
    echo "CTG: ${CTG}"
    echo "ANSIBLE_HOST: ${ANSIBLE_HOST}"
    echo "ANSIBLE_DIR: ${ANSIBLE_DIR}"

    stage 'Copy new code to Ansible'
        sh "ssh -A root@${ANSIBLE_HOST} 'rm -rf ${ANSIBLE_DIR}/*'"
}

So to get it to work i had to change the code to

        myUrl = "http://mygitrepo.net/cgit/testing.git/plain/" + item + ".ddplist "
        // Requires script approval            
        def prop = new Properties() 
        prop.load(new URL(myUrl).openStream())

And then everything started to work. In other words i'm not defining instream.

dot
  • 14,928
  • 41
  • 110
  • 218
  • What you think? `def mymap = data.split("\n").findAll { it.contains("=") }.collectEntries{ [(it.split("=")[0]): it.split("=")[1]] }` – fsi Aug 29 '17 at 19:52
  • how would I then access the properties / map? – dot Aug 29 '17 at 20:09
  • `​mymap​.source​` or `​mymap​.ltp​` or `​mymap​.car​` – fsi Aug 29 '17 at 20:13
  • Well I didnt notice, but I checked on my groovy console, you might change like this: `def mymap = data.split("\n")[0].split(" ").findAll { it.contains("=") }.collectEntries{ [(it.split("=")[0]): it.split("=")[1]] } ` – fsi Aug 29 '17 at 20:17
  • https://stackoverflow.com/questions/38276341/jenkins-ci-pipeline-scripts-not-permitted-to-use-method-groovy-lang-groovyobject – daggett Aug 29 '17 at 21:00
  • dot, have you got the chance to try the solution? – Rao Aug 30 '17 at 14:36

1 Answers1

0

Here in this case, need to convert it as Properties which would be more simple.

Here you go:

def myUrl ='http://mygitrepo.net/cgit/testing.git/plain/' +item + '.ddplist '
def inStream = new URL(myUrl).openStream()

def prop = new Properties()
prop.load(inStream)

//Now you should be able to query easily
println prop.source
Rao
  • 20,781
  • 11
  • 57
  • 77
  • btw. I had to actually reduce the code because of serialization errors. Please see "EDIT 1" section in my original post. – dot Aug 30 '17 at 19:32