Username Password Credential in Jenkins: What It Is and How It Works
username password credential is a secure way to store and manage a username and password pair used for authentication in jobs or pipelines. It helps Jenkins access external systems like Git, servers, or APIs without exposing sensitive information in the code.How It Works
Think of a username password credential in Jenkins like a locked box where you keep your login details safe. Instead of typing your username and password directly into your build scripts or jobs, Jenkins stores them securely in this box. When a job needs to connect to an external service, Jenkins opens the box, uses the credentials, and then locks it again.
This keeps your sensitive information hidden from plain view and prevents accidental leaks. Jenkins encrypts these credentials and only allows authorized jobs or users to access them. This way, your automation can safely authenticate with services like Git repositories, deployment servers, or APIs without exposing your password in logs or code.
Example
This example shows how to create and use a username password credential in a Jenkins pipeline script.
pipeline {
agent any
stages {
stage('Use Credentials') {
steps {
withCredentials([usernamePassword(credentialsId: 'my-cred-id', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
sh 'echo Username is $USER'
sh 'echo Password is $PASS'
}
}
}
}
}When to Use
Use username password credentials in Jenkins whenever your jobs need to authenticate with external systems securely. Common cases include:
- Accessing private Git repositories
- Logging into remote servers for deployment
- Calling APIs that require basic authentication
- Connecting to databases or cloud services that use username/password login
This approach avoids hardcoding sensitive data in your pipeline scripts and helps keep your automation secure and manageable.
Key Points
- Username password credentials store login info securely in Jenkins.
- They prevent exposing sensitive data in code or logs.
- Used with
withCredentialsin pipeline scripts. - Essential for authenticating to external services in automation.