简体   繁体   中英

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

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"

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}"
            """  
        }
    }

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}'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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