Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed inside jenkins pipeline

I am trying to run the below in jenkins and i get error any suggestions?

     sh ''' sed -i \':a;N;$!ba;s/\\n/\\|\\#\\|/g\' ${concl} '''

Error - Why isnt the ${concl} being repalced with filename inside the shell script?

   + sed -i ':a;N;$!ba;s/\n/\|\#\|/g'
    sed: no input files
like image 707
DevOops Avatar asked Feb 08 '18 20:02

DevOops


2 Answers

I would suggest running bash command in double quotes and escape $ and \ character. Consider following Jenkins pipeline exemplary script:

#!/usr/bin/env groovy

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Inital content of temp.txt file'
                sh 'cat temp.txt'

                sh "sed -i ':a;N;\$!ba;s/\\n/\\|\\#\\|/g' temp.txt"

                echo 'Content of temt.txt file after running sed command...'
                sh 'cat temp.txt'
            }
        }
    }
}

The temp.txt file I use in this example contains:

lorem ipsum
dolor sit amet

12 13 14

test|test

When I run it I get following console output:

Started by user admin
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Inital content of temp.txt file
[Pipeline] sh
[test-pipeline] Running shell script
+ cat temp.txt
lorem ipsum
dolor sit amet

12 13 14

test|test

[Pipeline] sh
[test-pipeline] Running shell script
+ sed -i :a;N;$!ba;s/\n/\|\#\|/g temp.txt
[Pipeline] echo
Content of temt.txt file after running sed command...
[Pipeline] sh
[test-pipeline] Running shell script
+ cat temp.txt
lorem ipsum|#|dolor sit amet|#||#|12 13 14|#||#|test|test|#|
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

After running the script temp.txt file changes its contents to:

lorem ipsum|#|dolor sit amet|#||#|12 13 14|#||#|test|test|#|

Hope it helps.

like image 127
Szymon Stepniak Avatar answered Nov 07 '22 16:11

Szymon Stepniak


This is not related to sed but to string interpolation in Groovy. Variables (${variable}) will not be replaced within single-quoted strings, only within double-quoted ones.

Thus, replace sh ''' ... ''' with sh """ ... """ or maybe just with sh ".." as you have only one line or maybe with some Groovy/Java call.

like image 35
StephenKing Avatar answered Nov 07 '22 15:11

StephenKing