But the secret value is printed in the console log unmasked. What is the likely cause?
medium
A. The credentialsId 'mySecret' does not exist in Jenkins
B. Using echo instead of sh prevents masking
C. The variable name 'SECRET' is reserved and cannot be masked
D. The pipeline is missing the environment block
Solution
Step 1: Understand Jenkins masking scope
Jenkins masks secrets only in shell or batch commands, not in pipeline echo steps.
Step 2: Identify why echo prints unmasked
The echo step prints directly to console without masking, exposing the secret.
Final Answer:
Using echo instead of sh prevents masking -> Option B
Quick Check:
Masking works in sh, not pipeline echo [OK]
Hint: Use sh to mask secrets, not pipeline echo [OK]
Common Mistakes:
Assuming all prints mask secrets
Thinking missing credentials cause unmasked output
Believing environment block controls masking
5. You want to securely use multiple credentials (a username/password and a secret text) in a Jenkins scripted pipeline. Which snippet correctly binds both credentials and uses them in shell commands?
hard
A. withCredentials([usernamePassword('userPassId', 'USER', 'PASS'), string('tokenId', 'TOKEN')]) {
sh 'echo $USER $PASS $TOKEN'
}
B. credentials(['userPassId', 'tokenId']) {
sh 'echo $USER'
sh 'echo $PASS'
sh 'echo $TOKEN'
}
C. withCredentials([usernamePassword(credentialsId: 'userPassId', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'tokenId', variable: 'TOKEN')]) {
sh 'echo $USER $PASS $TOKEN'
}
D. withCredentials([usernamePassword(credentialsId: 'userPassId', usernameVariable: 'USER', passwordVariable: 'PASS'), string('tokenId', 'TOKEN')]) {
sh 'echo $USER'
sh 'echo $PASS'
sh 'echo $TOKEN'
}
Solution
Step 1: Understand binding multiple credentials
Use withCredentials with a list of credential bindings specifying IDs and variable names.
Step 2: Check correct syntax and usage in shell
withCredentials([usernamePassword(credentialsId: 'userPassId', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'tokenId', variable: 'TOKEN')]) {
sh 'echo $USER $PASS $TOKEN'
} correctly binds both credentials and uses a single shell command to echo all variables.
Final Answer:
withCredentials([usernamePassword(credentialsId: 'userPassId', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'tokenId', variable: 'TOKEN')]) {
sh 'echo $USER $PASS $TOKEN'
} -> Option C