代码之家  ›  专栏  ›  技术社区  ›  Ramesh Thiyagarajan

共享库中的sh step内部没有发生字符串插值

  •  0
  • Ramesh Thiyagarajan  · 技术社区  · 3 年前

    下面是我的共享库函数。

    def Printing_output(String name, String company){
        sh (
            'echo "Hi there, this is {name} and am working for {company}"'
        )
    }
    

    下面是我的管道脚本

    @Library('Maven_Build_Pipeline') _
    
    pipeline {
        agent { label "Build-Jenkins-Slave-94" }
        stages {
            stage('Test') {
                steps {
                    script{
                        def name = 'Rajini'
                        def company = 'Amazon'
                        partial_script_test.Printing_output(name, company)
                    }   
                }
            }
        }
    }
    

    Exution后,它没有正确打印参数值,也没有出现任何错误。

    下面是我的jenkins作业输出

    Hi there, this is {name} and am working for {company}
    

    有人能提一下我在这里错过了什么吗?我对共享图书馆的概念不熟悉。

    提前感谢。

    0 回复  |  直到 3 年前
        1
  •  0
  •   sbglasius    3 年前

    你的方法应该是这样的:

    def Printing_output(String name, String company){
        sh (
            "echo \"Hi there, this is ${name} and am working for ${company}\""
        )
    }
    

    正如@daggett所写,你应该使用 GString 不是普通人 String

    推荐文章