1

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

4

1 回答 1

2

我玩这个玩了很久...

首先,不要这样做:

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

附加或前置该行,我使用printf

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

通过使用printf,我在描述行分隔符中放置了 a<CR><LF>而不仅仅是 a <LF>。这样一来,我就没有了<NL>and<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>。用双引号改变它们解决了这个问题。

于 2013-07-24T17:02:40.507 回答