Bird
Raised Fist0
Jenkinsdevops~20 mins

WithCredentials block usage in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
WithCredentials Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Jenkins pipeline snippet using withCredentials?

Consider this Jenkins pipeline snippet:

pipeline {
  agent any
  stages {
    stage('Print Secret') {
      steps {
        withCredentials([string(credentialsId: 'my-secret', variable: 'SECRET')]) {
          echo "Secret is: $SECRET"
        }
      }
    }
  }
}

Assuming the credential 'my-secret' contains the value 's3cr3t!', what will Jenkins print in the console?

Jenkins
pipeline {
  agent any
  stages {
    stage('Print Secret') {
      steps {
        withCredentials([string(credentialsId: 'my-secret', variable: 'SECRET')]) {
          echo "Secret is: $SECRET"
        }
      }
    }
  }
}
ASecret is: my-secret
BSecret is: s3cr3t!
CSecret is: null
DSecret is: $SECRET
Attempts:
2 left
💡 Hint

withCredentials injects the secret value into the environment variable.

❓ Troubleshoot
intermediate
2:00remaining
Why does this Jenkins pipeline fail to access the secret inside withCredentials?

Look at this Jenkins pipeline snippet:

pipeline {
  agent any
  environment {
    SECRET = ''
  }
  stages {
    stage('Get Secret') {
      steps {
        withCredentials([string(credentialsId: 'my-secret', variable: 'SECRET')]) {
          echo "Secret is: $SECRET"
        }
      }
    }
  }
}

The pipeline prints: Secret is:

Why is the secret value empty?

Jenkins
pipeline {
  agent any
  environment {
    SECRET = ''
  }
  stages {
    stage('Get Secret') {
      steps {
        withCredentials([string(credentialsId: 'my-secret', variable: 'SECRET')]) {
          echo "Secret is: $SECRET"
        }
      }
    }
  }
}
AThe environment variable SECRET is overridden by the empty value in the environment block before withCredentials runs.
BThe credentialsId 'my-secret' does not exist in Jenkins credentials store.
CwithCredentials does not support string type credentials.
DThe echo command does not expand variables inside double quotes.
Attempts:
2 left
💡 Hint

Check how environment variables are set and overridden in Jenkins pipelines.

❓ Configuration
advanced
2:00remaining
How to correctly use multiple credentials in a withCredentials block?

You want to use two credentials in a Jenkins pipeline: a username/password and a secret text. Which of the following withCredentials blocks correctly sets environment variables USER, PASS, and TOKEN?

Jenkins
withCredentials([usernamePassword(credentialsId: 'user-pass', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'token', variable: 'TOKEN')]) {
  // steps
}
AwithCredentials([usernamePassword(credentialsId: 'user-pass', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'token', variable: 'TOKEN')]) { /* steps */ }
BwithCredentials([usernamePassword(credentialsId: 'user-pass', userVariable: 'USER', passVariable: 'PASS'), string(credentialsId: 'token', variable: 'TOKEN')]) { /* steps */ }
CwithCredentials([usernamePassword(credentialsId: 'user-pass', usernameVariable: 'USER'), string(credentialsId: 'token', variable: 'TOKEN')]) { /* steps */ }
DwithCredentials([usernamePassword(credentialsId: 'user-pass', usernameVariable: 'USER', passwordVariable: 'PASS'), secretText(credentialsId: 'token', variable: 'TOKEN')]) { /* steps */ }
Attempts:
2 left
💡 Hint

Check the exact parameter names for usernamePassword and string credential types.

✅ Best Practice
advanced
2:00remaining
What is the best practice for handling secrets in Jenkins pipelines using withCredentials?

Which of the following is the best practice when using withCredentials in Jenkins pipelines?

AUse environment block to set secret variables globally for all stages.
BStore secrets directly in pipeline scripts as plain text for easy access.
CPrint secret values in console logs for debugging purposes.
DUse withCredentials only around the steps that need the secret, not globally in the pipeline.
Attempts:
2 left
💡 Hint

