Jenkins Credentials Plugin: What It Is and How It Works
Credentials Plugin in Jenkins securely stores and manages sensitive information like passwords, tokens, and keys. It allows Jenkins jobs to access these secrets safely without exposing them in the job configuration or logs.How It Works
The Jenkins Credentials Plugin acts like a secure locker for your sensitive data such as passwords, API tokens, or SSH keys. Instead of writing these secrets directly in your job scripts or configuration files, you store them in this locker.
When a Jenkins job runs, it can ask the plugin to fetch the needed secret from the locker. This way, the secret is never openly visible in the job code or logs, reducing the risk of accidental leaks.
Think of it like a safe deposit box at a bank: you keep your valuables locked inside, and only trusted people with the right key can access them when needed.
Example
This example shows how to use the Credentials Plugin in a Jenkins Pipeline script to access a stored secret password.
pipeline {
agent any
stages {
stage('Use Credentials') {
steps {
withCredentials([string(credentialsId: 'my-secret-password', variable: 'PASSWORD')]) {
sh 'echo The secret password is masked and not shown in logs'
sh 'echo $PASSWORD'
}
}
}
}
}When to Use
Use the Credentials Plugin whenever your Jenkins jobs need to use sensitive information like passwords, API keys, or SSH private keys. It helps keep these secrets safe and prevents accidental exposure.
Common real-world uses include:
- Accessing private Git repositories with SSH keys
- Deploying applications using API tokens
- Connecting to databases with passwords
- Authenticating with cloud providers securely
By centralizing secret management, it also makes updating credentials easier without changing multiple job configurations.
Key Points
- The Credentials Plugin stores secrets securely in Jenkins.
- It prevents secrets from appearing in job code or logs.
- Supports many types of credentials like passwords, tokens, and keys.
- Integrates easily with Jenkins Pipelines using
withCredentials. - Helps centralize and manage secrets for multiple jobs.