Popular Plugin: Credentials Plugin
Introduction
The Credentials Plugin in Jenkins provides a way to store and manage credentials securely. This plugin allows you to create, manage, and use credentials in your Jenkins jobs, ensuring that sensitive information such as passwords, SSH keys, and API tokens are not exposed in your job configurations.
Key Concepts
- Credentials: Sensitive data that needs to be protected, such as passwords and tokens.
- Domain: A way to group credentials for organizational purposes.
- Credential Types: Various types of credentials supported by the plugin (e.g., Username with password, SSH Username with private key).
Installation
To install the Credentials Plugin:
- Open Jenkins in your web browser.
- Navigate to Manage Jenkins.
- Select Manage Plugins.
- Go to the Available tab.
- Search for Credentials Plugin.
- Check the box next to the plugin and click Install without restart.
Usage
To create and use credentials in Jenkins:
- Go to Manage Jenkins > Manage Credentials.
- Select a domain (or global if no domain is created).
- Click on Add Credentials.
- Select the type of credential you wish to create (e.g., Username with password).
- Fill out the required fields and click OK.
Example: Using Credentials in a Jenkins Pipeline
pipeline {
agent any
stages {
stage('Example') {
steps {
withCredentials([usernamePassword(credentialsId: 'my-credentials-id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh 'echo $USERNAME'
sh 'echo $PASSWORD'
}
}
}
}
}
Best Practices
- Use descriptive IDs for your credentials to easily identify them.
- Regularly update and rotate your credentials.
- Restrict access to credentials to only those who need it.
- Group related credentials in domains for better organization.
FAQ
What types of credentials can I create?
You can create various types of credentials such as Username and password, SSH Username with private key, Secret text, and more.
Can I use credentials in a freestyle project?
Yes, you can use credentials in freestyle projects by selecting the appropriate credential in the build step where it is needed.
How can I manage permissions for credentials?
You can manage permissions for credentials by configuring the appropriate security settings in Jenkins under Manage Jenkins > Configure Global Security.