即使存在错误,Jenkins 管道阶段构建也是绿色的
Jenkins pipeline stage build is green even though an error is present
我有一个包含三个阶段的 Jenkins 管道:“构建”、“测试”和“部署”。
这是我在使用“构建”时遇到的问题:
构建步骤确保 Control-M Automation API json 文件的结构有效。
为此,我在构建步骤中使用了 Automation API 提供的 $endpoint/build 服务:
stage('Build') {
environment {
CONTROLM_CREDS = credentials('xxx')
ENDPOINT = 'xxx'
}
steps {
sh '''
username=$CONTROLM_CREDS_USR
password=$CONTROLM_CREDS_PSW
# Login
login=$(curl -k -s -H "Content-Type: application/json" -X POST -d \{\"username\":\"$username\",\"password\":\"$password\"\} "$ENDPOINT/session/login" )
token=$(echo ${login##*token\" : \"} | cut -d '"' -f 1)
# Build
curl -k -s -H "Authorization: Bearer $token" -X POST -F "definitionsFile=@ctmjobs/TestCICD.json" "$ENDPOINT/build"
curl -k -s -H "Authorization: Bearer $token" -X POST "$ENDPOINT/session/logout"
'''
}
}
<snip>
一切都按预期工作,但如果我故意在 json 文件中输入错误,Jenkins 会检测到它并在终端中打印错误,但“构建”仍为绿色。任何人都可以识别错误吗?我的期望是,一旦 JSON 文件中出现错误,“构建”阶段就会变为红色。
这是终端的 Jenkins 输出:
+ password=****
++ curl -k -s -H 'Content-Type: application/json' -X POST -d '{"username":"xxx","password":"****"}' /automation-api/session/login
+ login='{
"username" : "xxx",
"token" : "xxx",
"version" : "9.19.200"
}'
++ echo 'xxx",
' '"version"' : '"9.19.200"
' '}'
++ cut -d '"' -f 1
+ token=xxx
+ curl -k -s -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build
{
"errors" : [ {
"message" : "unknown type: Job:Dummmy",
"file" : "Test.json",
"line" : 40,
"col" : 29
}, {
"message" : "unknown type: Job:Dummmy",
"file" : "Test.json",
"line" : 63,
"col" : 29
} ]
}+ curl -k -s -H 'Authorization: Bearer xxx' -X POST /automation-api/session/logout
{
"message" : "Successfully logged out from session xxx"
} ``
Jenkins 为了将一个阶段视为 failed
,它将检查所执行命令的 exit code
,在您的情况下
curl -k -s -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build
问题是 curl as a command
执行成功。
但是 curl 的正文表明 api 调用失败。
您可以将 --fail
标记添加到您的卷曲中。当 response status is > 400
时,这将强制卷曲到 return 和 erroneous exit code
(HTTP) Fail silently (no output at all) on server errors. This is
mostly done to enable scripts etc to better deal with failed attempts.
In normal cases when an HTTP server fails to deliver a document, it
returns an HTML document stating so (which often also describes why
and more). This flag will prevent curl from outputting that and return
error 22.
curl --show-error --fail -k -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build
我有一个包含三个阶段的 Jenkins 管道:“构建”、“测试”和“部署”。
这是我在使用“构建”时遇到的问题:
构建步骤确保 Control-M Automation API json 文件的结构有效。
为此,我在构建步骤中使用了 Automation API 提供的 $endpoint/build 服务:
stage('Build') {
environment {
CONTROLM_CREDS = credentials('xxx')
ENDPOINT = 'xxx'
}
steps {
sh '''
username=$CONTROLM_CREDS_USR
password=$CONTROLM_CREDS_PSW
# Login
login=$(curl -k -s -H "Content-Type: application/json" -X POST -d \{\"username\":\"$username\",\"password\":\"$password\"\} "$ENDPOINT/session/login" )
token=$(echo ${login##*token\" : \"} | cut -d '"' -f 1)
# Build
curl -k -s -H "Authorization: Bearer $token" -X POST -F "definitionsFile=@ctmjobs/TestCICD.json" "$ENDPOINT/build"
curl -k -s -H "Authorization: Bearer $token" -X POST "$ENDPOINT/session/logout"
'''
}
}
<snip>
一切都按预期工作,但如果我故意在 json 文件中输入错误,Jenkins 会检测到它并在终端中打印错误,但“构建”仍为绿色。任何人都可以识别错误吗?我的期望是,一旦 JSON 文件中出现错误,“构建”阶段就会变为红色。
这是终端的 Jenkins 输出:
+ password=****
++ curl -k -s -H 'Content-Type: application/json' -X POST -d '{"username":"xxx","password":"****"}' /automation-api/session/login
+ login='{
"username" : "xxx",
"token" : "xxx",
"version" : "9.19.200"
}'
++ echo 'xxx",
' '"version"' : '"9.19.200"
' '}'
++ cut -d '"' -f 1
+ token=xxx
+ curl -k -s -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build
{
"errors" : [ {
"message" : "unknown type: Job:Dummmy",
"file" : "Test.json",
"line" : 40,
"col" : 29
}, {
"message" : "unknown type: Job:Dummmy",
"file" : "Test.json",
"line" : 63,
"col" : 29
} ]
}+ curl -k -s -H 'Authorization: Bearer xxx' -X POST /automation-api/session/logout
{
"message" : "Successfully logged out from session xxx"
} ``
Jenkins 为了将一个阶段视为 failed
,它将检查所执行命令的 exit code
,在您的情况下
curl -k -s -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build
问题是 curl as a command
执行成功。
但是 curl 的正文表明 api 调用失败。
您可以将 --fail
标记添加到您的卷曲中。当 response status is > 400
erroneous exit code
(HTTP) Fail silently (no output at all) on server errors. This is mostly done to enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22.
curl --show-error --fail -k -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build