詹金斯管道中的sed

Dev*_*ops 4 groovy sed jenkins jenkins-pipeline

我正在尝试在詹金斯(Jenkins)中运行以下内容,但我得到任何建议的错误?

     sh ''' sed -i \':a;N;$!ba;s/\\n/\\|\\#\\|/g\' ${concl} '''
Run Code Online (Sandbox Code Playgroud)

错误-为什么${concl}在shell脚本中不使用文件名重新填充?

   + sed -i ':a;N;$!ba;s/\n/\|\#\|/g'
    sed: no input files
Run Code Online (Sandbox Code Playgroud)

Ste*_*ing 6

这与sedGroovy 中的字符串插值无关。变量 ( ${variable}) 不会在单引号字符串中被替换,只会在双引号字符串中被替换。

因此,替换sh ''' ... '''sh """ ... """或可能只是替换sh ".."为您只有一行,或者可能使用一些 Groovy/Java 调用。


Szy*_*iak 6

我建议在双引号中运行bash命令,并转义$\字符。考虑以下Jenkins管道示例脚本:

#!/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'
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

temp.txt我在此示例中使用的文件包含:

lorem ipsum
dolor sit amet

12 13 14

test|test
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到以下控制台输出:

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
Run Code Online (Sandbox Code Playgroud)

运行脚本temp.txt文件后,将其内容更改为:

lorem ipsum|#|dolor sit amet|#||#|12 13 14|#||#|test|test|#|
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。