I am currently trying to do the following inside of a declarative pipeline in Jenkins. I have a certificate in a file and I am trying to put this in a kubernetes secret. Now my problem is the following.
As the file contains newlines passing it as a parameter to kubectl will end the input right there and the command will fail. So I need to escape the newlines. My approach is the following
- cat the file and pipe it to
- base64
- place \n instead of newlines.
Now in the shell this works perfectly fine with
cat ./certs/mycert.pem | base64 | sed ':a;N;$!ba;s/\n/\\n/g'
However trying to use it in the Jenkinsfile as
sh(script: "cat ./certs/key.pem | base64 | sed ':a;N;$!ba;s/\n/\\n/g'", returnStdout: true);
will fail with the error
illegal string body character after dollar sign;
ok this one is easy. I can escape the dollar sign but unfortunately things don't end here. I will then get the error
sed: -e expression #1, char 10: unterminated `s' command
and I can go on and try to escape everything twice which will make sed stopping to fail but the output then has no \n but just regular newlines again.
Any ideas how to use this regex properly inside of my sh method?