Using the WithCredentials Block in Jenkins Pipelines
📖 Scenario: You are setting up a Jenkins pipeline to deploy an application. The deployment requires a secret username and password stored securely in Jenkins credentials.To keep your secrets safe, you will use the withCredentials block in your Jenkins pipeline script.
🎯 Goal: Build a Jenkins pipeline script that uses the withCredentials block to access a username and password stored in Jenkins credentials, and then prints a message using those credentials.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Use withCredentials block to access credentials with ID my-credentials-id
Bind the username to variable USERNAME and password to variable PASSWORD
Inside the withCredentials block, print a message using echo that shows the username and a masked password
Use declarative pipeline syntax
💡 Why This Matters
🌍 Real World
In real Jenkins pipelines, you often need to use sensitive data like passwords or API keys. The withCredentials block helps you use these secrets safely without exposing them in logs.
💼 Career
Knowing how to use withCredentials is essential for DevOps engineers and anyone managing CI/CD pipelines to keep secrets secure and automate deployments.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins declarative pipeline script with a pipeline block and an empty stages section.
Jenkins
Hint
Start with pipeline {, add agent any, and an empty stages block.
2
Add a stage named 'Deploy' with a steps block
Inside the stages block, add a stage named Deploy with an empty steps block.
Jenkins
Hint
Add stage('Deploy') and inside it add steps { }.
3
Use withCredentials block to bind username and password
Inside the steps block of the Deploy stage, add a withCredentials block that binds the Jenkins credentials with ID my-credentials-id to variables USERNAME and PASSWORD using usernamePassword binding.
Jenkins
Hint
Use withCredentials with usernamePassword binding and the exact credential ID and variable names.
4
Print a message using the credentials inside withCredentials block
Inside the withCredentials block, add an echo statement that prints: "Deploying with user: ${USERNAME} and password: ${PASSWORD}".
Jenkins
Hint
Use echo inside the withCredentials block to print the username and password variables.
Practice
(1/5)
1. What is the main purpose of the withCredentials block in a Jenkins pipeline?
easy
A. To display credentials in the console output for debugging
B. To permanently store credentials in the pipeline script
C. To securely inject credentials only inside the block without exposing them elsewhere
D. To create new credentials in Jenkins
Solution
Step 1: Understand the purpose of withCredentials
The block is designed to provide credentials securely only within its scope.
Step 2: Analyze other options
Options A, B, and D describe incorrect or unsafe behaviors not related to withCredentials.
Final Answer:
To securely inject credentials only inside the block without exposing them elsewhere -> Option C
Quick Check:
Secure injection inside block = C [OK]
Hint: Remember: credentials only live inside the block [OK]
Common Mistakes:
Thinking credentials persist outside the block
Assuming credentials are printed in logs
Confusing with credential creation
2. Which of the following is the correct syntax to use a username and password credential with withCredentials in a Jenkins pipeline?
easy
A. withCredentials([usernamePassword(credentialsId: 'cred-id', usernameVariable: 'USER', passwordVariable: 'PASS')]) { ... }
B. withCredentials(usernamePassword('cred-id', 'USER', 'PASS')) { ... }
C. withCredentials { usernamePassword('cred-id', 'USER', 'PASS') ... }
D. withCredentials([usernamePassword('cred-id', 'USER', 'PASS')]) { ... }
Solution
Step 1: Recall correct withCredentials syntax
The correct syntax uses a list with usernamePassword specifying named parameters.
Step 2: Check each option
Only withCredentials([usernamePassword(credentialsId: 'cred-id', usernameVariable: 'USER', passwordVariable: 'PASS')]) { ... } uses named parameters and list syntax correctly; others miss brackets or parameter names.
Hint: Use named parameters inside a list for credentials [OK]
Common Mistakes:
Omitting square brackets around credentials
Not using named parameters
Wrong block structure
3. Given this Jenkins pipeline snippet:
withCredentials([string(credentialsId: 'token-id', variable: 'TOKEN')]) {
sh 'echo $TOKEN'
}
What will happen when this pipeline runs?
medium
A. The token will be printed as plain text in the Jenkins UI
B. The secret token value will be printed in the console output
C. The pipeline will fail because $TOKEN is not accessible
D. The shell command will print the token value but it will be masked in logs
Solution
Step 1: Understand withCredentials behavior
The secret is injected as an environment variable inside the block and Jenkins masks it in logs.
Step 2: Analyze the shell command output
The echo $TOKEN prints the token, but Jenkins hides it in console output to protect secrets.
Final Answer:
The shell command will print the token value but it will be masked in logs -> Option D
Quick Check:
Secrets masked in logs = A [OK]
Hint: Secrets print masked, not visible in logs [OK]
Common Mistakes:
Assuming secrets print as plain text
Thinking environment variables are unavailable
Confusing console output with UI display
4. Identify the error in this Jenkins pipeline snippet:
withCredentials([usernamePassword(credentialsId: 'cred-id', usernameVariable: 'USER')]) {
sh 'echo $USER $PASS'
}
medium
A. Missing passwordVariable parameter causes $PASS to be undefined
B. Credentials ID is incorrect format
C. The sh step cannot access environment variables
D. The withCredentials block must not use a list
Solution
Step 1: Check withCredentials parameters
The snippet provides only usernameVariable but misses passwordVariable.
Step 2: Understand impact on shell command
Since passwordVariable is missing, $PASS is undefined and will cause an empty or error output.
Final Answer:
Missing passwordVariable parameter causes $PASS to be undefined -> Option A
Quick Check:
Both username and password variables needed = D [OK]
Hint: Always specify both usernameVariable and passwordVariable [OK]
Common Mistakes:
Forgetting passwordVariable
Assuming shell can't access env vars
Wrong credentialsId format
5. You want to use multiple credentials (a username/password and a secret text) inside a Jenkins pipeline securely. Which is the correct way to use withCredentials to access both?