将文件内容拆分为键值MAP

时间:2017-08-29 19:12:25

标签: jenkins groovy jenkins-pipeline

我的jenkins管道工作中有以下groovy代码:

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

返回以下输出:

  

进入阶段获取rel的ddpfile: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   汽车= http://mygitrepo.net/cgit/repo3.git/snapshot/car-v20170815.1.tar.bz2

问题

我怎样才能"查询"这个字符串找出源文件是什么或ltp文件?

我迄今为止所做的尝试

我尝试添加以下两行代码,如下所示:

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

但我收到了错误:

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)

任何建议都将不胜感激。

编辑1

虽然我在下面选择的答案在技术上有效,但它显示了回声结果。由于序列化错误,我实际上无法在任何地方使用数据。具体来说,我从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

使用以下代码:

        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}/*'"
}

为了让它工作,我必须将代码更改为

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

然后一切都开始奏效了。换句话说,我没有定义内流。

1 个答案:

答案 0 :(得分:0)

在这种情况下,需要将其转换为Properties,这将更简单。

你走了:

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