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

Jenkins Pipeline调用Credentials时Groovy报错如何修复

问题原因

报错是因为string类型的凭证绑定不能作为独立的嵌套步骤使用,它必须和其他凭证配置一起放在withCredentials的参数数组中。你当前写法将string()单独放在withCredentials的闭包内部,不符合Jenkins Pipeline的凭证绑定语法规则。

修复后的代码片段

stage('plan') {
  withCredentials([
    [$class: 'AmazonWebServicesCredentialsBinding', credentialsId: aws_cred_id,
      accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],
    string(credentialsId: 'xxxxcredx', variable: 'TF_VAR_token')
  ]) {
    docker.withRegistry('https://registry-1.docker.io/', 'dockerhub-creds') {
    // 后续执行逻辑保持不变
    }
  }
}

效果验证

修改后运行Pipeline,你可以在withCredentials的闭包内部直接调用${TF_VAR_token}获取对应凭证值,AWS的两个认证环境变量也会正常注入,不会再报参数类型错误。

内容的提问来源于stack exchange,提问作者devgirl

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 14:09:03