简体   繁体   中英

Grepping output from JenkinsFile ConsoleText

I am trying to grep some output from a JenkinsFile and assign it a variable.

def AMI_ID = sh(script: 'curl https://jenkins-xxxx.com/job/root-image/job/consoleText | grep -o "ami-[0-z]*" | tail -1')
echo "${AMI_ID}"

I ran this locally on my MAC and it returns the output I expect. However I am guessing it may be something with groovy which is causing this to not work as it is returning null.

Also just to note I am using curl here to just grab an old build that has an ami for testing. I ideally want to run a Jenkins job and then grab the AMI id from that current build with something like this curl ${BUILD_URL}consoleText so maybe there is a better way than using curl.

To assign the output of a shell command, you should use it like this:

def dirList = sh script: "ls -lhtr", returnStdout: true
print(dirList)

Best regards!

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