Search code examples
groovyshared-librariesstring-interpolation

String Interpolation is not happening inside sh step in shared library


Below is my share library function.

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

and below is my pipeline script

@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)
                }   
            }
        }
    }
}

After Excution it is not printing the parameters values properly and am not getting any error also.

Below is my jenkins job output

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

Can Someone mention what am i missing here ? Am new to shared library concept.

Thanks in advance.


Solution

  • Your method should be like this:

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

    As @daggett writes, you should use a GString not a regular String