Home > other >  How to pass a string parameter which contains spaces into the shell script in a Jenkinsfile
How to pass a string parameter which contains spaces into the shell script in a Jenkinsfile

Time:11-15

I have a string parameter in my Jenkinsfile which contains a space

parameters { string(name: 'KW_Issue_resolution', defaultValue: 'Not a Problem', description: 'Marking the issue as Not a problem') }

I am trying to pass this parameter into a shell script within a stage

stage ('Mark KW issues as not a problem') {
            
            steps {
                sh "kwcheck set-status ${params.KW_Issue_IDs} --status ${params.KW_Issue_resolution}"  
            }
         }

However, the shell doesn't recognize the entire string as "Not a Problem"

  kwcheck set-status 190 --status Not a Problem
Cannot change status, 'Not' is not a valid status name

Expected the shell command to be kwcheck set-status 190 --status "Not a Problem"

CodePudding user response:

You could use triple quotation marks in Groovy syntax.

So in triple quotation marks, you can use " and ' like a native bash script.

    stage ('Mark KW issues as not a problem') {    
        steps {
            sh """#! /bin/bash
                kwcheck set-status "${params.KW_Issue_IDs}" --status "${params.KW_Issue_resolution}"
            """  
        }
    }

CodePudding user response:

Since the variable is interpolated in the Groovy interpreter with whitespace, the shell interpreter within the sh step method will not resolve it as a single argument string due to the delimiter. You can cast it as a literal string in the shell interpreter with the normal syntax:

sh "kwcheck set-status ${params.KW_Issue_IDs} --status '${params.KW_Issue_resolution}'"
  • Related