Think about minimizing exposure of secrets.

🔀 Workflow
expert
2:00remaining
In which order are environment variables set when using withCredentials inside a pipeline with environment block?

Given this Jenkins pipeline snippet:

pipeline {
  agent any
  environment {
    TOKEN = 'env-token'
  }
  stages {
    stage('Use Credentials') {
      steps {
        withCredentials([string(credentialsId: 'secret-token', variable: 'TOKEN')]) {
          echo "Token is: $TOKEN"
        }
      }
    }
  }
}

What will be printed if the credential 'secret-token' has value 'cred-token'?

AToken is: $TOKEN
BToken is: env-token
CToken is: cred-token
DToken is: null
Attempts:
2 left
💡 Hint

Consider precedence of environment variables set by withCredentials vs pipeline environment block.

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

  1. Step 1: Understand the purpose of withCredentials

    The block is designed to provide credentials securely only within its scope.
  2. Step 2: Analyze other options

    Options A, B, and D describe incorrect or unsafe behaviors not related to withCredentials.
  3. Final Answer:

    To securely inject credentials only inside the block without exposing them elsewhere -> Option C
  4. 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

  1. Step 1: Recall correct withCredentials syntax

    The correct syntax uses a list with usernamePassword specifying named parameters.
  2. 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.
  3. Final Answer:

    withCredentials([usernamePassword(credentialsId: 'cred-id', usernameVariable: 'USER', passwordVariable: 'PASS')]) { ... } -> Option A
  4. Quick Check:

    Named params + list = A [OK]
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

  1. Step 1: Understand withCredentials behavior

    The secret is injected as an environment variable inside the block and Jenkins masks it in logs.
  2. Step 2: Analyze the shell command output

    The echo $TOKEN prints the token, but Jenkins hides it in console output to protect secrets.
  3. Final Answer:

    The shell command will print the token value but it will be masked in logs -> Option D
  4. 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

  1. Step 1: Check withCredentials parameters

    The snippet provides only usernameVariable but misses passwordVariable.
  2. Step 2: Understand impact on shell command

    Since passwordVariable is missing, $PASS is undefined and will cause an empty or error output.
  3. Final Answer:

    Missing passwordVariable parameter causes $PASS to be undefined -> Option A
  4. 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?
hard
A. withCredentials(usernamePassword(credentialsId: 'cred1', usernameVariable: 'USER', passwordVariable: 'PASS')) { withCredentials(string(credentialsId: 'cred2', variable: 'TOKEN')) { ... } }
B. withCredentials([usernamePassword(credentialsId: 'cred1', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'cred2', variable: 'TOKEN')]) { ... }
C. withCredentials([usernamePassword('cred1', 'USER', 'PASS'), string('cred2', 'TOKEN')]) { ... }
D. withCredentials([usernamePassword(credentialsId: 'cred1', usernameVariable: 'USER', passwordVariable: 'PASS')]) { withCredentials([string(credentialsId: 'cred2', variable: 'TOKEN')]) { ... } }

Solution

  1. Step 1: Understand multiple credentials usage

    You can pass multiple credentials as a list inside a single withCredentials block.
  2. Step 2: Compare options

    withCredentials([usernamePassword(credentialsId: 'cred1', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'cred2', variable: 'TOKEN')]) { ... } correctly uses a list with both credential types together. Options A and D nest blocks unnecessarily. withCredentials([usernamePassword('cred1', 'USER', 'PASS'), string('cred2', 'TOKEN')]) { ... } uses wrong syntax.
  3. Final Answer:

    withCredentials([usernamePassword(credentialsId: 'cred1', usernameVariable: 'USER', passwordVariable: 'PASS'), string(credentialsId: 'cred2', variable: 'TOKEN')]) { ... } -> Option B
  4. Quick Check:

    Multiple creds in one list = B [OK]
Hint: Use one list with all credentials inside withCredentials [OK]
Common Mistakes:
  • Nesting multiple withCredentials blocks unnecessarily
  • Using incorrect parameter syntax
  • Passing credentials without a list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard