简体   繁体   English

将文件内容拆分为键值MAP

[英]split file contents into key value MAP

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 进入阶段获取rel的ddp文件:20170815.2继续[管道]回声源= 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? 如何“查询”此字符串以找出源文件或ltp文件?

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 编辑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: 具体来说,我从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. 换句话说,我没有定义插播广告。

Here in this case, need to convert it as Properties which would be more simple. 在这种情况下,需要将其转换为Properties ,这样会更加简单。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM