简体   繁体   中英

Jenkins - Linux script - Extracting a variable from XML

I have an XML file like shown below(XML response will be always same). I need to extract sessionToken value and use it in Jenkins file.

XML file -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XmlResponse><httpCode>200</httpCode><httpStatus>OK</httpStatus><action>None</action><messageLevel>INFO</messageLevel><objectsList>{"sessionToken":"1234567890"}</objectsList><results/></XmlResponse>

This is the code I tried in Jenkins and didn't work -

def var=sh([returnStdout: true, script: '`cat output.xml | cut -f10 -d"\\""`'])

println ("var is" + var)

Here is the output I see in Jenkins console log -

++cat output.xml
+ 1234567890  -----> session Token is extracted in this step but for some reason it assume this as command
/workspace/script.sh: line 1: 1234567890: command not found

Answer

Depending on the version of Jenkins you are running, pipe can be an issue. Please try the following solution

def var=sh([returnStdout: true, script:'/bin/bash -c \'`cat output.xml | cut -f10 -d"\\""`\''])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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