一尘不染

修改构建的Jenkins描述

jenkins

我想 远程 更改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

如果我是第一次添加新描述,则可以在詹金斯中得到:

<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中


阅读 297

收藏
2020-07-25

共1个答案

一尘不染

我玩了很长时间…

首先,不要这样做:

    new_description="$new_description<br/>
$old_description"

追加或添加该行,我使用了printf

new_description="$(printf "$new_description\r\n$old_description")"

通过使用printf,我在广告内容描述分隔符中放置了a
<CR><LF>而不是一个<LF>。这样,我就不会感到混乱,<NL>并且<CR><NL>不再依赖于操作系统对换行符的定义。

sed命令花了我很长一段时间才能弄清楚。我尝试了各种事情:

old_description=$(sed 's/\\r\\n/\r\n/g' <<<$old_description)

但是,似乎没有任何工作…我尝试了-E允许我使用 扩展的 正则表达式的标志,但该标志始终解释\r\n为替换\\r\\nliteral 'rn

经过几个小时的处理,我终于尝试使用双引号而不是单引号:

old_description=$(sed "s/\\r\\n/\r\n/g" <<<$old_description)

可行!通常,您使用带sed的单引号来保护正则表达式免于插值。但是,单引号也杀死了\r\nas 的插值<CR><LF>。用双引号更改它们可以解决问题。

2020-07-25