Jenkins 2.18 withCredentials在管道中不起作用(Credentials Binding Plugin)

时间:2016-08-18 02:42:34

标签: shell jenkins groovy

当我使用以下代码在管道中尝试SyncedCron.add({ name: 'Update mass quantities', schedule: function(parser) { // parser is a later.parse object return parser.text('every 1 minute'); // or at any interval you wish }, job: function() { var query = { notYetProcessed: true }; // or whatever your criteria are var batchSize = { limit: 300 }; // for example myCollection.find(query,batchSize).forEach(doc){ var update = { $set: { notYetProcessed: false }}; // along with everything else you want to update myCollection.update(doc._id,update); } } }); 时:

withCredentials

我得到的错误结果如下:

node('master'){
    withCredentials([[$class: 'UsernamePasswordBinding', credentialsId: '6c7ca6d2-bb14-46e2-bcba-c0a09f8b9fc5', variable: 'USER']]) {
        sh "echo $USER"
    }
}

当我尝试使用以下配置创建正常作业时: jenkins config

然后得到了正确的结果:

[Pipeline] {
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: USERNAME for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
    at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:24)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:3)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:58)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:361)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:226)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE

我不知道自己错在哪里。管道代码由管道语法生成。它应该工作。

我也尝试过使用jenkins 2.7.2,但得到了完全相同的错误。

我不知道是不是詹金斯虫或我的错误。请帮忙......谢谢..

1 个答案:

答案 0 :(得分:0)

您似乎尚未发布并选择了自己的答案。无论如何,我会发布我的。

在文档here中。

  

//注意:单引号阻止Groovy插值;扩展是由Bourne Shell,这是你想要的

这个花了我一些时间来理解。这是我在调用shell脚本script.sh时如何做到的。我使用单引号让shell在脚本执行时插入变量,就像你所做的那样。如果它有帮助,你可以把它想象为延迟插值。

stage('<stage>') {
    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<id>', usernameVariable: '<var1>', passwordVariable: '<var2>']]) {
        sh './script.sh'
    }
}

变量<var1><var2>将在您的script.sh中作为环境变量提供。我试图在我的docker login中进行script.sh

#!/bin/bash

# Will still print ****, which is cool.
echo $<var1>

# Will interpolate to their values correctly.
docker login -u $<var1> -p $<var2>