📖 Scenario: You are setting up Jenkins to securely store credentials needed for your build jobs. Jenkins supports different types of credentials like username/password, secret text, and SSH keys. Properly storing and using these credentials helps keep your automation safe.
🎯 Goal: Learn how to create and store different credential types in Jenkins using the Jenkinsfile syntax. You will define credentials, configure their types, and then use them in a simple Jenkins pipeline.
📋 What You'll Learn
Create a credentials dictionary with exact credential IDs and types
Add a configuration variable for the default credential ID
Write a Jenkins pipeline snippet that uses the stored credential
Print the credential ID used in the pipeline
💡 Why This Matters
🌍 Real World
Jenkins pipelines often need to access external systems like Git, Docker registries, or cloud providers. Storing credentials securely and using them in pipelines is essential to protect sensitive data.
💼 Career
Understanding Jenkins credential types and storage is a key skill for DevOps engineers and automation specialists to maintain secure CI/CD pipelines.
Progress0 / 4 steps
1
Create a dictionary of Jenkins credentials
Create a dictionary called credentials with these exact entries: 'cred1': 'usernamePassword', 'cred2': 'secretText', 'cred3': 'sshUserPrivateKey'.
Jenkins
Hint
Use a Python dictionary with keys as credential IDs and values as their types.
2
Add a default credential ID variable
Add a variable called default_cred and set it to the string 'cred2'.
Jenkins
Hint
Just assign the string 'cred2' to a variable named default_cred.
3
Write a Jenkins pipeline snippet using the default credential
Write a variable called pipeline_snippet that contains this exact string: withCredentials([string(credentialsId: default_cred, variable: 'SECRET')]) { echo "Using credential: ${SECRET}" }.
Jenkins
Hint
Use double quotes and escape inner quotes properly to match the exact string.
4
Print the Jenkins pipeline snippet
Write a print statement to display the value of pipeline_snippet.
Jenkins
Hint
Use print(pipeline_snippet) to show the pipeline snippet.
Practice
(1/5)
1. Which of the following is NOT a common Jenkins credential type?
easy
A. Database connection string
B. Secret text
C. SSH private key
D. Username and password
Solution
Step 1: Review common Jenkins credential types
Jenkins commonly uses username/password, secret text, SSH keys, and secret files as credential types.
Step 2: Identify the option not listed as a credential type
Database connection string is not a built-in Jenkins credential type; it can be stored as secret text but is not a distinct type.
sshUserPrivateKey requires both credentialsId and usernameVariable to bind the SSH username.
Step 2: Identify missing usernameVariable
The snippet only uses keyFileVariable but misses usernameVariable, which is required for proper binding.
Final Answer:
Missing usernameVariable in sshUserPrivateKey binding -> Option D
Quick Check:
sshUserPrivateKey needs usernameVariable and keyFileVariable [OK]
Hint: sshUserPrivateKey needs usernameVariable and keyFileVariable [OK]
Common Mistakes:
Omitting usernameVariable in sshUserPrivateKey
Thinking keyFileVariable alone is enough
Believing sh cannot use env variables
5. You want to securely use a secret file credential in a Jenkins pipeline to deploy a configuration file. Which is the correct way to access the secret file inside the pipeline?
hard
A. Use credentials('config-file') directly in the shell command
B. Use withCredentials([string(credentialsId: 'config-file', variable: 'CONFIG')]) and echo $CONFIG
C. Use withCredentials([file(credentialsId: 'config-file', variable: 'CONFIG')]) and refer to $CONFIG path
D. Store the file content as secret text and write it manually in the pipeline
Solution
Step 1: Understand secret file credential usage
Secret files are accessed by binding them to a file path variable using file() in withCredentials.
Step 2: Use the file path variable in pipeline steps
The variable (e.g., CONFIG) holds the path to the secret file, which can be used in shell commands safely.
Final Answer:
Use withCredentials([file(credentialsId: 'config-file', variable: 'CONFIG')]) and refer to $CONFIG path -> Option C
Quick Check:
Secret files bind to file path variables with file() [OK]
Hint: Use file() binding for secret files to get file path [OK]
Common Mistakes:
Using string() for secret files instead of file()
Trying to use credentials() directly in shell
Manually writing file content instead of binding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.