jenkinsgroovystring-interpolation

Jenkins - How to use credentials and parameters within same shell string?


I am currently facing an issue, where I have to use parameters and credentials in the same shell string, which is causing me a lot of trouble.

When handling passwords in Jenkins the documentation emphasizes the use of single quotes to avoid the interpolation of sensitive variables:

Double quote example: enter image description here

Single quote example:

enter image description here

However, using single quotes will result in my parameters not being interpolated as showed in the below picture:

String interpolation

Thus, I can't find a solution which allows my to have both, and as I see it, this leaves me with one of the following options:

  1. I can choose to use single quotes, which result in my credentials being masked but my parameters are not interpolated.
  2. I can choose to use double quotes however, my username and password are no longer masked but my parameters are being interpolated.

Do someone know if it is possible to have both in the same string or know some sort of workaround?


Solution

  • You can use double quotes, but escape the $ for the secret variables like this:

    sh("curl -u \$USERNAME:\$PASSWORD ${url}")
    

    You can also use single quotes:

    sh('curl -u $USERNAME:$PASSWORD ' + url)
    

    You can use it with withCredentials().