Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

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:

  1. Open Jenkins in your web browser.
  2. Navigate to Manage Jenkins.
  3. Select Manage Plugins.
  4. Go to the Available tab.
  5. Search for Credentials Plugin.
  6. Check the box next to the plugin and click Install without restart.

Usage

To create and use credentials in Jenkins:

  1. Go to Manage Jenkins > Manage Credentials.
  2. Select a domain (or global if no domain is created).
  3. Click on Add Credentials.
  4. Select the type of credential you wish to create (e.g., Username with password).
  5. 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

Always use the Credentials Plugin to handle sensitive information instead of hardcoding them in your scripts or job configurations.
  • 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.