Jenkins shell脚本执行不使用多行双引号

时间:2017-10-25 23:55:04

标签: bash shell jenkins

这是我Jenkins构建步骤中的脚本:

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
"

第一个echo "和最后一个"正试图在中间吐出什么作为手动运行的指令,但$projectVersion变量替换为。如果我复制该代码并将其粘贴到我的终端,它运行并打印正常。然而,Jenkins正在用双引号多行字符串做一些有趣的事情,因为构建一直在失败:

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

它似乎完全忽略"之后的第一个echo它应该正在做的事情。

我在多个地方阅读了我需要在printf内部使用``内部 echo ""语句,所以我尝试了但是在尝试时迷失了逃避一切:

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\\"`"

3 个答案:

答案 0 :(得分:1)

方法一:Heredocs

如果要将一块内容转储到stdout,则heredoc是该作业的正确工具。这个假设您希望$projectVersion替换为该变量的值(但请注意,在代码生成环境中进行此类替换通常会构成安全隐患)。

# generate a shell-quoted version of $projectVersion
# this is safe to use, and *must only be used*, in an unquoted context.
printf -v projectVersion_q '%q' "$projectVersion"

cat <<EOF
echo "==== Tagging this version ===="
git checkout master
git pull
git tag -a $projectVersion_q -m "version "$projectVersion_q
git push origin $projectVersion_q

echo "==== Cleaning up merged branches ===="
git branch -r --merged | while read -r line; do
  case \$line in
    [*]|master|dmz|develop) continue ;;
    *) branch=\${line#origin/}; git push origin ":\$branch";;
  esac
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
EOF

方法二:一个大printf

printf -v projectVersion_q '%q' "$projectVersion"

# Generate an array, one element per line, individually quoted as appropriate for each
lines=(
  'echo "==== Tagging this version ===="'
  'git checkout master'
  'git pull'
  "git tag -a $projectVersion_q -m 'version '$projectVersion_q"
  "git push origin $projectVersion_q"
  ''
  'echo "==== Cleaning up merged branches ===="'
  'git branch -r --merged | while read -r line; do'
  '  case $line in'
  '    [*]|master|dmz|develop) continue ;;'
  '    *) branch=${line#origin/}; git push origin ":$branch";;'
  '  esac'
  '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'
)
printf '%s\n' "${lines[@]}"

答案 1 :(得分:0)

好吧,在我的疏忽中,在示例中的第一行没有结束引号之前,它在一行(未显示)中结果。我关闭它,一切都按预期工作。

话虽如此,this回答值得一读,因为这是一种更好的方式来做我正在做的事情。

答案 2 :(得分:0)

如果第一个echo是一个常规回声(即没有在shell中运行),那么你只需要做三重双引号就可以了:

回声“”“ 打印整体 一堆多线的东西 在这里 “”“