jenkins pipeline with groovy and use of “Withcredentials”

6,313 views
Skip to first unread message

Yiyito

unread,
Apr 9, 2018, 5:16:03 PM4/9/18
to Jenkins Users
Hello guys first time writting here was wondering If I culd get some help:

I have a jenkins pipeline from SCM running in jenkins with a groovyscript. In one of the steps I have upload a file to artifactory with CURL - u user:pass , but don´t wan´t that user and pass be visible in output of pipeline in jenkins. I have read in several places the "Withcredential" function but I´m not quite sure how to this works or apply or implement. I tried this 2 options with no luck:

stage ('Upload war to Artifactory') {
                withCredentials([usernamePassword(credentialsId: 'artifactory', usernameVariable: 'user', passwordVariable: 'Password')])
               sh "sudo curl -u $user:$password -T $warPath 'https://artifactory.uuuuuu.com:443/artifactory/orm/$warFile'"

            }

In this case I´m getting:

org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException: artifactory at org.jenkinsci.plugins.credentialsbinding.MultiBinding.getCredentials(MultiBinding.java:153) at org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding.bind(UsernamePasswordMultiBinding.java:76) at org.jenkinsci.plugins.credentialsbinding.impl.BindingStep$Execution.start(BindingStep.java:114) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:229) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108) at groovy.lang.MetaClassImpl.invokeMethodOnGroovyObject(MetaClassImpl.java:1278) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1172) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810)



Where does the credential ID taken from? how does this function works inside the groovy script? I also tried this other way that I read but It uses a class like obect oriented, and instead of 


usernamePassword it uses UsernamePasswordMultiBinding


withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'artifactory', usernameVariable: 'username', passwordVariable: 'password']])



and it gives me following error:


[Pipeline] End of Pipeline java.lang.IllegalStateException: There is no body to invoke at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283) at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:95) at org.jenkinsci.plugins.credentialsbinding.impl.BindingStep$Execution.start(BindingStep.java:118) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:229) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108) at groovy.lang.MetaClassImpl.invokeMethodOnGroovyObject(MetaClassImpl.java:1278) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1172)



I also installed credential plug in in which I store user and pass globally and created a user ID which I then use in "withcredentials" ( credentialsId: 'artifactory'), but I think that Id is not related at all. I did some research but is not quite well explained the use of this function, and if It can be used inside a groovyscript for a jenkins pipeline from SCM. Can some one please explain how can I achieve this?.

I´m attaching my groovy script so you can see. 

If is not this way, how can I achieve to use user and pass on job output but that they don´t appear (or appear like ***)

Extractor.groovy

Victor Martinez

unread,
Apr 10, 2018, 6:36:14 AM4/10/18
to Jenkins Users
IIUC, you are missing the body of the withCredentials step, so something like the below should work

...


stage ('Upload war to Artifactory') {
    withCredentials([usernamePassword(credentialsId: 'artifactory', usernameVariable: 'user', passwordVariable: 'password')]) {
        sh "sudo curl -u $user:$password -T $warPath 'https://artifactory.xxxxxx.com:443/artifactory/form/$warFile'"
    }
}
...


Besides of that, the Snippet Generator (https://jenkins.io/doc/book/pipeline/getting-started/#snippet-generator) might help you too

Cheers
Reply all
Reply to author
Forward
0 new messages