This is the script that is in my Jenkins build step:
echo "
echo \"==== Tagging this version ====\"
git checkout master
git pull
git tag -a $projectVersion -m \"version $projectVersion\"
git push origin $projectVersion
echo \"==== Cleaning up merged branches ====\"
git branch -r --merged | grep -v \\* | grep -v master | grep -v dmz | grep -v develop | while read line; do git push origin :\${line//origin\\//}; done
echo \"==== Merging release changes into develop and dmz ====\"
git checkout develop
git pull
git merge master
git push origin develop
git checkout dmz
git pull
git merge develop
git push origin dmz
git checkout master
"
The first echo "
and the last "
are trying to spit out whats in the middle as instructions to be run manually, but the $projectVersion
variable substituted in. If I copy that code and paste it into my terminal it runs and prints fine. However, Jenkins is doing something funny with double quote multi-line strings, because the build keeps failing with this:
19:31:30 echo
19:31:30 "==== Tagging this version ===="
19:31:30 Switched to a new branch 'master'
19:31:30 Branch master set up to track remote branch master from origin.
19:31:31 Already up-to-date.
19:31:31 fatal: Failed to resolve '1.0.3"' as a valid ref.
19:31:31 Build step 'Execute shell' marked build as failure
It looks like it is completely ignoring the "
right after the first echo
which it should not be doing.
I read in multiple places that I need to use printf
inside ``
, inside an echo ""
statement, so I tried that but got lost with trying to escape everything:
echo "`printf \"echo \\"==== Tagging this version ====\\"\ngit checkout master\ngit pull\ngit tag -a $projectVersion -m \\"version $projectVersion\\"\ngit push origin $projectVersion\n\necho \\"==== Cleaning up merged branches ====\\"\ngit branch -r --merged | grep -v \\\* | grep -v master | grep -v dmz | grep -v develop | while read line; do git push origin :\\${line//origin\\\//}; done\n\necho \\"==== Merging release changes into develop and dmz ====\\"\ngit checkout develop\ngit pull\ngit merge master\ngit push origin develop\ngit checkout dmz\ngit pull\ngit merge develop\ngit push origin dmz\ngit checkout master\\"`"