0
0
Jenkinsdevops~5 mins

Credential types and storage in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Jenkins needs to use passwords, keys, and tokens safely to connect to other tools and servers. Credential types and storage help keep these secrets safe and easy to use in your automation.
When Jenkins needs to connect to a Git repository using a username and password or SSH key.
When Jenkins must deploy code to a server using an SSH private key.
When Jenkins pipelines require API tokens to access cloud services.
When you want to avoid putting passwords directly in your pipeline scripts.
When you want to share credentials securely across multiple Jenkins jobs.
Commands
This command adds credentials to Jenkins using an XML file. It stores the secret safely for Jenkins jobs to use.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 create-credentials-by-xml system::system::jenkins _ < credentials.xml
Expected OutputExpected
Credentials added successfully
-s - Specifies the Jenkins server URL
Lists all credentials stored in Jenkins so you can verify they are saved correctly.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 list-credentials system::system::jenkins _
Expected OutputExpected
ID: my-ssh-key Description: SSH key for deployment ID: git-token Description: Token for Git access
-s - Specifies the Jenkins server URL
Deletes a credential by its ID when it is no longer needed or compromised.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 delete-credentials system::system::jenkins my-ssh-key
Expected OutputExpected
Credentials deleted successfully
-s - Specifies the Jenkins server URL
Key Concept

If you remember nothing else, remember: Jenkins stores credentials securely and lets you use them in pipelines without exposing secrets in code.

Common Mistakes
Putting passwords or keys directly in pipeline scripts.
This exposes secrets to anyone who can see the code, risking security breaches.
Store secrets in Jenkins credentials and reference them securely in pipelines.
Using wrong credential IDs in pipeline steps.
Jenkins will fail to find the credentials and the job will error out.
Double-check credential IDs in Jenkins and use exact IDs in pipeline code.
Not restricting credential permissions properly.
Anyone with access to Jenkins might misuse sensitive credentials.
Use Jenkins security settings to limit who can view or use credentials.
Summary
Use Jenkins credentials to store passwords, keys, and tokens securely.
Add, list, and delete credentials using Jenkins CLI commands.
Reference credentials in pipelines by their IDs to keep secrets safe.