withCredentials not working in multibranch project?

1,874 views
Skip to first unread message

Michael Irwin

unread,
Jun 10, 2016, 8:56:42 AM6/10/16
to Jenkins Users
Versions (current for each)...
- Jenkins - 2.8
- Credentials Plugin - 2.0.7
- Credentials Binding Plugin - 1.7
- Pipeline: Multibranch - 2.6
(let me know if you need any others)

- Working in a multibranch project
- Created a "Secret File" credential in the Global scope (just for testing) with id "settings-file".  
- Jenkinsfile has:

node {
  ...
  withCredentials([$class: 'FileBinding', credentialsId: 'settings-file', variable: 'SETTINGS_LOCATION']) {
    sh "cp $SETTINGS_LOCATION ./settings.xml"
  }
  ...
}

When it runs, get hit with...

groovy.lang.MissingPropertyException: No such property: SETTINGS_LOCATION for class: groovy.lang.Binding

Oddly, when I open the "Pipeline Syntax" at the project, I'm able to produce a Groovy snippet for the credential.  But, if I open the "Pipeline Syntax" at the branch level (within the project folder), there are no credentials available for me to select in the snippet generator.  So, it seems that the folder isn't getting the same visibility to the credential (even though it was defined globally).

Any pointers?

--
Michael Irwin

Matias Montes

unread,
Jun 24, 2016, 3:29:19 PM6/24/16
to Jenkins Users
I am at this exact same point trying to figure it out, but it looks like this simply cannot be done at the moment :(

Vincent Latombe

unread,
Jun 25, 2016, 12:40:34 PM6/25/16
to Jenkins Users
Use ${env.SETTINGS_LOCATION} in your groovy string.


Vincent

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/34aaef83-a04f-495f-b7ef-dc9fed3c32c3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Kobit

unread,
Jun 26, 2016, 9:49:55 AM6/26/16
to Jenkins Users
There are a couple different things to notice here:

  1. Double quotes in Groovy is string interpolation
    • Your sh "cp $SETTINGS_LOCATION ./settings.xml" is using string interpolation and $SETTINGS_LOCATION is being evaluated in the script, but that variable is not present
  2. withCredentials binds to an environment variable. So, SETTINGS_LOCATION should be available for execution on the allocated nodes
There are a few ways I believe you could fix this:
  1. Use the env global variable (like previously mentioned) to substitute in the value through the Groovy which will retrieve the previously set environment variable by withCredential
    • sh "cp ${env.SETTINGS_LOCATION} ./settings.xml"
  2. Use single quotes to delay the evaluation of the $SETTINGS_LOCATION until the script executes on the node and uses environment variable substituion (I believe this would work, haven't tested it)
    • sh 'cp $SETTINGS_LOCATION ./settings.xml'
I believe the second example will not output the value to the logs (which may be important if you do other credentials substitutions, like UsernamePasswordMultiBinding)
Reply all
Reply to author
Forward
0 new messages