-
-
Save Faheetah/e11bd0315c34ed32e681616e41279ef4 to your computer and use it in GitHub Desktop.
node { | |
echo 'Results included as an inline comment exactly how they are returned as of Jenkins 2.121, with $BUILD_NUMBER = 1' | |
echo 'No quotes, pipeline command in single quotes' | |
sh 'echo $BUILD_NUMBER' // 1 | |
echo 'Double quotes are silently dropped' | |
sh 'echo "$BUILD_NUMBER"' // 1 | |
echo 'Even escaped with a single backslash they are dropped' | |
sh 'echo \"$BUILD_NUMBER\"' // 1 | |
echo 'Using two backslashes, the quotes are preserved' | |
sh 'echo \\"$BUILD_NUMBER\\"' // "1" | |
echo 'Using three backslashes still results in only preserving the quotes' | |
sh 'echo \\\"$BUILD_NUMBER\\\"' // "1" | |
echo 'To end up with \" use \\\\\\" (yes, six backslashes)' | |
sh 'echo \\\\\\"$BUILD_NUMBER\\\\\\"' | |
echo 'This is fine and all, but we cannot substitute Jenkins variables in single quote strings' | |
def foo = 'bar' | |
sh 'echo "${foo}"' // (returns nothing) | |
echo 'This does not interpolate the string but instead tries to look up "foo" on the command line, so use double quotes' | |
sh "echo \"${foo}\"" // bar | |
echo 'Great, more escaping is needed now. How about just concatenate the strings? Well that gets kind of ugly' | |
sh 'echo \\\\\\"' + foo + '\\\\\\"' // \"bar\" | |
echo 'We still needed all of that escaping and mixing concatenation is hideous!' | |
echo 'There must be a better way, enter dollar slashy strings (actual term)' | |
def command = $/echo \\\"${foo}\\\"/$ | |
sh command // \"bar\" | |
echo 'String interpolation works out of the box as well as environment variables, escaped with double dollars' | |
def vash = $/echo \\\"$$BUILD_NUMBER\\\" ${foo}/$ | |
sh vash // \"3\" bar | |
echo 'It still requires escaping the escape but that is just bash being bash at that point' | |
echo 'Slashy strings are the closest to raw shell input with Jenkins, although the non dollar variant seems to give an error but the dollar slash works fine' | |
} |
@michaelPf85 Thanks for the explanation!!
But if i test the code in Jenkins job will this code with proper backslashes and escape characters work without giving error in pipeline job
Command:
wp db query "UPDATE wp_options SET option_value='s:${total_word_count}:\\"#https?://(www\\\\.)?(|${WP_ENGINE}.wpengine.com|${WP_ENGINE_CDN}.wpengine.netdna-(ssl|cdn).com)/wp-(content|includes)# => https://${WP_ENGINE}.wpenginepowered.com/wp-\\\$4\n#https?://${WP_ENGINE_CDN}(.wpengine|-wpengine).netdna-(ssl|cdn).com/([^\\\\.]*).pdf# => https://${WP_ENGINE}.wpengine.com/\\\$3.pdf\n#\\"/wp-content/themes/bs/img# => \\"https://${WP_ENGINE}.wpenginepowered.com/wp-content/themes/bs/img\\";' WHERE 1=1 AND option_name='regex_html_post_process'"
FYI: I have also escape '$' with triple backslashes to print $ within the string, will the above code work correctly without syntax error?
While running the query in the job it wont throw this error now correct?
Error: syntax error near unexpected token
('`
@burhanuddin7 Honestly I am not able to know if the code will work correctly. So the only 'advice' I can give you is to test at each step, looking at the command-line generated by Jenkins in the log, and find by yourself for each error where the escaping is wrong.
I know it's a pain but if anyone finds a better solution, I'd love to hear about it, it would save us all a lot of time !
@michaelPf85 the query above is working fine in Jenkins file but i have one small issue, in my rule_one and rule_two variable at he end i need a line break for which i have added '\n' but while compiling and giving final output it does not line break instead it gives me an empty space as output.
Can you please help me with this ? How do i line break my variable in the query so that it gives proper output
Code:::
**rule_one**='\\\\\\"#https?://(www\\\\.)?(|'${WP_ENGINE}'.wpengine.com|'${WP_ENGINE_CDN}'.wpengine.netdna-(ssl|cdn).com)/wp-(content|includes)# => https://'${WP_ENGINE}'.wpenginepowered.com/wp-\\\$4\n' **rule_two**='#https?://'${WP_ENGINE_CDN}'(.wpengine|-wpengine).netdna-(ssl|cdn).com/([^\\\\.]*).pdf# => https://'${WP_ENGINE}'.wpengine.com/\\\$3.pdf\n'
Query::
wp db query "UPDATE wp_options SET option_value='s:120:${rule_one}${rule_two}xyz' WHERE 1=1 AND option_name='regex_html_post_process'"
Can u please help here, I need line break after rule_one and rule_two variables
How to do that in groovy jenkins job
@michaelPf85 can you please help me out in my above query posted
You just need to escape the $
:
pipeline {
environment {
TEST = "It's easy!"
}
stages {
stage('Test') {
steps {
sh 'echo "\$BUILD_NUMBER: \$TEST"'
}
}
}
}
Works as expected:
[Pipeline] sh
+ echo '1: It'\''s easy!'
1: It's easy!
@burhanuddin7 If you are indeed writing your shell script in a Jenkinsfile, you simply CAN NOT test your command locally.
So my recommandation (doubling the backslash) should only be tested in the Jenkins job.
As another solution, to be able to test your command locally, you could extract your command in a separate .sh file, but it adds other challenges, it's not as simple to deploy and run.