【问题标题】:Jenkins 2.18 withCredentials doesnt work in pipeline (Credentials Binding Plugin)Jenkins 2.18 withCredentials 在管道中不起作用(Credentials Binding Plugin)
【发布时间】:2016-08-18 02:42:33
【问题描述】:

当我使用以下代码在管道中尝试withCredentials 时:

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

我得到如下错误结果:

[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 config

然后得到正确的结果:

[test2] $ /bin/sh -xe /tmp/hudson3350033971174776171.sh
+ echo jerry:123
jerry:123
Finished: SUCCESS

我不知道我哪里做错了。管道代码是由管道语法生成的。它应该工作。

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

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

【问题讨论】:

  • 我发现了我的错误。应该使用单引号而不是双引号...但是我发现另一个问题是当我回显 USER 时,只显示 ****,对吗?
  • 是的。根据文档here:“构建日志中的任何秘密都将被自动屏蔽。”
  • 我遇到了类似的问题,这是因为我的 Jenkins 项目中有一个空间。永远不会再犯这样的错误! (我可能会)

标签: shell jenkins groovy


【解决方案1】:

您似乎还没有发布并选择自己的答案。无论如何我都会发布我的。

在文档here

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

这个我也花了一些时间才理解。这是我在调用 shell 脚本script.sh 时的做法。我使用单引号让 shell 在脚本执行时插入变量,就像您所做的一样。如果有帮助,您可以将其视为后期插值。

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

变量&lt;var1&gt;&lt;var2&gt; 将在您的script.sh 中作为环境变量提供。我试图在我的script.sh 中做一个docker login

#!/bin/bash

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

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

【讨论】:

    猜你喜欢
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2020-03-28
    • 2014-10-06
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多