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
相关产品推荐
相关产品推荐

