简体   繁体   English

如何将 grep 和 tr 与管道一起用作 Jenkinsfile 中的 bash 命令?

[英]How do I use grep and tr with pipe as a bash command in a Jenkinsfile?

im using a grep command with tr to access a version from a file which in my current directory.我使用带有 tr 的 grep 命令从我当前目录中的文件访问版本。 The grep command works but not in the context of the Jenkinsfile as a variable. grep 命令可以工作,但不能作为变量在 Jenkinsfile 的上下文中使用。

I have this:我有这个:

            grepVersion='${grep -we "^version:" myconfig.yaml | tr -d "version:[:blank:]"}'         

I want this:我要这个:

myPath='something_"${grepVersion}"'

as this:像这样:

    echo "{$myPath}"
output: something_0.0.2

If tried a lot, would be great if someone could help me with this.如果尝试了很多,如果有人可以帮助我,那就太好了。

my Jenkinsfile looks something like this:我的 Jenkinsfile 看起来像这样:

pipeline {
    stages {
        stage('Build') {
            steps {
                script {
                    
                    echo "build something"
                }
            }
            
        }
        
        stage('Deploy'){
            steps{
                script {
                    
                    #grep version from file 

                    grepVersion='${grep -we "^version:" myconfig.yaml | tr -d "version:[:blank:]"}'                 
        
                    #access archivePath 
                    archivePath="${WORKSPACE}/archives/${env.JOB_NAME}_"${grepVersion}".mtar"
                    
                    #deploy something with arhivePath
                    sh 'XX deploy "${archivePath}" -f'
                }
            }
        }
    }
}

This command works and accesses the correct version:此命令有效并访问正确的版本:

grep -we "^version:" myconfig.yaml |tr -d "version:[:blank:]"

I have the following file in my path where I want to access version: 0.0.2 as 0.0.2 to concatenate this to my archivePath variable.我在我想要访问版本的路径中有以下文件: 0.0.2 as 0.0.2将其连接到我的 archivePath 变量。

ID
Name
version: 0.0.2



different_version: 0.0.2
    
    
different_version: 0.0.3

The following command works on bash shell and can acces the correct version: 0.0.2以下命令适用于 bash shell,可以访问正确的版本: 0.0.2

grep -we "^version:" myconfig.yaml |tr -d "version:[:blank:]"

But it won't work with the Jenkins fileand and groovy context.但它不适用于 Jenkins 文件和常规上下文。 If have tried the following commands to access 0.0.2 to use it as a bash variable :如果尝试使用以下命令访问0.0.2以将其用作 bash 变量:

Here the "^" makes a problem:这里的“^”有问题:

grepVersion=${grep -we "^version:" myconfig.yaml |tr -d "version:[:blank:]"}

Here the commands wont resolve because of the double quotes:由于双引号,这里的命令无法解析:

grepVersion="${grep -we '^version:' myconfig.yaml |tr -d 'version:[:blank:]'"'

... ...

grepVersion='$(grep -we '^version:' myconfig.yaml |tr -d 'version:[:blank:]')"

and so on haven' found a solution yet.等等还没有找到解决方案。

Thanks for your help already.已经感谢您的帮助。

thanks a lot, you both were right!非常感谢,你们都是对的!

The following command works for me:以下命令对我有用:

grepVersion=sh 'sed -n s/^version:[[:blank:]]*//p mta.yaml'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM