0
0
Jenkinsdevops~10 mins

Credentials binding in pipelines in Jenkins - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to bind a username and password credential in a Jenkins pipeline.

Jenkins
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        withCredentials([usernamePassword(credentialsId: '[1]', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
          echo "Username is $USER"
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Amy-credentials-id
Bdocker-hub
Caws-secret
Dgit-token
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong credentials ID that does not exist in Jenkins.
Confusing the credentials ID with the username or password.
2fill in blank
medium

Complete the code to bind a secret text credential in a Jenkins pipeline.

Jenkins
pipeline {
  agent any
  stages {
    stage('Use Secret') {
      steps {
        withCredentials([string(credentialsId: '[1]', variable: 'SECRET')]) {
          sh 'echo Secret is $SECRET'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Auser-pass
Bssh-key
Capi-key
Ddocker-login
Attempts:
3 left
💡 Hint
Common Mistakes
Using usernamePassword binding for secret text credentials.
Using an incorrect credentials ID.
3fill in blank
hard

Fix the error in the code to correctly bind an SSH private key credential.

Jenkins
pipeline {
  agent any
  stages {
    stage('SSH Access') {
      steps {
        withCredentials([sshUserPrivateKey(credentialsId: '[1]', keyFileVariable: 'KEYFILE', usernameVariable: 'USER')]) {
          sh 'ssh -i $KEYFILE $USER@host'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Auser-pass
Bssh-key-01
Cdocker-secret
Dapi-token
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-SSH credential ID with sshUserPrivateKey binding.
Mixing up username and key file variables.
4fill in blank
hard

Fill both blanks to bind a file credential and use it in a shell command.

Jenkins
pipeline {
  agent any
  stages {
    stage('Use File') {
      steps {
        withCredentials([file(credentialsId: '[1]', variable: '[2]')]) {
          sh 'cat $[2]'
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aconfig-file
BCONFIG_PATH
CSECRET_FILE
Dmy-config
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not match in the shell command.
Using a wrong credential ID that is not a file.
5fill in blank
hard

Fill all three blanks to create a map of environment variables from credentials and use a condition to filter.

Jenkins
pipeline {
  agent any
  stages {
    stage('Map Credentials') {
      steps {
        script {
          def credsMap = [
            [1]: env.USERNAME,
            [2]: env.PASSWORD
          ]
          if (credsMap.[3] != null) {
            echo "Credentials are set"
          }
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
A"USER_VAR"
B"PASS_VAR"
CUSER_VAR
DPASSWORD
Attempts:
3 left
💡 Hint
Common Mistakes
Using quoted strings as map keys incorrectly.
Checking the map with a wrong key name.