How can we get the global credentials from Jenkins during automation runtime?

I have a situation,

During automation execution, I have to read the global credential details for my execution. How can we achieve this in Java?

Hello @sanoj-s and welcome to this community. :wave:

To read Jenkins global credentials in Java, I think you could use the Jenkins Credentials Plugin. This plugin provides a standardized API for other plugins to store and retrieve different types of credentials.

1 Like

Hello @sanoj-s
I am new to using Jenkins, but I have some experience, which I assume, you can try to implement to figure out your question.

In my example, I use Secret text as a global credential, because we have a few types of global credentials to consider.

Based on my experience, we can do this in two ways. First, by using the withCredential step or second, by using the Environment block.

As I assume you already added the Credentials to the Jenkins, didn’t you?
If you did, the next step will be to access the credential in the Pipeline by using withCredential :

stage('') {
      steps {
	// Use the 'withCredentials' step in your pipeline to bind the secret text credential to a variable:
          withCredentials([string(credentialsId: 'your_credential_id', variable: 'NAME_OF_VARIABLE')]) {
	// After that is going your pipeline steps that require the secret text credential, for instance:
          sh ''' "VARIABLE=$NAME_OF_VARIABLE" '''
          // or 
	 echo "VARIABLE=$NAME_OF_VARIABLE"
            }
        }
    }
  }

The second way is by using Environment:
At the start bind the credentials to the variable.

 pipeline {
    agent any
    environment {
        YOUR_VARIABLE = credentials('your_cradential_ID')
       }
    
Then use a variable at the step like this:

> stage('') {
      steps {
	          sh ''' --env-var "variable=$YOUR_VARIABLE" '''
          }
        }
    }
  }

Make sure to replace such elements in the script like: 'your_credential_id', "variable=$YOUR_VARIABLE", 'your_cradential_ID', "VARIABLE=$NAME_OF_VARIABLE", 'NAME_OF_VARIABLE'.

I hope it may help you somehow.
Best of luck :slight_smile:

1 Like

I have to use the logic in Java code to retrieve the credentials during runtime. Thanks let me try.

I used the below logic, it is not working as expected. First getting the error Jenkins jenkins = Jenkins.getInstanceOrNull();
System.out.println(jenkins);

getting as null

Below is the code,

String credentialsId = “myid”;

	Jenkins jenkins = Jenkins.getInstanceOrNull();
	if (jenkins != null) {
		List<UsernamePasswordCredentials> credentialsList = CredentialsProvider.lookupCredentials(
				UsernamePasswordCredentials.class, (Item) null, ACL.SYSTEM, (DomainRequirement[]) null);

		for (StandardUsernamePasswordCredentials credentials : credentialsList) {
			if (credentials.getId().equals(credentialsId)) {
				String username = credentials.getUsername();
				String password = credentials.getPassword().getPlainText(); 
																	

				System.out.println("Username: " + username);
				System.out.println("Password: " + password);
				break;
			}
		}
	} else {
		System.out.println("Jenkins instance not available");
	}