Jerry Zhu
Jerry Zhu

Reputation: 86

Jenkins 2.18 withCredentials doesnt work in pipeline (Credentials Binding Plugin)

When I tried withCredentials in pipeline with the following code:

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

I got the error result like:

[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

When I tried create normal job with the following configs: jenkins config

Then got the right result like:

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

I don't know where I made wrong. The pipeline code was generated by pipeline syntax. It should work.

I also tried using jenkins 2.7.2, but got the exactly same error.

I don't know whether a jenkins bug or my mistake. Please help...Thank you..

Upvotes: 3

Views: 13453

Answers (1)

Kevin
Kevin

Reputation: 327

It seems you haven't posted and chosen your own answer yet. I'll post mine anyway.

In the documentation here.

// note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want

This one took me some time to understand, too. Here's how I did it while calling a shell script script.sh. I used single quotes to let the shell interpolate the variables when the script executes, just like what you did. If it helps, you can think of it as late interpolation.

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

And the variables <var1> and <var2> will be available in your script.sh as environment variables. I was trying to do a docker login in my script.sh.

#!/bin/bash

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

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

Upvotes: 2

Related Questions