2

I have found how to store an Username/Password or SSH Username/PrivateKey using the groovy-based APIs for Jenkins.

https://gist.github.com/iocanel/9de5c976cc0bd5011653

domain = Domain.global()
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()

priveteKey = new BasicSSHUserPrivateKey(
CredentialsScope.GLOBAL,
"jenkins-slave-key",
"root",
new BasicSSHUserPrivateKey.UsersPrivateKeySource(),
"",
""
)

usernameAndPassword = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL,
"jenkins-slave-password", "Jenkis Slave with Password Configuration",
"root",
"jenkins"
)

store.addCredentials(domain, priveteKey)
store.addCredentials(domain, usernameAndPassword)

There are more kinds of credentials that can be stored. How do I do:

  • Secret file
  • Secret text
||||||
2

Implementation using another FileCredentialsImpl constructor:

import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.Domain;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;

def secret = '''Hi
there,
only
test'''

def secretBytes = SecretBytes.fromBytes(secret.getBytes())
def credentials = new FileCredentialsImpl(CredentialsScope.GLOBAL, 'my test file', 'description', 'file.txt', secretBytes)

SystemCredentialsProvider.instance.store.addCredentials(Domain.global(), credentials)

import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.Domain;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
import java.nio.file.*;

Path fileLocation = Paths.get("/path/to/some/file.txt");

def secretBytes = SecretBytes.fromBytes(Files.readAllBytes(fileLocation))
def credentials = new FileCredentialsImpl(CredentialsScope.GLOBAL, 'my test file', 'description', 'file.txt', secretBytes)

SystemCredentialsProvider.instance.store.addCredentials(Domain.global(), credentials)
||||||
  • Thanks for another implementation – ryan1506 Nov 9 '17 at 11:50
5

After some research, I found that the plain-credentials plugin implements the Secret Text and Secret File credentials. I forked the gist above and added code for these two types (see the gist for the reqwuired imports).

https://gist.github.com/chrisvire/383a2c7b7cfb3f55df6a

secretText = new StringCredentialsImpl(
CredentialsScope.GLOBAL,
"secret-text",
"Secret Text Description",
Secret.fromString("some secret text goes here"))

file = new File("/path/to/some/file")
noFileItem = [ getName: { return "" } ] as FileItem
FileCredentailsImpl can take a file from a do
secretFile = new FileCredentialsImpl(
CredentialsScope.GLOBAL,
"secret-file",
"Secret File Description"
noFileItem, // Don't use FileItem
file.getName(),
file.text)

store.addCredentials(domain, secretText)
store.addCredentials(domain, secretFile)
||||||
  • I'm getting an error - "unable to resolve class StringCredentialsImpl" any idea. I do have the plain Credential Plugin Installed. – Dinesh Dec 8 '18 at 21:25
  • 1
    @Dinesh import or fully qualify org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl – Sunvic Jan 30 '19 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.