How to pass a parameter which includes a single quote in it from Jenkins to PowerShell? Unterminated quoted string

Viewed 25

In one of the stages in a Jenkins pipeline, I need to pass the values from parameters to a script. Sometimes these values may contain a single quote sign which disrupts the run and doesn't come through to the script.

sh "pwsh -file script.ps1 '${params.NAME}' '${params.DESCRIPTION}' '${params.SOMETHING}'"

Ie when the script is called in the stage, the passed parameters may look like this:

'Name' 'Somebody's description' 'Something' 

As you can see it messes with the arguments being passed on. It produces the error of an unterminated quoted string. I need to pass only 3 args to the script as the params suggest.

Any ways to solve this? Should I add another stage before calling the script that formats the values of the params in a way that is acceptable to pass to the PowerShell script? If so, how should I format it? Is there any other way to solve it?

1 Answers

bash's builtin printf has a %q format string that will insert quotes for you. You might try something like:

sh -c "$(printf "%q " pwsh -file script.ps1 "${NAME}" "${DESCRIPTION}" "${SOMETHING}")"

but I'm not sure at which point the params gets expanded. This sort of thing is inherently fragile, though, and you should find a better way.

Related