Using Credentials in Pipeline - Jenkins
1. Introduction
In Jenkins, managing credentials is crucial for securing sensitive information like passwords and tokens. This lesson will guide you on how to use credentials within Jenkins pipelines effectively.
2. Key Concepts
What are Jenkins Credentials?
Credentials in Jenkins are used to authenticate with external services or to securely store sensitive data.
Types of Credentials
- Username and Password
- SSH Username with Private Key
- Secret Text
- Secret File
3. Step-by-Step Process
-
Navigate to Jenkins Credentials:
Go to
Jenkins Dashboard > Credentials
to manage your credentials. -
Add a New Credential:
Click on
(global) > Add Credentials
and select the credential type. -
Using Credentials in Your Pipeline:
In your Jenkinsfile, use the
withCredentials
block to access credentials.pipeline { agent any stages { stage('Example') { steps { withCredentials([usernamePassword(credentialsId: 'my-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { sh 'echo $USERNAME' sh 'echo $PASSWORD' } } } } }
4. Best Practices
- Use unique credentials for different services to enhance security.
- Regularly rotate your credentials to mitigate the risk of exposure.
- Limit the number of users who can access sensitive credentials.
5. FAQ
How do I view existing credentials?
You can view existing credentials by navigating to Jenkins Dashboard > Credentials
.
Can I use credentials in a shared pipeline?
Yes, credentials can be shared across multiple pipelines if configured in the same domain.
What if I forget my credentials ID?
You can reset your credentials or create a new credential with a new ID from the Jenkins UI.