我想远程更改 Jenkins 构建描述。除了一个小问题:多行描述外,我的脚本已全部设置并准备就绪。
我在 Jenkins 中使用 REST API 和 JSON 来下载旧的描述:
old_description=$(curl -s --user "$USER:$PASSWORD" --data-urlencode "tree=description" \
"$jenkins_url/job/$job_name/$build_number/api/json")
old_description=${old_description#*:\"} #Remove JSON garbage
old_description=${old_description%\"\}} #Remove JSON garbage
`curl 命令拉出:
<font color=blue><b>At first you don't succeed. Try again</b></font><br/>
\r\n<font color=gold><b>At first you don't succeed. Try again</b></font><br/>
\r\n<font color=green><b>At first you don't succeed. Try again</b></font>
(注意:我添加了换行符以使上述内容更易于阅读。这是作为单行拉出的)。
这些\r\n
是单独的行,所以我这样做:
old_description=$(sed 's/\\r\\n/\
/g' <<<$old_description)
这将变为$old_description
:
font color=blue><b>At first you don't succeed. Try again</b></font><br/>
<font color=gold><b>At first you don't succeed. Try again</b></font><br/>
<font color=green><b>At first you don't succeed. Try again</b></font>
(注意:新行是值的一部分。这是一个三行描述。)
我的程序(取决于命令行参数)可以在构建中替换、附加或添加新的描述:
if [ "$prepend_flag" -a -n "$old_description" ] #Prepend new description to old description
then
new_description="$new_description<br/>
$old_description"
elif [ "$append_flag" -a -n "$old_description" ] #Append new description to old description
then
new_description="$old_description<br/>
$new_description"
fi
现在,我将重做描述:
if curl -u $USER:$PASSWORD --data-urlencode "description=$new_description" \
--data-urlencode "Submit=Submit" \
"$jenkins_url/job/$job_name/$build_number/submitDescription"
then
echo "Description successfully changed on Build #$build_number in Jenkins job $job_name"
else
echo "WARNING: Description was not set. Manually change the descripiton of the build"
echo " for Build #$build_number in Jenkins job $job_name"
fi
如果我是第一次添加或添加新描述,我会在 Jenkins 中得到:
<font color=blue><b>At first you don't succeed. Try again</b></font><br/>
<font color=gold><b>At first you don't succeed. Try again</b></font><br/>
<font color=green><b>At first you don't succeed. Try again</b></font><br/>
<font color=red><b>My new description</b></font><br/>
看起来不错。下一次,它不起作用。我明白了:
<font color=blue><b>At first you don't succeed. Try again</b></font><br/>\n<font color=gold><b>At first you don't succeed. Try again</b></font><br/>\n<font color=green><b>At first you don't succeed. Try again</b></font><br/>\n<font color=red><b>My new description</b></font><br/>
<font color=blue><b>My new new description</b></font>
注意\n
出现。
我该如何解决这个问题?
我已将整个程序放在pastebin中。