Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline, bash, and pipes

I have an output string that I want to run a 'tr' and 'jq' command against. Piping makes sense like so, IP= sh(script: "echo $spawnServer | jq .[0] | tr -d '\"'", returnStdout: true) Unfortunately the jenkins pipeline hates pipes, so what I get is

+ tr -d '"'
+ jq '.[0]'
+ echo '[' 172.31.79.253, 'i-0d65b431f18a385d0]'
parse error: Invalid numeric literal at line 1, column 16

Any tips would be great! the only thing I found so far was someone using eval, but that didn't work for me. Any tips would be great!

like image 727
pcort Avatar asked Nov 09 '17 03:11

pcort


People also ask

What are the 3 types of pipelines in Jenkins?

There are two types of pipelines in Jenkins: Declarative. Scripted.

Does Jenkins use bash?

If you go to Manage Jenkins --> Configure System you will find an option (called "Shell executable") to set the name or absolute path to the shell that you want your shell scripts to use... For my system without configuring this option... it uses bash!

Can we run multiple pipelines in Jenkins?

A Jenkins pipeline can have multiple stages and each stage will be executed by a single agent, all running on a single machine or multiple machines. A pipeline is normally created for a specific branch of source code.


1 Answers

Instead of struggling with quotes and escapes, you could use def, as in:

def command = $/"echo ${spawnServer} | jq .[0] | tr -d '\"'"/$

res = sh(returnStdout: true, script: command).trim()
sh("echo ${res}")
like image 155
VonC Avatar answered Oct 06 '22 08:10

VonC