简体   繁体   中英

Passing Jenkins environment variable with spaces to shell script

I am unable to properly pass a variable with spaces in Jenkinsfile to shell command

I have tried using quotes (double and single) backspaces and various other combination. Jenkins will treat the string like as 2 item and single quoted them.

The variable in question in the Jenkins file.

MYTIME = 2022-01-02 03:04:05

In Jenkinsfile

    stage('list') {
      steps{
        sh script:'ansible-playbook -i ./hosts.ini ./ping_playbook.yml -e time=\\"$TIME\\" --limit ${ENV}', label: "ping test"
      }
    }

Jenkins will run it as

ansible-playbook -i ./lzhxjp-test-update/lzhxjp/hosts.ini .ping_playbook.yml -e 'time="2022-01-02' '11:22:33"' --limit sdktest

How do I put it so that Jenkins interpret it as -e time="2022-01-02 11:22:33"

Inside single-quoted strings no variables are expanded by Jenkins (Groovy to be exact).

Single-quoted strings are plain java.lang.String and don't support interpolation. https://groovy-lang.org/syntax.html#all-strings

Thus it is passed directly into the shell and therefore already subject to shell quoting, you can remove the \\ before the " and then the shell the will expand "$TIME" correctly.

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