You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:25:58