修改Jenkins描述构建

时间:2013-07-24 16:03:41

标签: bash curl jenkins jenkins-api

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

1 个答案:

答案 0 :(得分:2)

我玩了很久了...

首先,而不是这样做:

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

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

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

使用printf,我在说明行分隔符中添加了<CR><LF>而不只是<LF>。这样,我就没有<NL><CR><NL>的混乱,我不再依赖于操作系统对换行符的定义。

sed命令花了很长时间才弄明白。我尝试过各种各样的事情:

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

但是,似乎没有任何效果......我尝试了-E标志,它允许我使用扩展正则表达式,但它一直将\r\n解释为替换{ {1}} \\r\\n

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

literal 'rn

那很有效!您通常使用sed单引号来保护正则表达式不被插值。但是,单引号也将old_description=$(sed "s/\\r\\n/\r\n/g" <<<$old_description) 的插值视为\r\n。用双引号改变它们解决了这个问题。