Home > Enterprise >  Can I add or remove credentials from Jenkins from an automated job?
Can I add or remove credentials from Jenkins from an automated job?

Time:08-30

Lets say I wanted to manage Jenkins credentials in an automated fashion and I wanted to add or remove credentials from a Jenkins job. Is this possible? Every documentation I find involves using the web interface to add or remove credentials but I need to configure a Pipeline job to do it.

CodePudding user response:

Yes you can. You can either use, Jenkins CLI, REST APIs or Groovy script to do this. Check the answer here. Here is a reference to how you can do this with Groovy scripts. Example below from the link,

import com.cloudbees.plugins.credentials.impl.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*

def source = new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("key")
def ck1 = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL,java.util.UUID.randomUUID().toString(), "username", source, "passphrase", "description")

SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), ck1)
  • Related