Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins pipeline: multiline shell commands with pipe

I am trying to create a Jenkins pipeline where I need to execute multiple shell commands and use the result of one command in the next command or so. I found that wrapping the commands in a pair of three single quotes ''' can accomplish the same. However, I am facing issues while using pipe to feed output of one command to another command. For example

   stage('Test') {       sh '''          echo "Executing Tests"          URL=`curl -s "http://localhost:4040/api/tunnels/command_line" | jq -r '.public_url'`          echo $URL          RESULT=`curl -sPOST "https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=$URL" | jq -r '.code'`          echo $RESULT       '''    } 

Commands with pipe are not working properly. Here is the jenkins console output:

+ echo Executing Tests Executing Tests + curl -s http://localhost:4040/api/tunnels/command_line + jq -r .public_url + URL=null + echo null null + curl -sPOST https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=null 
like image 310
Mandeep Singh Avatar asked Apr 17 '17 13:04

Mandeep Singh


People also ask

How do I comment multiple lines in Jenkins pipeline?

Block Comments. Block comments in Jenkinsfile are used to comment out a block of code. Again, the pattern is similar to Java and C++. A block comment starts with a forward-slash followed by an asterisk (/*) and ends with an asterisk followed by a forward-slash (*/).

Can a stage have multiple steps?

Stages in Declarative Pipeline may have a parallel section containing a list of nested stages to be run in parallel. Note that a stage must have one and only one of steps, stages, parallel, or matrix.


2 Answers

I tried entering all these commands in the jenkins snippet generator for pipeline and it gave the following output:

sh '''         echo "Executing Tests"          URL=`curl -s "http://localhost:4040/api/tunnels/command_line" | jq -r \'.public_url\'`          echo $URL          RESULT=`curl -sPOST "https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=$URL" | jq -r \'.code\'`          echo $RESULT ''' 

Notice the escaped single quotes in the commands jq -r \'.public_url\' and jq -r \'.code\'. Using the code this way solved the problem

UPDATE: : After a while even that started to give problems. There were certain commands executing prior to these commands. One of them was grunt serve and the other was ./ngrok http 9000. I added some delay after each of these commands and it solved the problem for now.

like image 153
Mandeep Singh Avatar answered Sep 21 '22 00:09

Mandeep Singh


The following scenario shows a real example that may need to use multiline shell commands. Which is, say you are using a plugin like Publish Over SSH and you need to execute a set of commands in the destination host in a single SSH session:

stage ('Prepare destination host') {   sh '''       ssh -t -t user@host 'bash -s << 'ENDSSH'       if [[ -d "/path/to/some/directory/" ]];       then           rm -f /path/to/some/directory/*.jar       else           sudo mkdir -p /path/to/some/directory/           sudo chmod -R 755 /path/to/some/directory/           sudo chown -R user:user /path/to/some/directory/       fi ENDSSH'      ''' } 

Special Notes:

  • The last ENDSSH' should not have any characters before it. So it should be at the starting position of a new line.
  • use ssh -t -t if you have sudo within the remote shell command
like image 29
Ashan Priyadarshana Avatar answered Sep 23 '22 00:09

Ashan Priyadarshana