Skip to main content

Creating credentials using Groovy

Comments

9 comments

  • Alexander Ost

    The folder example will only work if the folder already has a FolderCredentialsProperty (e.g., because some credentials were defined from GUI side before). Newly created folders do not have that, and the example will fail.

    How to proceed in that case?

    0
  • Allan Burdajewicz

    In that case, you can create a new FolderCredentialsProperty - which can take a list of credentials as parameter:

     

    --------

    [...]

    if(property) {
        println "Add credentials in global store"
        property.getStore().addCredentials(Domain.global(), c)
    } else {
        println "Initialize Folder Credentials store and add credentials in global store"
        property = new FolderCredentialsProperty([c])
        folderAbs.addProperty(property)
    }

    --------

     

    See source code of FolderCredentialsProvider.FolderCredentialsProperty

    0
  • Marc Sutter

     Hi,

    Thanks for the folder example. It works fine if used as init.groovy.d script. However it doesn't seems to work if used in a jenkinsfile or a groovy dsl file.

    following import fails

    import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider.FolderCredentialsProperty;

    Any hints how to use it from a groovy dsl script ?

    0
  • Arnaud Héritier

    Hi Marc,

     

      You have the (CloudBees) Folder plugin installed ? In which version ?

    0
  • Marc Sutter

    Hi Arnaud,

    I'm running jenkins LTS 2.32.2 with the folder plugin version 5.17. 

    Cheers

    0
  • Jorge Peña

    How to add a credential domain via groovy ? I want to add a credential domain on a specific folder (Folder -> Credential -> Add domain) and then inside this domain add a credential.

    0
  • Ott Ramst

    Hi!

    I have a question about this script.

    This script puts the created credential into the 'global' domain. Is there any way to do this for a specific folder domain?

    i.e. I have a folder called 'build-jobs' and it has a credentials domain called 'build-jobs-credentials' and I only want to add credentials under that domain. Is is possible with Groovy?

    Thanks!

    1
  • Dheeraj Janagam

    Hi

    Instead of randomly generated "ID", how can I have a fixed though this script?

    Something like "userid_prod_usertoken_github-enterprise-prod" #36 char length

    Thanks!

    0
  • Sonali Prasad17

    Hi,

    Your blog is quite informative, but I see that these are the ways if you want to add credentials globally or for a folder, what about the project/job then? Will you please mention the script for how can we generate credentials that are specific to job/project.
    Thanks in advance.

    0

Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.