Complete the code to bind a username and password credential in a Jenkins pipeline.
pipeline {
agent any
stages {
stage('Example') {
steps {
withCredentials([usernamePassword(credentialsId: '[1]', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
echo "Username is $USER"
}
}
}
}
}The credentialsId must match the ID of the stored Jenkins credential you want to bind.
Complete the code to bind a secret text credential in a Jenkins pipeline.
pipeline {
agent any
stages {
stage('Use Secret') {
steps {
withCredentials([string(credentialsId: '[1]', variable: 'SECRET')]) {
sh 'echo Secret is $SECRET'
}
}
}
}
}usernamePassword binding for secret text credentials.The string binding is used for secret text credentials, and the credentialsId must match the stored secret's ID.
Fix the error in the code to correctly bind an SSH private key credential.
pipeline {
agent any
stages {
stage('SSH Access') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: '[1]', keyFileVariable: 'KEYFILE', usernameVariable: 'USER')]) {
sh 'ssh -i $KEYFILE $USER@host'
}
}
}
}
}sshUserPrivateKey binding.The sshUserPrivateKey binding requires the correct SSH key credential ID to work properly.
Fill both blanks to bind a file credential and use it in a shell command.
pipeline {
agent any
stages {
stage('Use File') {
steps {
withCredentials([file(credentialsId: '[1]', variable: '[2]')]) {
sh 'cat $[2]'
}
}
}
}
}The file binding needs the credential ID and a variable name to hold the file path. The variable is then used in the shell command.
Fill all three blanks to create a map of environment variables from credentials and use a condition to filter.
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"
}
}
}
}
}
}The map keys should be variable names without quotes, and the condition checks if the key exists in the map.