Jenkins流水线Groovy脚本传递uploadID变量失败求助
解决Jenkins流水线Groovy脚本中curl请求的变量传递问题
问题根源:你用Groovy单引号(
'')包裹shell脚本时,单引号字符串不会触发Groovy的变量插值,${uploadID}会被当成shell字面量而非Groovy的uploadID变量,导致API收到的不是实际的上传ID整数,从而报错。修复方案:
将包裹shell脚本的单引号改为双引号(""),同时对shell中需要保留的$符号(如$curl_output)转义,让Groovy先完成变量插值再传递给shell执行。修改后的代码如下:
... def jsonResponse = readJSON text: uploadResponse def uploadID = jsonResponse.message // 可选:确认uploadID为整数,必要时强制转换 // uploadID = uploadID as Integer def response_analyze = sh(script: """ curl_output=\$(curl -X POST "http://10.10.10.10:8081/repo/api/v2/jobs" \\ -H "Authorization: Bearer xxxxxxxxxxxxxxx" \\ -H "folderId: 3" \\ -H "uploadId:${uploadID}" \\ -H "Content-Type: application/json") echo \$curl_output """.trim(), returnStdout: true).trim() // Parse the JSON response def jsonResponseAnalyze = readJSON text: response_analyze def message_analyze_ID = jsonResponseAnalyze.message ...
- 额外检查项:
- 先添加
echo "uploadID值:${uploadID}"确认变量是否为有效整数,避免jsonResponse.message返回字符串类型整数或非数值内容。 - 若
uploadID是字符串类型,可通过uploadID = uploadID as Integer强制转换为整数,确保传递给API的格式正确。
- 先添加
内容的提问来源于stack exchange,提问作者sleepy
相关产品推荐
相关产品推荐

